Skip to content

Instantly share code, notes, and snippets.

@sriramster
Created February 22, 2021 14:18
Show Gist options
  • Save sriramster/bdfb5cdf1f00727a9e643b623e63bc0b to your computer and use it in GitHub Desktop.
Save sriramster/bdfb5cdf1f00727a9e643b623e63bc0b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <inttypes.h>
uint32_t rev(uint32_t x)
{
x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
x = (x << 24) | ((x & 0xFF00) << 8) |
(x >> 8) & 0xFF00 | (x >> 24);
return x;
}
int main(int argc, char *argv[])
{
uint32_t i = atoi(argv[1]);
printf("%ld", rev(i));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment