THN Interview Prep

Bit Manipulation

Bit manipulation questions ask you to treat an integer as a compact set of flags or binary digits. The key is to state which bit-level operation preserves the answer: XOR cancellation, mask extraction, shift accumulation, or carry propagation.

Mental Model

  • XOR cancellation: x ^ x = 0, x ^ 0 = x.
  • Masking: isolate or clear specific bits with &, |, ^, and ~.
  • Shifts: move a digit position left or right while building a number.
  • Carry simulation: addition can be modeled as partial sum plus shifted carry.

Diagram

Loading diagram…

Problem List

Start with 136. Single Number, 191. Number of 1 Bits, 338. Counting Bits, and 371. Sum of Two Integers.

Mark this page when you finish learning it.

Last updated on

Spotted something unclear or wrong on this page?

On this page