*}
codea teams

Reverse Bits 32



/*
 * Reverses the bits in a 32-bit integer. 
 * Uses a few lines with logical and bit operations but NO loops. 
 */
{
    unsigned {32-bit-type} x;

    /* Reverse the bits of x (32 total). */
    x = ((x & 0xAAAAAAAA) >>  1) | ((x & 0x55555555) <<  1);
    x = ((x & 0xCCCCCCCC) >>  2) | ((x & 0x33333333) <<  2);
    x = ((x & 0xF0F0F0F0) >>  4) | ((x & 0x0F0F0F0F) <<  4);
    x = ((x & 0xFF00FF00) >>  8) | ((x & 0x00FF00FF) <<  8);
    x = (x >> 16) | (x << 16);
}