Last active
March 21, 2024 17:55
-
-
Save ikautak/4952667 to your computer and use it in GitHub Desktop.
reverse MSB LSB bit.
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
unsigned char reverse_bit8(unsigned char x) | |
{ | |
x = ((x & 0x55) << 1) | ((x & 0xAA) >> 1); | |
x = ((x & 0x33) << 2) | ((x & 0xCC) >> 2); | |
return (x << 4) | (x >> 4); | |
} | |
unsigned short reverse_bit16(unsigned short x) | |
{ | |
x = ((x & 0x5555) << 1) | ((x & 0xAAAA) >> 1); | |
x = ((x & 0x3333) << 2) | ((x & 0xCCCC) >> 2); | |
x = ((x & 0x0F0F) << 4) | ((x & 0xF0F0) >> 4); | |
return (x << 8) | (x >> 8); | |
} | |
unsigned int reverse_bit32(unsigned int x) | |
{ | |
x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1); | |
x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2); | |
x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4); | |
x = ((x & 0x00FF00FF) << 8) | ((x & 0xFF00FF00) >> 8); | |
return (x << 16) | (x >> 16); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, works like a charm