0
0
Embedded Cprogramming~10 mins

Signed vs unsigned behavior on 8-bit MCU in Embedded C - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Signed vs unsigned behavior on 8-bit MCU
Start with 8-bit variable
Assign value (signed or unsigned)
Store in 8 bits
Interpret bits as signed or unsigned
Perform arithmetic or comparison
Result depends on signedness
Use result in program
This flow shows how an 8-bit variable stores values and how interpreting bits as signed or unsigned changes the result.
Execution Sample
Embedded C
unsigned char u = 250;
signed char s = 250;

printf("u = %u\n", u);
printf("s = %d\n", s);
This code assigns 250 to both unsigned and signed 8-bit variables and prints their values.
Execution Table
StepVariableValue AssignedStored Bits (8-bit)Interpreted ValuePrint Output
1u (unsigned char)25011111010250u = 250
2s (signed char)25011111010-6s = -6
3End---Execution ends
💡 Execution stops after printing both values.
Variable Tracker
VariableStartAfter AssignmentFinal
u (unsigned char)undefined250250
s (signed char)undefined250 (stored as 11111010)-6 (interpreted signed)
Key Moments - 3 Insights
Why does assigning 250 to a signed 8-bit variable show -6 when printed?
Because 8-bit signed variables use two's complement. The bits 11111010 represent -6 in signed form, as shown in execution_table row 2.
Why does the unsigned variable print 250 correctly?
Unsigned variables interpret bits as positive numbers only, so 11111010 is 250 decimal, as seen in execution_table row 1.
What happens if arithmetic is done on signed vs unsigned variables?
Arithmetic results depend on interpretation: signed variables can represent negative numbers, unsigned cannot, affecting overflow and comparisons.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the interpreted value of the signed char variable after assignment?
A250
B-6
C6
D-250
💡 Hint
Check execution_table row 2 under 'Interpreted Value' column.
At which step does the unsigned variable get assigned its value?
AStep 2
BStep 3
CStep 1
DNo assignment
💡 Hint
Look at execution_table row 1 under 'Variable' and 'Value Assigned'.
If the signed char variable was assigned 130 instead of 250, what would be its interpreted value?
A-126
B130
C-130
D126
💡 Hint
Signed 8-bit range is -128 to 127; 130 stored as bits wraps to negative number similar to 250 case.
Concept Snapshot
Signed vs unsigned 8-bit variables store the same bits but interpret them differently.
Unsigned chars hold 0 to 255.
Signed chars hold -128 to 127 using two's complement.
Assigning values >127 to signed chars wraps to negative numbers.
Printing must match variable type to show correct value.
Full Transcript
This lesson shows how 8-bit microcontrollers store values in variables. When you assign a number like 250 to an unsigned 8-bit variable, it stores the bits 11111010 and prints 250. But if you assign 250 to a signed 8-bit variable, it stores the same bits but interprets them as -6 because signed 8-bit uses two's complement. This difference affects how arithmetic and comparisons work. Always remember to use the correct type and print format to see the expected values.