9am

Archive for the ‘Bit Fun’ Category

Byte Reversal

In Bit Fun, Programming on September 3, 2010 at 10:19 pm

Let’s consider reversing a byte in a speedy way using some parallelism.

Byte b;

b = b & FO >> 4  | b & OF << 4; // This swaps the nibbles.

b = b & CC >> 2 | b & 33 << 2; // swap bit pairs in each nibble.

b = b & AA >> 1 | b & 55 << 1; // swap even / odd bits

Done!