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.

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
58 practiced
A peripheral's 32-bit control register has: bits [0..7] writable, [8..15] read-only status, [16..23] reserved (must be written as 0), and [24..31] write-only command bits. Multiple threads and ISRs update the writable bits concurrently. Design robust, portable C code to set/clear specific writable bits without modifying RO/reserved fields, minimizing race windows. Explain testing strategy on hardware to validate correctness and race freedom.
MediumSystem Design
57 practiced
A 12-byte telemetry packet must pack many variable-length fields bitwise to minimize airtime. As an embedded developer, propose a portable bit-field layout and parsing/writing strategy that addresses alignment, endianness, future extension (versioning), and error detection. Provide pseudocode or an outline of your parser and how you would test it across big/little-endian hosts.
EasyTechnical
75 practiced
You have a volatile 32-bit STATUS_REG that holds device flags. Implement a function void toggle_flags(volatile uint32_t *reg, uint32_t mask) that toggles bits specified by mask without touching reserved bits. Assume mask contains only writable bits. Provide a safe approach for single-core scenario where both main code and ISR may call toggle_flags.
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.

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.