InterviewStack.io LogoInterviewStack.io

Bitwise Operations and Bit Manipulation Questions

Covers working with binary representations of integers and performing fundamental bitwise operations including and, or, xor, not, and left and right shifts. Includes techniques for testing setting clearing and toggling individual bits, creating and applying masks, and using bit fields and bit packed structures for compact storage. Addresses endianness and byte order considerations, bit level register manipulation for hardware and embedded systems, and efficient bit algorithms such as population count parity leading and trailing zero detection and sign extension. Encompasses practical uses in protocol parsing flag management performance optimizations and cryptographic primitives including substitution and diffusion concepts. Candidates may be assessed with coding problems that require bit tricks and algorithmic reasoning as well as design questions about data layout and low level interfaces.

MediumTechnical
60 practiced
On a single-core ARM Cortex-M MCU, both main thread and an ISR need to set and clear individual bits in a shared 32-bit MMIO control register. Describe three safe approaches to perform these bit manipulations atomically and list pros/cons for each (interrupt-disable RMW, bit-banding, dedicated SET/CLR registers). Provide short pseudo-code or code snippets for each approach.
MediumTechnical
75 practiced
Write uint32_t reverse_bits32(uint32_t x) in C that reverses the bit order (bit 0 <-> bit 31, etc.). Implement an O(log W) approach using masks and shifts (swap bit-pairs, nibbles, bytes, half-words) rather than a per-bit loop. Explain why this approach is efficient on microcontrollers without a dedicated bit-reverse instruction.
HardTechnical
53 practiced
A shared MMIO register controls GPIO and multiple CPU cores may update different bits concurrently. Read-modify-write races cause lost updates. Propose hardware and software strategies to ensure correct concurrent bit updates with minimal latency. Discuss hardware SET/CLR registers, LL/SC or CAS approaches, spinlocks, and compare trade-offs (latency, power, complexity). Provide example pseudo-code for a CAS-based bit-update loop.
HardTechnical
60 practiced
Given two large arrays A and B of uint64_t fingerprints (length N up to 1e6), implement an efficient routine in C that computes the Hamming distance for each pair: out[i] = popcountll(A[i] ^ B[i]) and stores results in an 8-bit array. Discuss optimizations for memory bandwidth, usage of hardware POPCNT, SIMD, multithreading, and embedded SoC constraints (cache, DMA).
MediumTechnical
85 practiced
Implement two functions in C: int count_leading_zeros32(uint32_t x) and int count_trailing_zeros32(uint32_t x) without using compiler intrinsics. They must return 32 when x==0. Describe an O(log W) algorithm, and briefly explain the de Bruijn trick for CTZ.

Unlock Full Question Bank

Get access to hundreds of Bitwise Operations and Bit Manipulation interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.