Given below a C program to reverse the bits in an integer.
unsigned int num; /* Reverse the bits in this number. */
unsigned int temp = num;
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. */
See also Write a C program to reverse the place of words in a sentence.