|
| 1 | +--- |
| 2 | +seoTitle: Advanced Bitwise Hacks – O(1) Fast Formulas |
| 3 | +description: "Master advanced bitwise operations including Brian Kernighan's Algorithm, XOR Swaps, and Power of 2 checks." |
| 4 | +keywords: "bitwise hacks, brian kernighan algorithm, xor swap, power of 2, bit manipulation, fast formulas, O(1), VR-Rathod, Code-Note, code note vr, vr book" |
| 5 | +--- |
| 6 | + |
| 7 | +> [!info] What are Bitwise Hacks? |
| 8 | +> Bitwise hacks are extremely fast, low-level formulas that manipulate integer data directly at the binary level using bitwise operators (`&`, `|`, `^`, `~`, `<<`, `>>`). |
| 9 | +> They execute in a single CPU cycle ($O(1)$) and are essential for competitive programming, graphics engines, and performance-critical systems. |
| 10 | +
|
| 11 | +- # 1. Check if a Number is a Power of 2 |
| 12 | + - **Formula**: `(x & (x - 1)) == 0` |
| 13 | + - **Explanation**: A power of 2 has exactly one bit set to `1` (e.g., `8` is `1000`). Subtracting `1` from it flips all bits starting from that single `1` downwards (e.g., `7` is `0111`). Therefore, `x & (x - 1)` clears that single bit, leaving `0`. |
| 14 | + - **Note**: Ensure `x > 0` before checking. |
| 15 | + - ```c++ |
| 16 | + bool isPowerOfTwo(int x) { |
| 17 | + return x > 0 && (x & (x - 1)) == 0; |
| 18 | + } |
| 19 | + ``` |
| 20 | + |
| 21 | +- # 2. Count Set Bits (Brian Kernighan’s Algorithm) |
| 22 | + - **Formula**: `x = x & (x - 1)` |
| 23 | + - **Explanation**: Instead of checking every bit in an integer ($O(\text{Bits})$ time), Brian Kernighan's algorithm drops the lowest set bit in each iteration. |
| 24 | + - **Time Complexity**: $O(K)$ where $K$ is the number of set bits (`1`s). |
| 25 | + - ```python |
| 26 | + def count_set_bits(x): |
| 27 | + count = 0 |
| 28 | + while x > 0: |
| 29 | + x = x & (x - 1) # Clear the lowest set bit |
| 30 | + count += 1 |
| 31 | + return count |
| 32 | + ``` |
| 33 | + |
| 34 | +- # 3. Isolate the Rightmost 1-Bit |
| 35 | + - **Formula**: `x & (-x)` |
| 36 | + - **Explanation**: In Two's Complement representation, `-x` is equivalent to `(~x) + 1`. This mathematical property flips all bits above the lowest set bit, leaving only the rightmost set bit in common with `x`. |
| 37 | + - Highly useful in building the Fenwick Tree (Binary Indexed Tree). |
| 38 | + - ```c++ |
| 39 | + int isolateRightmostBit(int x) { |
| 40 | + return x & (-x); |
| 41 | + } |
| 42 | + ``` |
| 43 | + |
| 44 | +- # 4. The XOR Swap |
| 45 | + - **Formula**: `a ^= b; b ^= a; a ^= b;` |
| 46 | + - **Explanation**: Swaps the values of two variables without using a temporary third variable, utilizing the property $A \oplus B \oplus B = A$. |
| 47 | + - ```c++ |
| 48 | + void swap(int &a, int &b) { |
| 49 | + if (&a != &b) { // Must not point to the same memory location! |
| 50 | + a ^= b; |
| 51 | + b ^= a; |
| 52 | + a ^= b; |
| 53 | + } |
| 54 | + } |
| 55 | + ``` |
| 56 | + |
| 57 | +- # Key Takeaways |
| 58 | + collapsed:: true |
| 59 | + - Bitwise hacks exploit the binary representation of integers to replace loops and conditionals with single-instruction math. |
| 60 | + - They are the foundation of many high-performance data structures like Fenwick Trees and Zobrist Hashing. |
| 61 | + |
| 62 | +- # More Learn |
| 63 | + collapsed:: true |
| 64 | + - ## GitHub & Webs |
| 65 | + - [Bit Twiddling Hacks (Stanford)](https://graphics.stanford.edu/~seander/bithacks.html) |
| 66 | + - [GeeksforGeeks -> Bit Magic](https://www.geeksforgeeks.org/bitwise-hacks-for-competitive-programming/) |
0 commit comments