Bit Manipulation Questions

Working directly with binary representations: bitwise operators (AND, OR, XOR, NOT, shifts, rotates) and how they differ from the logical operators, masking, two's complement and sign extension, endianness, and classic bit tricks (popcount, parity, leading and trailing zeros, lowest and highest set bit, power-of-two tests and rounding, Gray code, bit reversal, swapping without a temporary, and finding unique or missing elements with XOR). Covers bitsets and bit-vectors (including rank and select, set operations, and bitmap-based counting and sieving), packing and unpacking fields into words, protocol headers and bit-level streams, C bitfields and their portability limits, bitmask enumeration of subsets, maximum XOR and Hamming distance, SIMD and table-driven popcount, branchless arithmetic and when it pays off, overflow detection with bit operations, and integer square root by bitwise methods. Especially relevant where memory and cycles are constrained. Excludes general number theory and modular arithmetic, device register programming, Bloom filters and hash design, and cryptographic primitives, which are covered elsewhere.

HardTechnical
90 practiced

You have two versions of a hot function, one with a data-dependent branch and one branchless. How would you decide which to ship? Describe what you would measure, what input distributions matter and how the answer can flip across CPUs.

EasyTechnical
87 practiced

Every value in an integer array appears exactly twice except one that appears once. Find it in O(n) time and O(1) extra space, and explain why the method works.

EasyTechnical
92 practiced

What is endianness and how does it affect serialising a 32-bit integer to bytes or reading it from a network packet? Write code that writes and reads a 32-bit value in a fixed byte order independent of the host.

HardTechnical
92 practiced

A hot loop extracts bits from words and branches on each one, and profiling shows heavy branch misprediction. Refactor it to reduce mispredictions and improve instruction-level parallelism, and explain why each change helps.

EasyTechnical
76 practiced

Compare std::bitset<N>, std::vector<bool> and a hand-rolled packed bit array in C++. When would you choose each, considering memory, speed of set, test and scan, and API surprises?

Unlock Full Question Bank

Get access to all Bit Manipulation interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.