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.

MediumTechnical
74 practiced

A binary protocol packs a 3-bit version, 13-bit type, 20-bit length and further non-byte-aligned fields into a 64-bit header. Write pack and unpack code, and explain how you would make it correct on both big- and little-endian machines.

MediumTechnical
79 practiced

Implement a compact bit set backed by an array of 64-bit words with set, clear, get and a count of set bits in a range. Then explain how you would adapt it for 10 million flags, and for a microcontroller with 64 KB of RAM.

EasyTechnical
80 practiced

What is the difference between a logical and an arithmetic right shift? What does the C standard guarantee when you shift a negative signed value right, and how would you write code that behaves the same on every compiler?

MediumTechnical
100 practiced

Implement popcount for a 32-bit value in C with no builtins and no large lookup tables, using a fixed number of operations independent of the input. Explain each step on a small example.

MediumTechnical
121 practiced

Reverse the bits of a 32-bit unsigned integer. First do it simply, then in O(log W) steps with masks and shifts. How would you make it fast if it is called millions of times?

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.