Skip to content

Instantly share code, notes, and snippets.

@corytodd
Last active April 1, 2016 02:21
Show Gist options
  • Save corytodd/056ed01228f59fee9a13d00fc25b9a62 to your computer and use it in GitHub Desktop.
Save corytodd/056ed01228f59fee9a13d00fc25b9a62 to your computer and use it in GitHub Desktop.
http://stackoverflow.com/questions/9023129/algorithm-for-bit-expansion-duplication
We are isolating each nibble, then each bit per the expansion factor (3 in this case)
out = (in | in << 8) & 0x0F00F;
out = (out | out << 4) & 0x0C30C3;
out = (out | out << 2) & 0x249249;
out *= 7;
== 0000 0000 0000 0000 1101 0101 (in == 0xD5)
--------------------------------
8< 0000 0000 1101 0101 0000 0000 (in << 8)
0000 0000 0000 0000 1101 0101 (in)
--------------------------------
| 0000 0000 1101 0101 1101 0101 (in | in << 8)
0000 0000 1111 0000 0000 1111 (0x0F00F)
--------------------------------
& 0000 0000 1101 0000 0000 0101 (out)
--------------------------------
4< 0000 1101 0000 0000 0101 0000 (out << 4)
0000 0000 1101 0000 0000 0101 (out)
--------------------------------
| 0000 1101 1101 0000 0101 0101 (out | out << 4)
0000 1100 0011 0000 1100 0011 (0x0C30C3)
--------------------------------
& 0000 1100 0001 0000 0100 0001 (out)
--------------------------------
2< 0011 0000 0100 0001 0000 0100 (out << 2)
0000 1100 0001 0000 0100 0001 (out)
--------------------------------
| 0011 1100 0101 0001 0100 0101 (out | out << 2)
0010 0100 1001 0010 0100 1001 (0x249249)
--------------------------------
& 0010 0100 0001 0000 0100 0001 (out == 0x00241041)
--------------------------------
7* 0000 0000 0000 0000 0000 0111 (7)
--------------------------------
= 1111 1100 0111 0001 1100 0111 = 0xFC71C7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment