Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created April 18, 2015 11:11
Show Gist options
  • Select an option

  • Save tomcha/439b7dcd3fd833b31ed1 to your computer and use it in GitHub Desktop.

Select an option

Save tomcha/439b7dcd3fd833b31ed1 to your computer and use it in GitHub Desktop.
2-7
#include <stdio.h>
int invert(unsigned x, int p, int n);
int main(){
int x = 0b00011100;
int y = 0b00010000;
// x -> 0b00000000 = 0
// y -> 0b00001100 = 12
x = invert(x, 4, 3);
printf("%d\n", x);
y = invert(y, 4, 3);
printf("%d\n", y);
}
int invert(unsigned x, int p, int n){
x = x ^(~(~0 << n) << (p - (n - 1)));
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment