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.

EasyTechnical
79 practiced

Write functions to test, set, clear and toggle bit k (0-indexed from the least significant bit) of a 32-bit integer. Decide how to handle out-of-range k, and say what would change if the value lived in a shared hardware status register.

MediumTechnical
86 practiced

Implement integer square root, floor(sqrt(n)), for a 64-bit unsigned integer using only integer operations and a digit-by-digit bitwise method. Explain the invariant and trace a small input.

HardTechnical
85 practiced

Design a static bit vector that supports rank(pos), the number of set bits up to pos, and select(k), the position of the k-th set bit. State the space overhead you accept and the time you get for each operation.

EasyTechnical
90 practiced

Why does x & -x give the lowest set bit of x in two's complement? Walk through a positive and a negative example, then show how to use it to loop over all set bits of a word and where else it shows up in practice.

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?

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.