Skip to content

Instantly share code, notes, and snippets.

@mrcat323
Created February 27, 2020 14:55
Show Gist options
  • Save mrcat323/6ab1f379832dccd26cd1e133752daa45 to your computer and use it in GitHub Desktop.
Save mrcat323/6ab1f379832dccd26cd1e133752daa45 to your computer and use it in GitHub Desktop.
#include <stdio.h>
unsigned getbits(unsigned, int, int);
unsigned setbits(unsigned, int, int, unsigned);
int main(void)
{
int n;
int x, y;
n = 58 ^ 20;
n = 57 << 1;
x = y = 64;
/* printf("%d\n", 57 & 1 + 9); */
/* printf("%d\n", 57 | 19); */
/* printf("%d\n", 57 ^ 19); */
/* printf("%d - right\n", 57 >> 2); */
/* printf("%d\n", 57 << 2); */
printf("%d\n", (~(~0 << 3)));
printf("%u\n", setbits(x, 7, 3, y));
printf("%d\n", x);
printf("%d\n", y);
return 0;
}
unsigned getbits(unsigned x, int p, int n)
{
return (x >> (p + 1 - n)) & ~(~0 << n);
}
/* unsigned setbits(unsigned x, int p, int n, int y) */
/* { */
/* return getbits(x, p, n) | y >> n; */
/* } */
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
return (x & ((~0 << (p+1)) | ~(~0 << (p+1-n)))) | ((y & ~(~0 << n)) << (p+1-n));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment