Created
April 18, 2015 11:11
-
-
Save tomcha/439b7dcd3fd833b31ed1 to your computer and use it in GitHub Desktop.
2-7
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
| #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