Bit Manipulation Patterns Talentio
Sonu
1 Basic Operations - Check if Number is Even/Odd
Question: Given an integer n, determine if it’s even or odd using bit manipulation.
Examples:
• Input: n = 5 Output: “Odd”
• Input: n = 8 Output: “Even”
What to Learn:
• Use n & 1 to check the least significant bit
• If LSB is 1, number is odd; if 0, number is even
• This is faster than using modulo operator
2 Basic Operations - Check if Bit is Set
Question: Given an integer n and position i, check if the i-th bit (0-indexed from right) is set.
Examples:
• Input: n = 10 (binary: 1010), i = 1 Output: true (bit at position 1 is set)
• Input: n = 10, i = 0 Output: false (bit at position 0 is not set)
What to Learn:
• Use (n >> i) & 1 or n & (1 << i) to check if i-th bit is set
• Right shift moves bits right, left shift creates a mask
3 Basic Operations - Set/Clear/Toggle Bits
Question: Given an integer n and position i, perform:
a) Set the i-th bit
b) Clear the i-th bit
c) Toggle the i-th bit
Example: n = 10 (1010), i = 0
• Set: Output = 11 (1011)
• Clear: Output = 10 (1010)
• Toggle: Output = 11 (1011)
What to Learn:
1
• Set bit: n | (1 << i)
• Clear bit: n & ~(1 << i)
• Toggle bit: n ^ (1 << i)
4 Power of 2 Check
Question: Check if a given positive integer is a power of 2.
Examples:
• Input: n = 16 Output: true
• Input: n = 18 Output: false
What to Learn:
• A power of 2 has exactly one bit set
• Use n > 0 && (n & (n-1)) == 0
• n & (n-1) removes the rightmost set bit
5 Count Set Bits (Population Count)
Question: Count the number of 1s in the binary representation of a number.
Example: Input: n = 13 (binary: 1101) Output: 3
What to Learn:
• Method 1: Use n & (n-1) repeatedly until n becomes 0
• Method 2: Use Brian Kernighan’s algorithm
• Each n & (n-1) removes one set bit
6 Find the Only Non-Duplicate
Question: In an array where every element appears twice except one, find that unique element.
Example: Input: [4, 1, 2, 1, 2] Output: 4
What to Learn:
• XOR all elements: a ⊕ a = 0 and a ⊕ 0 = a
• Duplicates cancel out, leaving only the unique element
• XOR is commutative and associative
7 Swap Two Numbers
Question: Swap two integers without using a temporary variable.
Example: Input: a = 5, b = 3 Output: a = 3, b = 5
What to Learn:
• Use XOR: a = a ^ b, b = a ^ b, a = a ^ b
• Or do it in one line with destructuring in some languages
• Understanding XOR properties: a ⊕ b ⊕ b = a
2
8 Find Missing Number
Question: Given an array containing n distinct numbers from 0 to n, find the missing number.
Example: Input: [3, 0, 1] Output: 2
What to Learn:
• XOR all array elements with all numbers from 0 to n
• Missing number will remain after cancellation
• Alternative: use sum formula, but XOR prevents overflow
9 Two Numbers Appearing Once
Question: In an array where every element appears twice except two elements, find those two elements.
Example: Input: [1, 2, 1, 3, 2, 5] Output: [3, 5]
What to Learn:
• XOR all elements to get a ⊕ b where a, b are the unique numbers
• Find rightmost set bit in the XOR result
• Use this bit to partition numbers into two groups
• XOR each group separately
10 Reverse Bits
Question: Reverse the bits of a 32-bit unsigned integer.
Example:
• Input: n = 43261596 (00000010100101000001111010011100)
• Output: 964176192 (00111001011110000010100101000000)
What to Learn:
• Process bits from right to left
• Build result by shifting left and adding current bit
• Use n & 1 to get rightmost bit, n >>= 1 to move to next