Skip to content

Instantly share code, notes, and snippets.

@xymopen
Last active October 9, 2017 15:10
Show Gist options
  • Save xymopen/4ceeb2cbd9a159179c4bd01b01497882 to your computer and use it in GitHub Desktop.
Save xymopen/4ceeb2cbd9a159179c4bd01b01497882 to your computer and use it in GitHub Desktop.
Code snippet to reverse a byte from StackOverflow
// https://stackoverflow.com/questions/2602823/in-c-c-whats-the-simplest-way-to-reverse-the-order-of-bits-in-a-byte
unsigned char rvs_byte( unsigned char b ) {
b = ( b & 0b11110000 ) >> 4 | ( b & 0b00001111 ) << 4;
b = ( b & 0b11001100 ) >> 2 | ( b & 0b00110011 ) << 2;
b = ( b & 0b10101010 ) >> 1 | ( b & 0b01010101 ) << 1;
return b;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment