x = x>>16 | (0x0000ffff&x)<<16 this line exchanges ls 16bits with ms 16bits, i.e. 1 pair of 16bit
this logic of exchanging bits is the used for 2 pairs of 8bits each, then for 4 pairs of 4bit, then for 8 pairs of 2 bit and finally 16 pairs of 1bit. On Fri, Aug 5, 2011 at 6:04 PM, rShetty <rajeevr...@gmail.com> wrote: > This is the code to reverse the bits in an unsigned integer . > Could anyone please explain the logic of this approach ? Thank You !! > > #define reverse(x) \ > (x=x>>16|(0x0000ffff&x)<<16, \ > x=(0xff00ff00&x)>>8|(0x00ff00ff&x)<<8, \ > x=(0xf0f0f0f0&x)>>4|(0x0f0f0f0f&x)<<4, \ > x=(0xcccccccc&x)>>2|(0x33333333&x)<<2, \ > x=(0xaaaaaaaa&x)>>1|(0x55555555&x)<<1) > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.