How to reverse bits of a byte?


Question

For example, in PHP, how would I reverse the bits of the byte 11011111 to 11111011?

1
5
7/20/2019 12:25:56 PM

The straight forward approach is to perform 8 masks, 8 rotates, and 7 additions:

$blah = $blah & 128 >> 7 + $blah & 64 >> 5 + $blah & 32 >> 3 + $blah & 16 >> 1 + $blah & 8 << 1 + $blah & 4 << 3 + $blah & 2 << 5 + $blah & 1 << 7;
17
1/7/2010 5:26:57 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon