Putting all your code in the main method is seldom a good idea.
Additionally, int
is only guaranteed to 16 bits, you need to use long
.
This problem lends itself to creating a function to do the work for you:
intunsigned long bitReverse(intunsigned long value)
{
intunsigned long result = 0;
while (value != 0)
{
/* make space by shifting left on the result */
result <<= 1;
/* add the low bit in to the space */
result |= value & 1;
/* get a new low bit. */
value >>= 1;
}
return result;
}
The above function takes the low-bit off of the input value, and feeds them to the high bits of the output value.
I have put this together in a simple ideone