Bit manipulation has a reputation for being obscure. The truth is most useful bit hacks rely on just six operators you already know. Once you internalize a handful of patterns, you start seeing opportunities everywhere — flag checks, memory layouts, hash functions, graphics, and competitive programming. These ten tricks cover the patterns that show up most often in real code. Test them yourself on the BitwiseCalc homepage as you read.
1. Clear the Lowest Set Bit: x & (x - 1)
This is the Swiss Army knife of bit manipulation. Subtracting 1 from a number flips the lowest set bit to 0 and all bits below it to 1. AND-ing with the original clears exactly that bit and leaves the rest untouched.
// C
x = x & (x - 1); // clears lowest set bit
# Python
x = x & (x - 1)
// JavaScript
x = x & (x - 1);
Example: 12 & 11. 12 in binary is 1100, 11 is 1011. 1100 & 1011 = 1000 = 8. The lowest set bit (position 2, value 4) is gone. This trick is the basis for checking if a number is a power of two and counting set bits.
2. Isolate the Lowest Set Bit: x & -x
Two's complement negation flips all bits and adds 1. The result shares exactly one 1-bit with the original — the lowest set bit. AND-ing the two isolates it in a single operation.
int lowest = x & -x; // C
lowest = x & -x # Python
Example: 12 & -12. 12 = 00001100, -12 in two's complement = 11110100. AND gives 00000100 = 4. This is heavily used in Fenwick trees (Binary Indexed Trees) for prefix sum queries in O(log n).
3. Self-Cancellation: x ^ x = 0
Any number XOR'd with itself produces zero. This property is the reason XOR solves the famous "find the single number" interview problem in O(n) time and O(1) space — XOR all elements, and paired numbers cancel out, leaving only the unpaired one.
int result = 0;
for (int i = 0; i < n; i++) result ^= arr[i]; // result = the single number
4. Swap Two Variables With XOR
// No temp variable needed
a = a ^ b;
b = a ^ b; // b becomes original a
a = a ^ b; // a becomes original b
This works because (a ^ b) ^ b = a. It is more of a party trick than a performance win on modern CPUs (compilers optimize temp-variable swaps to register moves), but it demonstrates XOR's reversibility beautifully.
5. Check If a Number Is a Power of Two
A power of two has exactly one bit set. Subtracting 1 turns that bit to 0 and all lower bits to 1. If the AND is zero, you have a power of two. Special case: zero is not a power of two.
bool isPowerOfTwo = (x > 0) && ((x & (x - 1)) == 0); // C
is_power_of_two = x > 0 and (x & (x - 1)) == 0 # Python
6. Count Set Bits (Popcount) With Builtins
Modern CPUs have a POPCNT instruction, and most languages expose it. It is worth knowing rather than rolling your own Brian Kernighan loop.
// C (GCC/Clang builtin)
int count = __builtin_popcount(x);
# Python 3.8+
count = x.bit_count()
For a manual version, the Kernighan loop uses hack #1 repeatedly: while (x) { x &= (x - 1); count++; } — it iterates only once per set bit, not once per bit position. Try it on the BitwiseCalc tool to see bit-level transformations step by step.
7. Reverse Bits Pattern
Reversing the bits of a byte or word comes up in graphics (bitmap rotation), cryptography, and CRC calculations. A common 8-bit lookup table approach runs in O(1) after precomputation. For 32-bit, you can cascade swap operations:
// C: swap adjacent bits, then nibbles, then bytes
x = ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
x = ((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4);
x = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
x = (x >> 16) | (x << 16);
8. Check Odd or Even: x & 1
The least significant bit is 1 for odd numbers and 0 for even numbers. AND with 1 extracts it. This is faster than modulo on some architectures where division is expensive.
if (x & 1) { /* odd */ } else { /* even */ }
9. Divide by 2: x >> 1
Each right shift divides by 2, discarding the remainder. This is an integer division with truncation toward negative infinity in arithmetic shift. For unsigned types it is always floor division.
half = x >> 1; // same as x / 2 for positive integers
quarter = x >> 2; // same as x / 4
10. Lowercase to Uppercase: c & ~32
In ASCII, the difference between a lowercase letter and its uppercase counterpart is exactly 32 (bit 5). Clearing bit 5 converts lowercase to uppercase. The inverse — c | 32 — converts uppercase to lowercase.
char upper = c & ~32; // 'a' becomes 'A', 'A' stays 'A'
char lower = c | 32; // 'A' becomes 'a'
This works because ASCII was deliberately designed with a single-bit difference between cases. It is faster than calling toupper() when you control the input character set.
These ten patterns cover the majority of bit manipulation you will encounter. The key is practice — write them out by hand once with real numbers, and they will stick. Check this site's bitwise calculator to experiment with each hack interactively, or browse the full bitwise operations guide for deeper explanations of the underlying operators.