Bitwise Operators Interview Questions for Embedded C: Quiz and Code Challenges
63 questions: multiple choice, compute-the-result blanks, an identify-the-operation table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for bitwise operators interview questions.
63
Questions
Beginner → Advanced
Level
Live score
Auto-graded
Live Score Tracker
0 Correct
0 Wrong
0 Attempted
Answer the auto-graded questions (MCQ, fill-in, true/false) to see your score.
Section 1: Multiple Choice Questions — AND, OR, XOR, NOT and Shifts
These bitwise operators interview questions in multiple-choice form are standard in embedded screening tests. Click an answer; explanations always appear.
1. What is 0x0F & 0x3C?
0000 1111 AND 0011 1100 = 0000 1100 = 0x0C.
2. What is 0x0F | 0x30?
No overlapping bits: 0011 1111.
3. What is 0xFF ^ 0x0F?
XOR with 1 flips the low nibble: 1111 0000.
4. Which operator toggles a bit?
XOR with 1 flips the bit.
5. Which operator is used to set a bit?
OR with a 1 forces the bit to 1.
6. Which operator clears a bit when combined with a mask that has a 0 there?
AND with 0 forces 0.
7. What is 1 << 4?
24 = 16.
8. What is 0x80 >> 7 for an unsigned value?
The MSB moves to bit 0.
9. Which statement is true about x << 3 for unsigned x without overflow?
23 = 8.
10. What is ~0 (an int on a 32-bit ARM)?
All 32 bits become 1, which is -1 in two’s complement.
11. What is the result of 0x0F && 0xF0?
Both operands are non-zero; logical AND gives 1.
12. What is the result of 0x0F & 0xF0?
No bit position is 1 in both.
13. For int8_t x = -8, what is x >> 1 on GCC?
Arithmetic shift copies the sign bit.
14. Which is undefined behavior?
Shift count equal to the type width is undefined.
15. In if (x & 4 == 4), what is evaluated first?
== has higher precedence than &.
Section 2: Compute the Bitwise Result (Fill in the Blanks)
Enter the result. Use upper or lower case for hex digits; spaces are ignored.
1. 0xF0 & 0x3C = 0x (two hex digits)
1111 0000 AND 0011 1100.
2. 0xF0 | 0x0F = 0x (two hex digits)
Complementary nibbles.
3. 0xAA ^ 0x55 = 0x (two hex digits)
The patterns are complements.
4. 0x0F ^ 0x0F =
x XOR x is 0.
5. 1 << 7 =
27.
6. 256 >> 4 =
256 / 16.
7. 5 << 2 =
5 × 4.
8. (uint8_t)~0xA5 = 0x (two hex digits)
Invert 1010 0101.
9. 0x35 & 0x01 =
The lowest bit of 0x35 is 1 (odd).
10. 0x12 | 0x01 = 0x (two hex digits)
Sets bit 0.
11. 0xFF >> 4 (unsigned) = 0x (hex, one digit)
High nibble moves down.
12. The mask for bit 6 is 1u <<
Bit n has mask 1u << n.
Section 3: Which Operation Does the Job?
Identify the bitwise operator for each task. Matching tasks to operators is a favourite of bitwise operators interview questions.
Task
Your answer
Result
Force bit 3 of a register to 1
AND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Force bit 3 of a register to 0
AND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Flip bit 3 of a register
AND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Find which bits differ between two values
AND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Keep only the low 4 bits
AND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
t.c: In function ‘main’:
t.c:6:15: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
6 | if (flags & 0x04 == 0x04) printf("A set\n"); else printf("A clear\n");
| ^
flags & 0x04 == 0x04 is flags & 1, which is 0 for flags = 4, so the check wrongly reports clear.
Challenge 5 — Output?: Complement of a byte
#include <stdio.h>#include <stdint.h>int main(void)
{
uint8_t x = 0x0F;
if (~x == 0xF0) printf(“equal\n”); else printf(“not equal\n”);
if ((uint8_t)~x == 0xF0) printf(“equal (cast)\n”);
return0;
}
Why is the first comparison false?
Output:
not equal
equal (cast)
~x is computed on a promoted int: 0xFFFFFFF0, which differs from 0xF0.
Challenge 6 — Output?: Extracting and swapping bytes
#include <stdio.h>#include <stdint.h>int main(void)
{
uint32_t v = 0x00F0A5C3;
printf(“%X %X %X\n”, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF);
printf(“%X\n”, (v & 0xFF00) >> 8 | (v & 0xFF) << 8);
return0;
}
What are the outputs?
Output:
F0 A5 C3
C3A5
The first line prints bytes 2, 1 and 0 of the word. The second line swaps the low two bytes: 0xA5C3 becomes 0xC3A5.
Section 6: Bitwise Operators Interview Questions for Embedded C (with Detailed Answers)
These bitwise operators interview questions come up in almost every embedded C interview. Click a question for a model answer.
Q1. Explain the difference between bitwise and logical operators with an example.Basic+
Bitwise &
Logical &&
Operates on
each bit
whole value as true/false
6 and 3
2
1
Short-circuit
No
Yes
Q2. How do you set, clear, toggle and test a bit?Basic+
Action
Expression
Set bit n
reg |= (1u << n);
Clear bit n
reg &= ~(1u << n);
Toggle bit n
reg ^= (1u << n);
Test bit n
if (reg & (1u << n))
Lesson 5.3 covers these five idioms in depth.
Q3. What does the left shift operator do, and when is it undefined?Basic+
x << n moves the bits left by n, filling with zeros; for unsigned x it multiplies by 2n modulo the type width. It is undefined when n is negative or not less than the width of the promoted type, and when a signed left operand is negative or the result overflows. So write 1u << n and validate n.
Q4. What is the difference between logical and arithmetic right shift?Basic+
Logical shift fills the vacated high bits with zero; arithmetic shift copies the sign bit. In C, unsigned operands use logical shifts. For negative signed values the behavior is implementation-defined; GCC (and almost all compilers) use arithmetic shifts.
Q5. Write a function that counts set bits. How would you make it faster?Intermediate+
popcount_iq.c
#include <stdio.h>#include <stdint.h>staticunsigned popcount(uint32_t x)
{
unsigned c = 0;
while (x) { x &= x – 1; c++; } /* clears the lowest set bit each time */return c;
}
int main(void)
{
printf(“%u %u %u\n”, popcount(0), popcount(0xF0F0), popcount(0xFFFFFFFFu));
return0;
}
Output (verified by compiling and running)
0 8 32
The loop runs once per set bit. Faster options: a 256-entry lookup table, the SWAR parallel-sum method, or the compiler built-in __builtin_popcount (on Cortex-M4 it calls a library routine; Cortex-A cores with NEON have a hardware count).
Q6. How do you test whether a number is a power of two?Intermediate+
x != 0 && (x & (x - 1)) == 0. A power of two has exactly one set bit; subtracting 1 turns that bit off and all lower bits on, so the AND is zero. The explicit x != 0 is needed because 0 would also satisfy the second test.
Q7. Swap two variables without a temporary using XOR. Why is it not recommended?Intermediate+
a ^= b; b ^= a; a ^= b;. It fails if a and b alias the same object (the value becomes 0), it is harder to read, creates a data dependency chain, and optimising compilers already produce register swaps without any temporary. Use a temporary variable.
Q8. Reverse the bits of a byte.Intermediate+
reverse_iq.c
#include <stdio.h>#include <stdint.h>staticuint8_t rev(uint8_t v)
{
uint8_t r = 0;
for (int i = 0; i < 8; i++) { r = (uint8_t)((r << 1) | (v & 1u)); v >>= 1; }
return r;
}
int main(void)
{
printf(“0x%02X -> 0x%02X, 0x%02X -> 0x%02X\n”, 0x01, rev(0x01), 0x3A, rev(0x3A));
return0;
}
Output (verified by compiling and running)
0x01 -> 0x80, 0x3A -> 0x5C
Cortex-M3/M4 have a single instruction for this on 32-bit words (RBIT), exposed through CMSIS as __RBIT(); for a byte, reverse the word and shift right by 24.
Q9. Why can (~x) == 0xF0 be false for a uint8_t x = 0x0F?Advanced+
Integer promotion converts x to int before ~ is applied, giving 0xFFFFFFF0, which is not equal to 0xF0. Cast: (uint8_t)~x == 0xF0. MISRA C:2012 rule 10.1/10.8 and the related composite-expression rules exist to catch this class of error.
Q10. How would you extract an arbitrary bit field and insert a new value into it?Advanced+
For a field of width w at bit position p: mask = ((1u << w) - 1u) << p;. Read: (reg & mask) >> p. Write: reg = (reg & ~mask) | ((value << p) & mask);. The final AND protects neighbouring bits if value is too large. CMSIS provides MODIFY_REG(REG, CLEARMASK, SETMASK) for exactly this read-modify-write.
Q11. Is x >> 1 the same as x / 2?Advanced+
For unsigned or non-negative x yes. For negative signed x they differ: -7 >> 1 is -4 (arithmetic shift, rounds toward minus infinity) while -7 / 2 is -3 (truncation toward zero), and the shift is implementation-defined. Use division for signed values; the compiler emits the shift plus a correction.
Q12. What are the endianness issues when building a 32-bit value from four bytes read over a bus?Advanced+
Using shifts and ORs is portable because it operates on values: ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | b[3] reads a big-endian field on any CPU. Casting the buffer to uint32_t * and dereferencing depends on the CPU’s endianness, may be misaligned and violates strict aliasing. Always cast each byte to uint32_t before shifting, otherwise the promotion to signed int makes b[0] << 24 overflow for values above 127.
Study Plan: Using Your Score on These Bitwise Operators Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Lesson 5.3: practical bit manipulation on registers.
70–89%
Good
Redo Examples 2, 4, 5 and 9.
Below 70%
Revise
Re-read theory sections 2–7, then retake the quiz.
Next lesson: Lesson 5.3 combines everything here into the set / clear / toggle / read / write-field idioms used on every STM32 register.
Lesson 5.2 complete!
Next up: setting, clearing, toggling and testing bits in real peripheral registers.
2 Comments