I am ashamed to admit that I do not know as much about bits and bit manipulations as I probably should. I tried to fix this this weekend by writing a few "reverse bit orders" and "counting bit bits." I gave an example from here , but when I implemented it as shown below, I found that I needed to loop while <29. If I loop while <32 (as in the example) Then, when I try to print an integer (using written by my printBits function), I seem to be missing the first 3 bits. It makes no sense to me, can someone help me?
Thanks for the help, I added comments to show the changes I made.
int reverse(int n)
{
int r = 0;
int i = 0;
for(i = 0; i < 29; i++)
{
r = (r << 1) + (n & 1);
n >>=1;
}
return r;
}
Here is my printBits function:
void printBits(int n)
{
int mask = 0X10000000;
while (mask)
{
if (mask & n)
{
printf("1");
}
else
{
printf("0");
}
mask >>= 1;
}
printf("\n");
}
?
int reverse2(int n)
{
int r = n;
int s = sizeof(n) * 7;
for (n >>= 1; n; n >>=1)
{
r <<=1;
r |= n & 1;
s--;
r <<= s;
return r;
}