Bit Manipulation Questions
Working directly with binary representations: bitwise operators, masking, shifting, bit counting, and integer-encoding tricks. Covers using bit-level operations for compact state, fast arithmetic, and low-level optimization. Especially relevant where memory and cycles are constrained.
Explain ARM Cortex-M 'bit-banding': show the formula to compute the alias address for a bit located at byte address 'addr' and bit number 'bit' within the SRAM bit-band region (bit-band base 0x20000000, alias base 0x22000000). Then compute the alias address for address 0x2000A000, bit 5. Show the arithmetic step-by-step.
Explain how C bitfields work (for example: struct { unsigned a:3; unsigned b:5; }). Discuss advantages and disadvantages of using bitfields for hardware register definitions in embedded firmware. Cover portability, ordering, packing, volatile semantics, atomicity, and debugging. Propose a safer portable alternative for register access.
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.
When is it safe to replace multiplication or division by powers of two with left or right shifts in C? Give examples where this transformation is incorrect (signed negatives, overflow) and provide safe idiomatic patterns that embedded developers can use for speed-sensitive code.
Write a short C function to detect endianness at runtime without compiler macros. The function should return 1 for little-endian and 0 for big-endian. Explain how this detection can affect how you read/write multi-byte registers or wire formats in embedded systems.
Unlock Full Question Bank
Get access to all 33 Bit Manipulation interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.