Skip to content

Instantly share code, notes, and snippets.

@heatblazer
Created March 11, 2020 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heatblazer/d59ca9eceabdf74231a216222b2ec577 to your computer and use it in GitHub Desktop.
Save heatblazer/d59ca9eceabdf74231a216222b2ec577 to your computer and use it in GitHub Desktop.
#include <stdio.h>
static unsigned int flipb(unsigned int x)
{
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return((x >> 16) | (x << 16));
}
static unsigned int lrot(unsigned int x, int n)
{
return (x << n) | (x >> -n);
}
static unsigned int rrot(unsigned int x, int n)
{
return (x << -n) | (x >> n);
}
union bitset
{
unsigned int val;
struct
{
unsigned char b0: 1;
unsigned char b1: 1;
unsigned char b2: 1;
unsigned char b3: 1;
unsigned char b4: 1;
unsigned char b5: 1;
unsigned char b6: 1;
unsigned char b7: 1;
} m_field[sizeof(int)];
unsigned char data[sizeof(int)];
};
int main()
{
// int i =3;
// i = FLIPBACK(i);
// printf("%d", i);
union bitset bs = {.val = 0xff};
union bitset bs1 = {.val = 0xFF000003};
bs1.val = flipb(bs1.val);
bs.val = lrot(bs.val, 16);
bs.val = rrot(bs.val, 3);
///printf("%d\r\n", bs.val);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment