blob: 97af143b4aa766d08a2f64bf9a00edf9adc454e1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <stdio.h>
/* inverts n bits of x starting at p */
unsigned invert(unsigned x, int p, int n) {
unsigned mask, y;
// mask to unset n bits starting at p:
mask = (~0 << p) | ~(~0 << (p - n));
// invert and extract bits:
y = ~(x & ~mask) & ~mask;
// mask out x and copy inverted bits into x:
return (x & mask) | y;
}
|