9am

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!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: