Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to reverse Bits in a 32 bit word

Status
Not open for further replies.

rsnambiar

Programmer
Apr 24, 2005
1
0
0
IN


Can explain me with a C source code how reverse bits in 32 bit word.
 
Do you mean reverse each bit ie. 1110 to 0001 which could be done like this using the NOT operator:-

int val;
val = ~val;

Or do you mean reverse the order of the bits ie. 1110 to 0111 which could be done like this(assumes that your implementation uses 32bit ints):-

int val, temp, i;

val = 0xFFF03001; /*for example*/
temp = 0;


for (i=0; i < 32; ++i)
{
temp <<= 1; /* left shift one place */
temp |= (val & 1);
val >>= 1; /* right shift one place */
}
val = temp; /* val should now be 0x800C0FFF */

Hope this helps, sorry about the lack of explanation,

paulf

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top