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
69 practiced

Implement roundUpToPowerOfTwo(x) (smallest power of two >= x, 0 for x == 0) and roundDownToPowerOfTwo(x) for 32-bit unsigned values without loops over every bit. Cover x already a power of two and x above 2^31.

EasyTechnical
73 practiced

Pack two 16-bit unsigned fields into one 32-bit word and write the matching extraction. Then extend it to three fields of different widths, one of them a signed 16-bit temperature.

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?

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.

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.

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.