Quiz- Bitwise Operators Interview Questions for Embedded C: Quiz and Code Challenges-Free Embedded C Tutorial Online

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

MODULE 5 · LESSON 5.2 · PRACTICE

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?

2. What is 0x0F | 0x30?

3. What is 0xFF ^ 0x0F?

4. Which operator toggles a bit?

5. Which operator is used to set a bit?

6. Which operator clears a bit when combined with a mask that has a 0 there?

7. What is 1 << 4?

8. What is 0x80 >> 7 for an unsigned value?

9. Which statement is true about x << 3 for unsigned x without overflow?

10. What is ~0 (an int on a 32-bit ARM)?

11. What is the result of 0x0F && 0xF0?

12. What is the result of 0x0F & 0xF0?

13. For int8_t x = -8, what is x >> 1 on GCC?

14. Which is undefined behavior?

15. In if (x & 4 == 4), what is evaluated first?

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)

2. 0xF0 | 0x0F = 0x (two hex digits)

3. 0xAA ^ 0x55 = 0x (two hex digits)

4. 0x0F ^ 0x0F =

5. 1 << 7 =

6. 256 >> 4 =

7. 5 << 2 =

8. (uint8_t)~0xA5 = 0x (two hex digits)

9. 0x35 & 0x01 =

10. 0x12 | 0x01 = 0x (two hex digits)

11. 0xFF >> 4 (unsigned) = 0x (hex, one digit)

12. The mask for bit 6 is 1u <<

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.

TaskYour answerResult
Force bit 3 of a register to 1AND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Force bit 3 of a register to 0AND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Flip bit 3 of a registerAND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Find which bits differ between two valuesAND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Keep only the low 4 bitsAND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Multiply an unsigned value by 16<select id=”op5″ data-ans=”Left shift (<AND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Divide an unsigned value by 4>)” style=”border:2px solid #0ea5e9;border-radius:6px;padding:5px 8px;max-width:210px;”>AND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Invert every bitAND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Test whether bit 5 is setAND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)
Combine two non-overlapping fields into one wordAND (&)OR (|)XOR (^)NOT (~)Left shift (<<)Right shift (>>)

Section 4: True or False

Short statements that show up in bitwise operators interview questions.

1. a & b and a && b always give the same result.

2. XOR with 1 toggles a bit.

3. x << 1 always doubles x for unsigned x.

4. Right-shifting a negative int is implementation-defined in C.

5. ~x for a uint8_t x yields a value with only 8 significant bits.

6. Setting a bit that is already set with |= changes nothing.

7. x & (x - 1) clears the lowest set bit of x.

8. Bit numbering in ARM reference manuals starts at 1 on the left.

Section 5: Code Snippet Challenges

Work these code challenges by hand before opening the answers: the real output is shown, as usual in bitwise operators interview questions.

Challenge 1 — Output?: The four basic operators
#include <stdio.h> #include <stdint.h> int main(void) { uint8_t a = 0x6B, b = 0x35; printf(“%02X %02X %02X %02X\n”, a & b, a | b, a ^ b, (uint8_t)~a); return 0; }

What are the four bytes?

Output:
21 7F 5E 94
a = 0110 1011, b = 0011 0101. AND = 0010 0001 (21), OR = 0111 1111 (7F), XOR = 0101 1110 (5E), NOT a = 1001 0100 (94).
Challenge 2 — Output?: Set, toggle, clear
#include <stdio.h> #include <stdint.h> int main(void) { uint8_t r = 0; r |= (1u << 1) | (1u << 3); r ^= (1u << 3) | (1u << 5); r &= ~(1u << 1); printf(“0x%02X\n”, r); return 0; }

What is r at the end?

Output:
0x20
Start 0. After OR: bits 1 and 3 set (0x0A). XOR with bits 3 and 5: bit 3 clears, bit 5 sets (0x22). Clear bit 1: 0x20.
Challenge 3 — Output?: Shifts, signed and unsigned
#include <stdio.h> int main(void) { unsigned x = 5; printf(“%u %u %u\n”, x << 3, x >> 1, (x << 1) | 1); int y = –20; printf(“%d %d\n”, y >> 2, y / 4); return 0; }

What are the two lines?

Output:
40 2 11
-5 -5
5<<3 = 40, 5>>1 = 2, (5<<1)|1 = 11. For -20, >> 2 is -5 and / 4 is also -5 here, because -20 is divisible by 4; they differ for values such as -7.
Challenge 4 — Find the bug: Precedence of == and &
#include <stdio.h> #include <stdint.h> int main(void) { uint8_t flags = 0x04; if (flags & 0x04 == 0x04) printf(“A set\n”); else printf(“A clear\n”); if ((flags & 0x04) == 0x04) printf(“B set\n”); else printf(“B clear\n”); return 0; }

What is printed and what does GCC say?

Output:
A clear
B set
GCC with -Wall:
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”); return 0; }

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); return 0; }

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 oneach bitwhole value as true/false
6 and 321
Short-circuitNoYes
Q2. How do you set, clear, toggle and test a bit?Basic+
ActionExpression
Set bit nreg |= (1u << n);
Clear bit nreg &= ~(1u << n);
Toggle bit nreg ^= (1u << n);
Test bit nif (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> static unsigned 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)); return 0; }
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> static uint8_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)); return 0; }
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

ScoreMeaningNext step
90–100%ExcellentContinue to Lesson 5.3: practical bit manipulation on registers.
70–89%GoodRedo Examples 2, 4, 5 and 9.
Below 70%ReviseRe-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.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *