Here’s a comprehensive list of Bit Manipulation Techniques and Interview Practice Problems with related LeetCode/GeeksforGeeks questions where possible.
1. Basics of Bit Manipulation
Understanding the core bitwise operators is essential for solving bit manipulation problems.
- AND (
&): Masks bits, used to clear bits or check specific bits. - OR (
|): Sets specific bits in a number. - XOR (
^): Flips bits when two bits are different, used for toggling bits or checking equality. - NOT (
~): Inverts all bits. - Left Shift (
<<): Multiplies by powers of two. - Right Shift (
>>): Divides by powers of two. - Key Concept: Understanding how these operators work on binary representations of numbers.
Example Questions:
- LeetCode #191 - Number of 1 Bits
- LeetCode #231 - Power of Two
- LeetCode #371 - Sum of Two Integers
- GeeksforGeeks - Count set bits
- LeetCode #136 - Single Number
- GeeksforGeeks - Check whether K-th bit is set or not
- LeetCode #268 - Missing Number
- GeeksforGeeks - Check if a number is odd or even
- LeetCode #190 - Reverse Bits
- GeeksforGeeks - Toggle K-th bit
2. Power of Two Checks
- Approach: To check if a number
nis a power of two, usen & (n - 1). If the result is0, it’s a power of two.
Key Insight: Powers of two have only one bit set in their binary representation.
Example Questions:
- LeetCode #231 - Power of Two
- GeeksforGeeks - Check if a number is a power of two
- LeetCode #342 - Power of Four
- LeetCode #326 - Power of Three
- LeetCode #693 - Binary Number with Alternating Bits
- LeetCode #137 - Single Number II
- GeeksforGeeks - Count total set bits in all numbers from 1 to N
- LeetCode #338 - Counting Bits
- GeeksforGeeks - Turn off the rightmost set bit
- GeeksforGeeks - Position of rightmost set bit
3. Counting Set Bits
- Approach: Use
x &= (x - 1)to remove the lowest set bit and keep counting untilx == 0. This helps count the number of 1’s (set bits) in the binary representation.
Key Insight: Each x &= (x - 1) removes the lowest set bit, making it ideal for counting bits.
Example Questions:
- LeetCode #191 - Number of 1 Bits
- GeeksforGeeks - Brian Kernighan’s Algorithm
- LeetCode #338 - Counting Bits
- GeeksforGeeks - Flip all bits of a number
- LeetCode #476 - Number Complement
- GeeksforGeeks - Swap all odd and even bits
- LeetCode #89 - Gray Code
- GeeksforGeeks - Find parity of a number
- LeetCode #67 - Add Binary
- LeetCode #405 - Convert a Number to Hexadecimal
4. Swapping Numbers Using XOR
- Approach: You can swap two numbers
xandywithout using a temporary variable with the following operations:x = x ^ yy = x ^ yx = x ^ y
Key Insight: XORing a number with itself results in 0, so swapping can be done in-place without using additional space.
Example Questions:
- LeetCode #136 - Single Number
- GeeksforGeeks - Swap two numbers without a temporary variable
- LeetCode #137 - Single Number II
- GeeksforGeeks - Swap all odd and even bits
- LeetCode #260 - Single Number III
- LeetCode #645 - Set Mismatch
- GeeksforGeeks - Find the only odd occurring element
- LeetCode #260 - Single Number III
- LeetCode #137 - Single Number II
- LeetCode #268 - Missing Number
5. Reversing Bits
- Approach: Reversing the bits of a number involves extracting each bit and shifting it into its reverse position.
Key Insight: By shifting bits one at a time and using bitwise OR, you can reverse the order of bits in a number.
Example Questions:
- LeetCode #190 - Reverse Bits
- GeeksforGeeks - Reverse bits of a number
- LeetCode #405 - Convert a Number to Hexadecimal
- LeetCode #190 - Reverse Bits
- GeeksforGeeks - Find whether a number is a palindrome in binary representation
- LeetCode #1290 - Convert Binary Number in a Linked List to Integer
- GeeksforGeeks - Reverse individual bits of a number
- LeetCode #405 - Convert a Number to Hexadecimal
- GeeksforGeeks - Reverse a linked list using bitwise operators
- GeeksforGeeks - Convert Binary to Gray code
This list provides a comprehensive guide to mastering bit manipulation, with more than 10 examples in each section for targeted practice on LeetCode and GeeksforGeeks.