Signed and Unsigned Interview Questions for Embedded C: 2’s Complement Quiz-Free Embedded C Tutorial Online

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

MODULE 2 · LESSON 2.3 · PRACTICE

Signed and Unsigned Interview Questions for Embedded C: 2’s Complement Quiz

16 multiple choice questions, 12 compute-and-fill exercises, an overflow identification table, 8 true/false statements, 6 code snippet challenges and 12 detailed interview questions with verified answers.

62
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 — Signed, Unsigned and 2’s Complement

Click your answer. Explanations always appear, correct or not.

1. What is the range of an int8_t?

2. What is the 8-bit 2’s complement representation of −5?

3. The byte 0xFF stored in an int8_t has the value:

4. How many representations of zero does 2’s complement have?

5. What is −1 as a 16-bit signed integer, in hex?

6. What is the range of int16_t?

7. What is 0x80 when interpreted as int8_t?

8. In hardware, 127 + 1 in an 8-bit signed register gives:

9. What is the 1’s complement of 0x0F (8-bit)?

10. int8_t value 0xFB is converted to int16_t. The result in hex is:

11. Which Cortex-M4 APSR flag indicates signed overflow?

12. What does printf("%d", -1 < 1u) print on a 32-bit-int platform?

13. uint8_t x = 3; x = x - 5; What is x?

14. What is the largest positive value of a 12-bit signed number?

15. A sensor returns the byte 0xF6 as a signed temperature in °C. What is it?

16. Why do processors prefer 2’s complement over sign-magnitude?

Section 2: Compute 2’s Complement and Fill in the Blanks

Spaces and capitalisation are ignored. You may type the minus sign as -.

1. −1 in 8-bit 2’s complement is 0x

2. 2’s complement of 0000 1010 (binary, 8 bits):

3. −128 as an 8-bit pattern:

4. 2’s complement of 0x01 (8-bit) is 0x

5. 0x9C as int8_t is

6. The uint8_t value 200 reinterpreted as int8_t is

7. −2 as a 16-bit pattern in hex: 0x

8. 2’s complement of 0x40 (8-bit) is 0x

9. Largest value of int32_t (decimal):

10. Smallest value of int16_t (decimal):

11. The flag that signals an unsigned carry-out on the Cortex-M4 is the flag (one letter)

12. Zero-extending the byte 0xF0 to 16 bits gives 0x

Section 3: Identify the Overflow

For each addition, decide whether the result overflows the type or fits. Hint: compare the true sum with the range of the type.

AdditionYour answerResult
int8_t: 50 + 60OverflowFits
int8_t: 100 + 30OverflowFits
int8_t: −100 + (−30)OverflowFits
int8_t: 64 + 64OverflowFits
int8_t: −64 + (−64)OverflowFits
int8_t: 127 + (−1)OverflowFits
uint8_t: 200 + 60OverflowFits
uint8_t: 100 + 100OverflowFits

Section 4: True or False

1. The byte 0xFF always means 255.

2. In 2’s complement the most significant bit acts as the sign bit (and has weight −2N−1).

3. int8_t can represent one more negative value than positive value.

4. Unsigned integer overflow is undefined behaviour in C.

5. The 1’s complement scheme has two representations of zero.

6. Negating INT8_MIN (−128) gives +128 in an int8_t.

7. Sign extension copies the sign bit into the new upper bits.

8. for (uint8_t i = 5; i >= 0; i--) terminates normally.

Section 5: Code Snippet Challenges

Predict first. Outputs come from compiling and running each program (GCC on a little-endian PC with 32-bit int, like the Cortex-M4).

Challenge 1 — Output?: Wrapping a signed byte
#include <stdio.h> #include <stdint.h> int main(void) { int8_t x = 127; int y = x + 1; /* computed in int */ x = (int8_t)y; /* stored back into 8 bits */ printf(“%d %d\n”, y, x); return 0; }

What does this print?

Output: 128 -128
The sum 128 exists in int; storing it in int8_t keeps the low 8 bits 1000 0000, which is −128. (The out-of-range conversion is implementation-defined; GCC wraps.)
Challenge 2 — Find the bug: The cold-weather bug
#include <stdio.h> #include <stdint.h> int main(void) { uint8_t temperature = –5; /* sensor reports minus five degrees */ if (temperature < 0) printf(“below zero\n”); else printf(“temperature = %d\n”, temperature); return 0; }

What does this print for −5 °C, and what is the fix?

Output: temperature = 251
uint8_t cannot hold −5: it becomes 251 and temperature < 0 is always false. Fix: use int8_t temperature (or int16_t for tenths of a degree).
Challenge 3 — Complete: Sign-extend 12 bits
#include <stdio.h> #include <stdint.h> /* Sign-extend a 12-bit two’s complement value */ int32_t sext12(uint16_t raw) { raw &= 0x0FFFu; return /* ??? */; } int main(void) { printf(“%d %d %d\n”, (int)sext12(0x7FF), (int)sext12(0x800), (int)sext12(0xFFF)); return 0; }

Replace /* ??? */ so the program prints 2047 -2048 -1.

Use (int32_t)(raw ^ 0x800u) - 0x800: XOR the sign bit, then subtract the bias.
solution
#include <stdio.h> #include <stdint.h> /* Sign-extend a 12-bit two’s complement value */ int32_t sext12(uint16_t raw) { raw &= 0x0FFFu; return (int32_t)(raw ^ 0x800u) – 0x800; } int main(void) { printf(“%d %d %d\n”, (int)sext12(0x7FF), (int)sext12(0x800), (int)sext12(0xFFF)); return 0; }
Verified output: 2047 -2048 -1
Challenge 4 — Output?: Signed meets unsigned
#include <stdio.h> int main(void) { unsigned a = 3; int b = –4; printf(“%d\n”, a > b); printf(“%d\n”, (int)a > b); return 0; }

What are the two lines of output? Explain the difference.

Output:
0
1
In the first comparison b (−4) is converted to unsigned 4294967292, so 3 > 4294967292 is false. After casting a to int the comparison is done with signed values: 3 > −4 is true.
Challenge 5 — Find the bug: Negating the minimum
#include <stdio.h> #include <stdint.h> int main(void) { int8_t m = INT8_MIN; int8_t n = (int8_t)(-m); /* try to negate the most negative value */ printf(“m = %d, -m stored in int8_t = %d\n”, m, n); return 0; }

What is printed and why is it a trap in abs() style code?

Output: m = -128, -m stored in int8_t = -128
+128 is not representable in 8 bits, so negation of −128 yields −128 again. Any abs() or sign-flipping code must handle the most negative value separately.
Challenge 6 — Output?: Right shift of a negative value
#include <stdio.h> #include <stdint.h> int main(void) { int8_t v = –16; printf(“%d %d\n”, v >> 2, (uint8_t)v >> 2); return 0; }

What are the two numbers? Why do they differ?

Output: -4 60
Signed: −16 >> 2 = −4 (an arithmetic shift copies the sign bit; implementation-defined, GCC does this). Unsigned: 0xF0 = 240, 240 >> 2 = 60 (a logical shift inserts zeros).

Section 6: Signed and Unsigned Interview Questions for Embedded C (with Detailed Answers)

These signed and unsigned interview questions for embedded C come up in nearly every firmware interview. Click a question to see a model answer, then try to explain it aloud in your own words.

Q1. What is 2’s complement and why do processors use it?Basic+

It is the standard way to represent signed integers: a negative number −x is stored as 2N − x, which you obtain by inverting all bits of x and adding 1. The MSB acts as a sign bit with weight −2N−1.

  • One representation of zero (no −0).
  • The same adder circuit handles signed and unsigned addition; the result bits are identical, only the interpretation differs.
  • Subtraction is addition of the negated operand: a − b = a + ~b + 1.
  • Range is asymmetric: −2N−1 to 2N−1 − 1.
Q2. Represent −45 as an 8-bit signed number. Show the steps.Basic+

+45 = 0010 1101. Invert: 1101 0010. Add 1: 1101 0011 = 0xD3. Check: 256 − 45 = 211 = 0xD3, and −128 + 64 + 16 + 2 + 1 = −45.

Q3. Why is the range of a signed type asymmetric? What is the consequence?Basic+

Zero consumes one of the 2N−1 non-negative patterns, so the positive side ends at 2N−1 − 1 while the negative side reaches −2N−1. The consequence is that the most negative value has no positive counterpart: -INT_MIN and abs(INT_MIN) overflow. Safe code treats it as a special case or works in a wider type.

Q4. What is the difference between 1’s complement and 2’s complement?Basic+
1’s complement2’s complement
NegationInvert bitsInvert bits and add 1
Zeros+0 (0000) and −0 (1111)One zero
8-bit range−127 to +127−128 to +127
AdditionNeeds end-around carryPlain binary adder
TodayInternet checksumAll integer CPUs
Q5. How do you detect overflow when adding two signed integers? And two unsigned integers?Intermediate+

Unsigned: overflow if the carry-out of the MSB is 1, equivalently sum < a after the wrapped addition. Signed: overflow if both operands have the same sign and the result has a different sign; in hardware V = carry into the MSB XOR carry out of the MSB.

In C avoid testing after a signed overflow (undefined behaviour); compute in a wider type first or use __builtin_add_overflow.

saturate.c — a saturating signed add built on the widening technique
#include <stdio.h> #include <stdint.h> int8_t sat_add_i8(int8_t a, int8_t b) { int sum = (int)a + (int)b; if (sum > INT8_MAX) return INT8_MAX; if (sum < INT8_MIN) return INT8_MIN; return (int8_t)sum; } int main(void) { printf(“100 + 100 = %d (saturated)\n”, sat_add_i8(100, 100)); printf(“-100 + -100 = %d (saturated)\n”, sat_add_i8(-100, –100)); printf(“50 + (-20) = %d\n”, sat_add_i8(50, –20)); return 0; }
Output (verified)
100 + 100 = 127 (saturated) -100 + -100 = -128 (saturated) 50 + (-20) = 30
Q6. Is signed integer overflow undefined behaviour in C? What can go wrong?Intermediate+

Yes. The standard allows the compiler to assume it never happens. Consequences: a check such as if (x + 1 < x) may be optimised away entirely, loops with signed counters may be transformed assuming no wrap, and results can differ between -O0 and -O2. Unsigned overflow, in contrast, is defined as modular arithmetic.

ub_example.c (do not rely on this)
int is_max(int x) { return x + 1 < x; /* GCC at -O2 may compile this to “return 0” */ }

Mitigations: use unsigned types for wrapping arithmetic, check before adding, compile with -fsanitize=signed-integer-overflow during testing, or -fwrapv if you intentionally rely on wrapping.

Q7. What does -1 < 1u evaluate to in C, and why?Intermediate+

It is false (0). In a comparison between int and unsigned int the usual arithmetic conversions convert the signed operand to unsigned, so −1 becomes 4294967295 (0xFFFFFFFF).

signed_vs_unsigned.c
#include <stdio.h> #include <stdint.h> int main(void) { int s = –1; unsigned u = 1u; printf(“-1 < 1u : %d\n”, s < u); /* s converted to unsigned */ printf(“(unsigned)-1 < 1u : %d\n”, (unsigned)s < u); printf(“(unsigned)-1 = %u\n”, (unsigned)s); return 0; }
Output (verified)
-1 < 1u : 0 (unsigned)-1 < 1u : 0 (unsigned)-1 = 4294967295

The compiler warns with -Wsign-compare. Fix by casting both sides to the same signed type, or by checking for negative values first.

Q8. Explain sign extension. Is plain char signed or unsigned?Intermediate+

Sign extension widens a signed value by copying the sign bit into the new upper bits so its numeric value is preserved (int8_t 0xFB → int16_t 0xFFFB). Unsigned values are zero-extended.

The signedness of plain char is implementation-defined: signed on x86 GCC, unsigned on ARM (the ARM EABI default). A program that stores a byte such as 0x80 in a char therefore behaves differently on a PC and on the STM32. Use uint8_t for raw bytes.

char_signedness.c
#include <stdio.h> int main(void) { char c = (char)0x80; int i = c; /* promotion: sign- or zero-extension? */ printf(“char is %s on this platform, (int)c = %d\n”, (i < 0) ? “signed” : “unsigned”, i); return 0; }
Output on this PC (an ARM Cortex-M build with arm-none-eabi-gcc prints &ldquo;unsigned&rdquo; and 128)
char is signed on this platform, (int)c = -128
Q9. How can you get the sign of an integer, or its absolute value, without a branch?Advanced+

The sign is the MSB: shift right by 31 as unsigned. For absolute value use a mask made by an arithmetic right shift (all ones when negative): (x ^ mask) - mask. This is the same “invert and add one” negation, applied conditionally.

sign_abs.c
#include <stdio.h> #include <stdint.h> static int sign_bit(int32_t x) { return (int)((uint32_t)x >> 31); } /* branch-free absolute value (INT32_MIN stays INT32_MIN: no positive counterpart) */ static int32_t abs_i32(int32_t x) { uint32_t mask = (uint32_t)(x >> 31); /* all ones if negative, else 0 (arithmetic shift) */ return (int32_t)(((uint32_t)x ^ mask) – mask); } int main(void) { printf(“sign_bit(5)=%d sign_bit(-5)=%d sign_bit(0)=%d\n”, sign_bit(5), sign_bit(-5), sign_bit(0)); printf(“abs(-123)=%d abs(77)=%d\n”, (int)abs_i32(-123), (int)abs_i32(77)); printf(“abs(INT32_MIN)=%d (still negative!)\n”, (int)abs_i32(INT32_MIN)); return 0; }
Output (verified)
sign_bit(5)=0 sign_bit(-5)=1 sign_bit(0)=0 abs(-123)=123 abs(77)=77 abs(INT32_MIN)=-2147483648 (still negative!)
Caveat: It relies on arithmetic right shift of negative numbers, which is implementation-defined (but universal on GCC/Clang/ARM). abs(INT32_MIN) still fails.
Q10. How do you compare two timestamps from a free-running 32-bit counter that may wrap?Advanced+

Subtract the two values as unsigned (giving the true distance modulo 232), then interpret the difference as signed. If it is positive, a is later than b. This is valid as long as the two timestamps are less than 231 ticks apart (about 24.8 days for a millisecond counter).

tick_compare.c
#include <stdio.h> #include <stdint.h> /* true if tick a is later than tick b, valid while |a – b| < 2^31 ticks */ static int is_after(uint32_t a, uint32_t b) { return (int32_t)(a – b) > 0; } int main(void) { uint32_t before = 0xFFFFFFF0u, after = 0x00000010u; /* counter wrapped in between */ printf(“naive after > before : %d (wrong)\n”, after > before); printf(“is_after(after, before) : %d (correct)\n”, is_after(after, before)); printf(“is_after(before, after) : %d\n”, is_after(before, after)); return 0; }
Output (verified)
naive after > before : 0 (wrong) is_after(after, before) : 1 (correct) is_after(before, after) : 0

This is the idiom used by the Linux kernel’s time_after() macro and by many RTOS tick APIs.

Module 2 Complete: Your Next Step

You have finished Module 2: Number Systems: binary and decimal, hexadecimal, and signed/unsigned representation with 1’s and 2’s complement.

ScoreMeaningNext step
90–100%ExcellentStart Module 3: Data Types in C.
70–89%GoodRedo Examples 5–12 on the examples page, then continue.
Below 70%ReviseRe-read the theory sections on 2’s complement, then retake this quiz tomorrow.
Module 3 preview: Next you will map everything learned here onto real C types: char, int, uint8_t, sizeof and type casting. Signed versus unsigned will appear in every lesson.

Module 2 complete!

Next up is Module 3: standard C data types, fixed-width integer types from stdint.h, sizeof and type casting.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

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

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →