On Sun, Dec 19, 2010 at 2:36 AM, Dave dave_and_da...@juno.com wrote:
For 32-bit integers:
x = ((x 16) 0X) | ((x 0X) 16);
x = ((x 8) 0X00FF00FF) | ((x 0X00FF00FF) 8);
x = ((x 4) 0X0F0F0F0F) | ((x 0X0F0F0F0F) 4);
x = ((x 2) 0X) | ((x 0X)
@Pratik:
The first line swaps the leftmost 16 bits with the rightmost 16 bits.
The second line swaps all adjacent pairs of 8-bit quantities.
The third line swaps all adjacent pairs of 4-bit quantities.
Etc.
Dave
On Dec 20, 11:29 am, Pratik Kathalkar dancewithpra...@gmail.com
wrote:
On Sun, Dec
unsigned int reverseBits(unsigned int num)
{
unsigned int count = sizeof(num) * 8 - 1;
unsigned int reverse_num = num;
num = 1;
while(num)
{
reverse_num = 1;
reverse_num |= num 1;
num = 1;
count--;
}
reverse_num = count;
return
For 32-bit integers:
x = ((x 16) 0X) | ((x 0X) 16);
x = ((x 8) 0X00FF00FF) | ((x 0X00FF00FF) 8);
x = ((x 4) 0X0F0F0F0F) | ((x 0X0F0F0F0F) 4);
x = ((x 2) 0X) | ((x 0X) 2);
x = ((x 1) 0X) | ((x 0X) 1);
x is now the binary
There is a great list of bit manipulation hacks that has been around
and added to for years. See for example:
http://www-graphics.stanford.edu/~seander/bithacks.html
On Dec 18, 2:22 pm, bittu shashank7andr...@gmail.com wrote:
Write an Efficient C Program to Reverse Bits of a Number
--
You