01

Bit Manipulation

XOR, AND, OR, and shifts for O(1) space solutions to number problems.

9 Problems1-2 hours5 Easy3 Medium1 Hard

Overview

Bit manipulation solves problems at the binary level. XOR is the workhorse: x XOR x = 0 and x XOR 0 = x.

Complexity Summary

Time Complexity

All bit operations O(1)

Space Complexity

O(1)

Key Patterns & Techniques

Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.

1

XOR Trick

Concept

a XOR a = 0, a XOR 0 = a; XOR all to find single element

Practice questions for this pattern
2

Clear Lowest Bit

Concept

n & (n - 1) removes the lowest set bit

Practice questions for this pattern
3

Isolate Lowest Bit

Concept

n & (-n) isolates the rightmost set bit

Practice questions for this pattern
4

Check kth Bit

Concept

(n >> k) & 1

Practice questions for this pattern
5

Set/Clear kth Bit

Concept

set: n | (1 << k), clear: n & ~(1 << k)

Practice questions for this pattern
6

Bit Addition

Concept

XOR gives sum without carry; AND<<1 gives carry

Practice questions for this pattern
DSA Practice Online - 150+ Coding Interview Questions | LeetCode Alternative | InstaMock - AI Mock Interview