Skip to content
Tech Interview Prep home
Technical interview guide

Bit Manipulation

Working directly on a number's binary representation with AND/OR/XOR/shifts — for O(1) tricks and memory-efficient state.

Read
40 min
Practice MCQs
25
Interview QA
25
Edition
v7
Editorial status
Reviewed

Scope: Language-neutral, with integer semantics cited from CPython 3.14, java.util as of Java 21, and the C++ standard library.

Interview QA

Treat each question like a live interview question: answer out loud first (structure, assumptions, tradeoffs), then open the model answer to spot gaps and rehearse a tighter follow-up.

Curated: · Written: · Reviewed:

QA-1

Explain two's complement representation and how it changes a production decision.

QA-2

How would you reason about the asymmetric integer range in a system you own?

QA-3

Walk through arithmetic versus logical shift, including where engineers most often get it wrong.

QA-4

What does shift amounts outside the width guarantee, and what does it deliberately leave open?

QA-5

Describe arbitrary-precision integers and the evidence you would collect before relying on it.

QA-6

Explain XOR from first principles: the truth table, why a ^ a = 0 and a ^ 0 = a, and how those identities power temp-free swaps and the appears-once problems, including where they stop working.

QA-7

Where does isolating the lowest set bit matter, and where is it irrelevant?

QA-8

Teach clearing the lowest set bit to an engineer who has only seen it as a rule of thumb.

QA-9

What are the exact expressions for getting, setting, clearing, and toggling bit i with the mask (1 << i), and where do engineers get the indexing wrong?

QA-10

Walk me through what a bit and a fixed-width word are, how to read a binary value by hand, and why the width constrains every operation that follows.

QA-11

Given an array where every element appears twice except two elements that appear once, how do you find both of them in O(n) time and O(1) extra space?

QA-12

How does bitmasks as sets change if the workload grows by two orders of magnitude?

QA-13

Given an integer mask with k set bits, how do you enumerate all 2^k of its subsets, and what is the total cost when you run that loop for every mask of an n-bit universe?

QA-14

When would you refuse a design because of iterating set bits?

QA-15

What breaks when bit tricks meet real languages and real data, and what does bitwise code that survives those hazards actually look like?

QA-16

Walk me through bitwise AND, OR, and NOT: truth tables, one worked 8-bit example each, how they mask, set and invert bits, and where they collide with && and ||.

QA-17

Walk me through counting the set bits of an integer: the naive loop, the n & (n - 1) trick with one worked example, why it runs in O(number of set bits), and what breaks at n = 0 and for negative values.

QA-18

Why do hash functions include a bit-mixing or avalanche step, and how would you recognize a weak mixer — in the code, and in a measurement?

QA-19

Explain endianness: what big- and little-endian disagree about, why shifts and masks are indifferent to byte order but byte-level reads are not, and what breaks when a packed word crosses a machine boundary.

QA-20

What trade-off does overflow behaviour force that a junior answer usually skips?

QA-21

How do you isolate the lowest set bit of an integer using two's complement operations, and why does x & (-x) work?

QA-22

How does Brian Kernighan's algorithm count set bits, and how does its time complexity compare to iterating through the word length?

QA-23

How do you represent subsets using bitmasks in dynamic programming, and how do you iterate through all submasks of every mask in O(3^n) time?

QA-24

How do you swap two variables in place using bitwise XOR, and what are the edge cases or pitfalls of this technique?

QA-25

How do you check whether a given 32-bit integer is a power of four using bit manipulation?