Skip to content

Instantly share code, notes, and snippets.

@jsarenik
Created July 14, 2016 12:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jsarenik/899bd83240729bfe3cc615dcb2133c8f to your computer and use it in GitHub Desktop.
Change endianess (e.g. 01101 to 10110 bit-wise)
#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