This displayBits function works fine in this code in displaying the bits of the input integer. I put the same thing into function reverseBits and have been trying different things to get it to reverse the order, but can't seem to get it right. I think it's a minor modification needed. Now it outputs:
255 = 00000000 00000000 00000000 11111111
but I want:
255 = 11111111 00000000 00000000 00000000
#include <stdio.h>
void displayBits(unsigned value);
void reverseBits(unsigned reverse);
int main(void)
{
unsigned x;
printf("Enter an Integer: ");
scanf("%u", &x);
displayBits(x);
reverseBits(x);
return 0;
}
void displayBits(unsigned value)
{
unsigned c;
unsigned displayMask = 1 << 31;
printf("%10u = ", value);
for(c = 1; c <= 32; c++)
{
putchar(value & displayMask ? '1' : '0');
value <<= 1;
if (c %8 == 0)
{
putchar(' ');
}
}
putchar('\n');
}
void reverseBits(unsigned reverse)
{
unsigned c;
unsigned displayMask = 1 << 31;
printf("%10u = ", reverse);
for(c = 1; c <= 32; c++)
{
putchar(reverse & displayMask ? '1' : '0');
reverse <<= 1 ;
if (c %8 == 0)
{
putchar(' ');
}
}
putchar('\n');
}