Bit Manipulation Tricks
We’ll assume:
int x; // some non-negative integer
int k; // 0-based bit index (0 = least significant bit)and use masks like 1 << k which has only bit k equal 1.
Checking if a Bit is 1
bool isSet = (x >> k) & 1;x >> k→ shift right so that the k-th bit comes to the rightmost position.(x >> k) & 1→ keeps only the rightmost bit, zeroes everything else.- The result is
0or1, which is the same asfalseortrue.
Examples
Check if bit 1 of
xis set.
int x = 10; // 10 = 1010 (in binary)
int k = 1;
bool isSet = (x >> 1) & 1; // 1010 >> 1 = 101; 101 & 1 = 1 (true)Given
nandk, write a function that determines if thek-th bit ofnis set.
bool kthBitIsSet(int n, int k) {
return (n >> k) && 1;
}Setting the k-th Bit to 1
x |= (1 << k);|sets bits where either side has 1.- So we force bit
kto 1, leave others unchanged.
Example
int x = 4; // 0100
x |= (1 << 0); // 0100 | 0001 = 0101 (5)Clearing the k-th Bit (Set it to 0)
x &= ~(1 << k);1 << k→ mask with bitkequal 1.~(1 << k)→ mask with bitkequal 0, all others equal 1.&with that mask clears bitk.
Example
int x = 13; // 1101
x &= ~(1 << 2); // ~(0100) = ...1011
// 1101 & 1011 = 1001 (9)Now bit 2 is 0.
Toggling (Flipping) the k-th Bit
x ^= (1 << k);^(XOR) flips a bit if the mask has 1:- 0 ^ 1 = 1
- 1 ^ 1 = 0
Example
int x = 5; // 0101
x ^= (1 << 2); // 0101 ^ 0100 = 0001 (1)Bit 2 was 1 → now 0.
Remove the Lowest Set Bit
This is a very famous trick.
x = x & (x - 1);This clears the lowest (rightmost) 1-bit in x.
Why This Works
Think about what happens to the bits when you subtract 1:
- When you do
x - 1, you scan from the right until you find the first 1-bit. - That lowest 1-bit becomes
0. - All the bits to the right of it (which were
0s) become1s (because of borrowing).
So around the lowest set bit, the pattern looks like this:
x: .... 1 0 0 0 0
x - 1: .... 0 1 1 1 1
Now AND them:
x = .... 1 0 0 0 0
x - 1 = .... 0 1 1 1 1
-------------------------
x&(x-1) = .... 0 0 0 0 0
- The lowest 1-bit becomes
0because it’s1 & 0. - All bits to the right are
0inxand something inx - 1, so they also become0. - All bits to the left of that position are unchanged, because subtraction didn’t touch them (same bits in both
xandx - 1).
So x & (x - 1) is just x with its rightmost 1-bit cleared.
Example
x = 12 = 1100
x - 1 = 11 = 1011
x & (x - 1) = 8 = 1000
So we removed the lowest 1-bit (bit 2).
Check if x is a Power of Two
We assume x is a positive number, and use the previous trick.
bool isPowerOfTwo = (x & (x - 1)) == 0;Why this works:
- A power of two in binary (
1,10,100,1000, …) has exactly one bit set. - If you do
x & (x - 1)it removes the lowest set bit. - If there was exactly one set bit, result becomes 0.
So:
- “
x & (x - 1)is 0” ⇔ “xhas exactly one set bit”.
Examples
x = 8 (1000)→x & (x-1) = 1000 & 0111 = 0000→ power of two.x = 12 (1100)→x & (x-1) = 1100 & 1011 = 1000→ not zero → not a power of two.
Get the Lowest Set Bit Only
int lowest = x & -x;Here -x is the two’s-complement negative of x.
Pattern:
x & -xisolates the rightmost 1-bit and zeroes everything else.
Example
x = 12 = ...00001100
-x (two's complement) = ...11110100
x & -x = ...00000100 = 4
So the lowest set bit corresponds to value 4 (bit 2).
Quick Summary
- Check bit k:
isSet = (x >> k) & 1; - Set bit k:
x |= (1 << k); - Clear bit k:
x &= ~(1 << k); - Toggle bit k:
x ^= (1 << k); - Remove lowest set bit:
x &= (x - 1); - Check power of two:
x > 0 && (x & (x - 1)) == 0 - Get lowest set bit:
lowest = x & -x;
These are the the most important bit manipulation tricks.