What is the difference between signed and unsigned integers on an 8-bit MCU?
Signed integers can represent both positive and negative numbers, while unsigned integers represent only non-negative numbers (zero and positive). On an 8-bit MCU, signed integers typically use one bit for the sign, reducing the maximum positive value.
Click to reveal answer
beginner
What is the range of an 8-bit signed integer?
An 8-bit signed integer ranges from -128 to 127. This is because one bit is used for the sign, and the remaining 7 bits represent the value.
Click to reveal answer
beginner
What is the range of an 8-bit unsigned integer?
An 8-bit unsigned integer ranges from 0 to 255. All 8 bits are used to represent the value, so no bits are reserved for sign.
Click to reveal answer
intermediate
What happens when you assign a negative value to an unsigned 8-bit variable on an MCU?
The negative value wraps around due to underflow, resulting in a large positive number. For example, assigning -1 to an unsigned 8-bit variable results in 255.
Click to reveal answer
intermediate
How does overflow behave differently for signed and unsigned 8-bit integers?
For unsigned integers, overflow wraps around from the maximum value back to zero. For signed integers, overflow causes undefined behavior or wraps around in two's complement form, which can lead to negative values.
Click to reveal answer
What is the maximum value of an 8-bit unsigned integer?
A128
B127
C255
D256
✗ Incorrect
An 8-bit unsigned integer uses all 8 bits for value, so max is 2^8 - 1 = 255.
What is the range of an 8-bit signed integer?
A-128 to 127
B-256 to 255
C-127 to 128
D0 to 255
✗ Incorrect
Signed 8-bit integers use one bit for sign, so range is -128 to 127.
What happens if you assign -1 to an unsigned 8-bit variable?
AIt stores 255
BIt stores -1
CIt causes an error
DIt stores 0
✗ Incorrect
Unsigned variables wrap around, so -1 becomes 255.
Which of these is true about overflow in unsigned 8-bit integers?
AIt stays at max value
BIt causes program crash
CIt becomes negative
DIt wraps around to zero
✗ Incorrect
Unsigned overflow wraps around to zero due to modulo arithmetic.
Why is one bit reserved in signed 8-bit integers?
ATo store parity
BTo store the sign (positive or negative)
CTo increase range
DTo store overflow info
✗ Incorrect
One bit is used to indicate if the number is positive or negative.
Explain the difference between signed and unsigned integers on an 8-bit MCU and how their ranges differ.
Think about how many values can be represented with 8 bits and how sign affects that.
You got /4 concepts.
Describe what happens when you assign a negative number to an unsigned 8-bit variable on an MCU.
Consider how unsigned variables handle values outside their range.
You got /4 concepts.
Practice
(1/5)
1. What is the range of values for an 8-bit signed char on a typical MCU?
easy
A. -128 to 127
B. 0 to 255
C. -256 to 255
D. -127 to 128
Solution
Step 1: Understand 8-bit signed char range
An 8-bit signed char uses one bit for sign and 7 bits for value, so it can represent values from -128 to 127.
Step 2: Compare with options
Only -128 to 127 matches the correct range for signed 8-bit char.
Final Answer:
-128 to 127 -> Option A
Quick Check:
Signed 8-bit char range = -128 to 127 [OK]
Hint: Signed 8-bit char always includes negative half [OK]
Common Mistakes:
Confusing signed and unsigned ranges
Assuming 8-bit signed char goes to 255
Mixing up sign bit usage
2. Which of the following declarations correctly defines an unsigned 8-bit variable in embedded C?
easy
A. signed char var;
B. int8_t var;
C. char unsigned var;
D. unsigned char var;
Solution
Step 1: Identify unsigned 8-bit type syntax
In embedded C, unsigned char is the standard way to declare an unsigned 8-bit variable.
Step 2: Check other options
signed char var; is signed, char unsigned var; is invalid syntax, int8_t var; is signed 8-bit integer.
Final Answer:
unsigned char var; -> Option D
Quick Check:
Unsigned 8-bit variable = unsigned char [OK]
Hint: Use 'unsigned char' for unsigned 8-bit variables [OK]
Common Mistakes:
Using 'char unsigned' which is invalid syntax
Assuming 'int8_t' is unsigned
Confusing signed and unsigned keywords
3. Consider this code snippet on an 8-bit MCU:
unsigned char a = 250;
signed char b = (signed char)a;
printf("%d", b);
What is the output?
medium
A. 250
B. -6
C. 6
D. -250
Solution
Step 1: Understand casting from unsigned to signed
Variable a holds 250 (unsigned). Casting to signed char wraps around because signed char max is 127.
Step 2: Calculate signed char value
250 - 256 = -6 (wrap-around in 8-bit signed range)
Final Answer:
-6 -> Option B
Quick Check:
Unsigned 250 cast to signed 8-bit = -6 [OK]
Hint: Subtract 256 if unsigned > 127 when casting to signed [OK]
Common Mistakes:
Assuming cast keeps original unsigned value
Confusing sign extension with wrap-around
Ignoring 8-bit overflow behavior
4. This code runs on an 8-bit MCU:
unsigned char x = 200;
signed char y = -100;
unsigned char z = x + y;
printf("%u", z);
What is the output?
medium
A. 156
B. 44
C. 100
D. 300
Solution
Step 1: Understand integer promotion in addition
In expressions, unsigned char and signed char are promoted to int before addition.
Step 2: Calculate the sum
200 + (-100) = 100 as int, but when assigned to unsigned char (8-bit), it wraps modulo 256. Since 100 fits in 8 bits, the value is 100.
Step 3: Print the result
printf with %u prints unsigned int, so output is 100.
Final Answer:
100 -> Option C
Quick Check:
Signed promoted to int before addition, result 100 [OK]
Hint: Signed promoted to int before addition, watch final cast [OK]
Common Mistakes:
Adding raw unsigned and signed bytes without promotion
Assuming overflow without checking promotion
Misinterpreting printf format specifiers
5. On an 8-bit MCU, you want to store the result of subtracting 150 from 100 using unsigned 8-bit variables:
unsigned char a = 100;
unsigned char b = 150;
unsigned char c = a - b;
printf("%u", c);
What will be printed and why?
hard
A. 206 because unsigned subtraction wraps around modulo 256