Skip to content

Instantly share code, notes, and snippets.

@jsarenik jsarenik/swap.c
Created Jul 14, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.