Given below a C program to reverse the bits in an integer.
int main()
{
unsigned int num; /* Reverse the bits in this number. */
unsigned int temp = num;
int i;
for (i = (sizeof(num)*8-1); i; i–)
{
temp = temp | (num & 1);
temp <<= 1; num >>= 1;
}
temp = temp | (num & 1); /* Now temp has the reversed bits of num. */
return 0;
}
See also Write a C program to reverse the place of words in a sentence.