Created
July 14, 2016 12:40
-
-
Save jsarenik/899bd83240729bfe3cc615dcb2133c8f to your computer and use it in GitHub Desktop.
Change endianess (e.g. 01101 to 10110 bit-wise)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
unsigned int reverse(unsigned char b) { | |
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; | |
b = (b & 0xCC) >> 2 | (b & 0x33) << 2; | |
b = (b & 0xAA) >> 1 | (b & 0x55) << 1; | |
return b; | |
} | |
int | |
main () | |
{ | |
unsigned int c; | |
while (1) { | |
c = getchar(); | |
if (c == EOF) | |
break; | |
putchar(reverse(c)); | |
}; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/2602823/in-c-c-whats-the-simplest-way-to-reverse-the-order-of-bits-in-a-byte