⬅ Previous Topic
Tree / Graph Traversal Technique in DSANext Topic ⮕
Hashing Technique⬅ Previous Topic
Tree / Graph Traversal Technique in DSANext Topic ⮕
Hashing TechniqueTopic Contents
Bit Manipulation is a powerful technique in DSA where we use binary operations to solve problems efficiently. It's commonly used in problems related to power-of-two checks, counting bits, generating subsets, toggling bits, and more.
It relies on bitwise operators like:
&
(AND)|
(OR)^
(XOR)~
(NOT)<<
(left shift)>>
(right shift)To determine if a number is a power of two using bit manipulation, we rely on the fact that powers of two have a very unique pattern in binary:
0001
0010
0100
1000
Notice a pattern? Every power of two has exactly one bit set to 1 and all other bits are 0.
This is the key to our trick: if a number n
is a power of two, then n
has exactly one set bit. And here’s what happens when you subtract 1 from such a number:
n = 8
(binary = 1000
)n - 1 = 7
(binary = 0111
)Now, let’s perform a bitwise AND between n
and n - 1
:
1000 (8)
& 0111 (7)
= 0000
The result is 0
. This will only happen for numbers that are powers of two.
n
is greater than 0 (since 0 and negatives are not powers of 2).n & (n - 1)
n
is a power of 2. Otherwise, it’s not.Bit manipulation provides an incredibly fast and elegant solution here. Instead of looping or dividing repeatedly to check powers of 2, we do just one &
operation:
This is far more efficient than traditional methods like looping, especially for performance-critical applications like embedded systems, competitive programming, and large-scale simulations.
// Pseudocode
function isPowerOfTwo(n):
return n > 0 and (n & (n - 1)) == 0
n = 16
→ binary = 10000
→ n & (n-1) = 10000 & 01111 = 0
→ Power of 2n = 18
→ binary = 10010
→ n & (n-1) ≠ 0
→ ❌ Not a power of 2Note: This method only works for positive integers. If n ≤ 0
, it's not a power of 2.
Count how many bits are set to 1 in the binary representation of a number.
// Pseudocode
function countSetBits(n):
count = 0
while n != 0:
n = n & (n - 1)
count += 1
return count
Explanation: Each iteration clears the rightmost set bit.
Flip the bit at position k
(0-based from right).
// Pseudocode
function toggleBit(n, k):
return n ^ (1 << k)
Check if bit at position k
is 1.
// Pseudocode
function isKthBitSet(n, k):
return (n & (1 << k)) != 0
// Pseudocode
function turnOffRightmostBit(n):
return n & (n - 1)
// Pseudocode
function getRightmostSetBit(n):
return n & -n
Explanation: -n
is the two's complement of n
.
n & 1
)a ^= b; b ^= a; a ^= b
)Given an array where every element appears twice except one, find the unique one.
// Pseudocode
function findUnique(arr):
result = 0
for num in arr:
result = result ^ num
return result
Why it works: XOR cancels out pairs (a ^ a = 0), and 0 ^ b = b.
Bit Manipulation is an efficient and low-level technique in DSA. It is particularly useful when solving problems related to flags, parity, and optimization. Mastering bit operations can lead to elegant and performant solutions for a variety of tricky problems.
⬅ Previous Topic
Tree / Graph Traversal Technique in DSANext Topic ⮕
Hashing TechniqueYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.