Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Exercise 2-7.
- * Write a function invert(x,p,n) that returns x with the n bits that begin at
- * position p inverted (i.e., 1 changed into 0 and vice versa), leaving the
- * others unchanged.
- */
- #include <stdio.h>
- unsigned int
- invert(unsigned int x, int p, int n) {
- unsigned int mask = (~0 << (p+1-n)) ^ (~0<<(p+1));
- return x ^ mask;
- }
- int
- main() {
- unsigned int x = 0xff;
- unsigned int y = 0x00;
- y = invert(x,2,1);
- printf ("y = %X \n", y);
- }
Advertisement
Add Comment
Please, Sign In to add comment