Q42 Write a C program to reverse the bits in an integer.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.