14. What is the largest positive value of a 12-bit signed number?
211 − 1 = 2047 (0x7FF).
15. A sensor returns the byte 0xF6 as a signed temperature in °C. What is it?
0xF6 = 246 unsigned; 246 − 256 = −10.
16. Why do processors prefer 2’s complement over sign-magnitude?
One adder handles both interpretations; subtraction becomes addition; no duplicate zero. (C23 mandates it; earlier standards allowed other representations.)
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
All ones.
2. 2’s complement of 0000 1010 (binary, 8 bits):
Invert: 1111 0101, add 1: 1111 0110 (= −10).
3. −128 as an 8-bit pattern:
Only the sign bit is set.
4. 2’s complement of 0x01 (8-bit) is 0x
0x01 → 0xFE + 1 = 0xFF.
5. 0x9C as int8_t is
156 − 256 = −100.
6. The uint8_t value 200 reinterpreted as int8_t is
11. The flag that signals an unsigned carry-out on the Cortex-M4 is the flag (one letter)
C (Carry). V is the signed-overflow flag.
12. Zero-extending the byte 0xF0 to 16 bits gives 0x
New upper bits are zero: 0x00F0 (=240). Sign extension would give 0xFFF0.
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.
Addition
Your answer
Result
int8_t: 50 + 60
OverflowFits
int8_t: 100 + 30
OverflowFits
int8_t: −100 + (−30)
OverflowFits
int8_t: 64 + 64
OverflowFits
int8_t: −64 + (−64)
OverflowFits
int8_t: 127 + (−1)
OverflowFits
uint8_t: 200 + 60
OverflowFits
uint8_t: 100 + 100
OverflowFits
Section 4: True or False
1. The byte 0xFF always means 255.
It is 255 as uint8_t and −1 as int8_t: the type decides.
2. In 2’s complement the most significant bit acts as the sign bit (and has weight −2N−1).
For 8 bits, the MSB weight is −128.
3. int8_t can represent one more negative value than positive value.
−128 to +127.
4. Unsigned integer overflow is undefined behaviour in C.
Unsigned arithmetic is defined modulo 2N. Signed overflow is the undefined one.
5. The 1’s complement scheme has two representations of zero.
0000 0000 (+0) and 1111 1111 (−0).
6. Negating INT8_MIN (−128) gives +128 in an int8_t.
+128 does not fit; the result stays −128 (and in C, abs(INT_MIN) is undefined).
7. Sign extension copies the sign bit into the new upper bits.
That preserves the numeric value when widening a signed type.
8. for (uint8_t i = 5; i >= 0; i--) terminates normally.
An unsigned counter is always ≥ 0, so the loop never ends.
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);
return0;
}
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);
return0;
}
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));
return0;
}
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));
return0;
}
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);
return0;
}
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);
return0;
}
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);
return0;
}
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+
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 complement
2’s complement
Negation
Invert bits
Invert bits and add 1
Zeros
+0 (0000) and −0 (1111)
One zero
8-bit range
−127 to +127
−128 to +127
Addition
Needs end-around carry
Plain binary adder
Today
Internet checksum
All 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));
return0;
}
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);
return0;
}
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);
return0;
}
Output on this PC (an ARM Cortex-M build with arm-none-eabi-gcc prints “unsigned” 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.
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 */staticint 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));
return0;
}
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.
Score
Meaning
Next step
90–100%
Excellent
Start Module 3: Data Types in C.
70–89%
Good
Redo Examples 5–12 on the examples page, then continue.
Below 70%
Revise
Re-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.
2 Comments