Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Bit Manipulation

Use AND, OR, XOR and shifts for compact, fast tricks.

Intermediate7 problems3 interactive subtopicsO(n) time · O(1) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/5Start at 0
result = 0 (000)
01234
4
1
2
1
2
result
0
bit 2
0
bit 1
0
bit 0
  • visited / done
  • active this step
  • result
  • ruled out
Space Play←→ StepR Replay
1 / 5
PYTHONnums = [4, 1, 2, 1, 2]
1def single_number(nums):
▶2 result = 0
3 for x in nums:
4 result ^= x
5 return result
STEP 1 OF 5Setup

Start at 0

XOR with 0 changes nothing, so 0 is a safe starting value.

VARIABLES

x
-
result
0 (000)
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Integers are stored as bits. Operators like & (and), | (or), ^ (xor), << and >> (shifts) read and change those bits directly.

Why it matters

Some problems have neat O(1)-space answers with bits: finding the odd one out, counting set bits, or treating a subset as a bitmask.

Think of it like this

A row of light switches: each bit is one switch, and a mask flips or checks several at once.

Use it when

  • Every number appears twice except one (XOR)
  • Counting or checking set bits
  • Subsets of a small set as bitmasks (n ≤ 20)

Avoid it when

  • Readability matters more than a tiny speed-up
  • Negative numbers in Python when 32-bit behaviour is expected - mask with 0xFFFFFFFF

Complexity cheatsheet

OperationTimeSpace
XOR all the numbersO(n)O(1)
Count set bits with x & (x − 1)O(set bits)O(1)
Try every mask of n bitsO(2ⁿ)O(1)

Often confused with: hash sets - they also find the unique number, but use O(n) memory.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

XORbinarypower of twoappears twicewithout + or −
  • x & (x − 1) clears the lowest set bit
  • x ^ x = 0 and x ^ 0 = x
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Expecting ~x to flip bits unsigned - in Python ~x is −x − 1
  • Forgetting Python ints never overflow, so negatives don’t wrap at 32 bits
  • Using bit tricks where they hurt clarity

Edge cases to test

  • A single-element array
  • Negative numbers
  • Zero in the input
Interview tip: Name the identity you rely on (x ^ x = 0) before using it - the trick then reads as reasoning.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

What does x & (x − 1) do?

06

Practice

Apply what you just watched on real interview problems.

Generate Bit Manipulation problem in AI DSA Practice →
  • BeginnerSingle NumberLeetCode 136 ↗ (opens in a new tab)
  • IntermediateCounting BitsLeetCode 338 ↗ (opens in a new tab)
  • AdvancedBitwise AND of Numbers RangeLeetCode 201 ↗ (opens in a new tab)
Shortest Path Algorithms