Thursday, April 23, 2009

Flipping burgers and bits is not same

How do we flip bits in a integer from p1 to p2 locations in C? Here is the macro for it.
#define flip_bits(x, p1, p2) (x ^ (((unsigned int)~0 >> ((sizeof(int) * 8) - (p2-p1+1))) << p1))

No comments:

Post a Comment