Activity 2.3.4 — Two's Complement & Signed Numbers¶
Learning Objectives¶
By the end of this lesson, students will be able to:
- Explain why digital systems need signed number representations and describe the limitations of sign-magnitude representation.
- Convert numbers to and from two's complement form for both positive and negative values.
- Determine the range of values that can be represented with a given number of bits in two's complement.
- Perform arithmetic operations with signed numbers in two's complement and detect overflow conditions.
Vocabulary¶
Vocabulary (click to expand)
| Term | Definition |
|---|---|
| Signed Number | A number that can represent both positive and negative values in a binary system. |
| Sign-Magnitude | A representation where the MSB indicates sign (0 = positive, 1 = negative) and remaining bits represent magnitude. |
| Two's Complement | The most common method for representing signed numbers in binary; positive numbers are unchanged, negative numbers are found by inverting bits and adding 1. |
| One's Complement | The result of inverting all bits in a binary number. |
| MSB (Most Significant Bit) | The leftmost bit in a binary number; in signed representation, this is the sign bit. |
| Overflow | An error condition that occurs when the result of an arithmetic operation exceeds the range that can be represented. |
Part 1: The Need for Signed Numbers¶
In digital electronics, we often need to represent negative numbers. Consider a temperature sensor that can read from -20°C to +50°C. We need a way to represent both positive and negative values in binary.
Sign-Magnitude Representation¶
One early approach was sign-magnitude:
- MSB (Most Significant Bit) = Sign bit (0 = positive, 1 = negative)
- Remaining bits = Magnitude (actual value)
Example (using 4 bits): - 0101 = +5 (sign bit 0, magnitude 5) - 1101 = -5 (sign bit 1, magnitude 5)
The Problem with Sign-Magnitude¶
Sign-magnitude has a critical flaw: it has two representations for zero
- 0000 = +0
- 1000 = -0
This creates confusion and wastes a valid pattern. With 4 bits, we can represent only 7 unique values (-7 to +7) instead of 8.
Key insight: Two's complement solves the "negative zero" problem by having only one zero representation, giving us a wider range of negative values.
Part 2: Two's Complement Representation¶
Two's complement is the standard method for representing signed numbers in all modern computers and digital systems.
Key Properties¶
- Positive numbers are represented the same way as unsigned numbers.
- Negative numbers are represented using the two's complement formula.
- Only one zero exists in two's complement.
The Two's Complement Formula¶
To find the two's complement (negative representation) of a number:
Step 1: Write the positive number in binary
Step 2: Invert all bits (one's complement)
Step 3: Add 1 to the result
Worked Examples: 4-Bit Numbers¶
| Value | Binary | One's Complement | +1 (Two's Complement) |
|---|---|---|---|
| +5 | 0101 | 1010 | 1011 (-5) |
| +1 | 0001 | 1110 | 1111 (-1) |
| +13 | 1101 | 0010 | 0011 (-13) |
The Shortcut Method¶
For negative numbers, you can also find the two's complement by:
- Starting from the right (LSB)
- Copy all bits to the right of the first 1
- Invert all bits to the left of that first 1
Example: Find -5 in 4-bit two's complement
- Start with +5: 0101
- Find first 1 from right: 0101
- Copy right: 1
- Invert left: 101
- Result: 1011
Key insight: The two's complement of a number is the binary representation of its negative. The two's complement of a negative number gives back the original positive number.
Part 3: The Two's Complement Range¶
Range Calculation¶
For an n-bit two's complement number:
- Maximum positive value: 2^(n-1) - 1
- Minimum negative value: -2^(n-1)
| Bits | Range | Maximum | Minimum |
|---|---|---|---|
| 4 | -8 to +7 | +7 | -8 |
| 8 | -128 to +127 | +127 | -128 |
| 16 | -32768 to +32767 | +32767 | -32768 |
| 32 | -2,147,483,648 to +2,147,483,647 | +2,147,483,647 | -2,147,483,648 |
Pattern to Remember¶
- The most negative value is always a 1 followed by all zeros (e.g., 1000...0)
- The most positive value is always a 0 followed by all ones (e.g., 0111...1)
Why This Range?¶
With n bits, we have 2^n possible patterns. In two's complement: - Half (2^(n-1)) represent positive numbers and zero - Half (2^(n-1)) represent negative numbers - The extra negative value (-2^(n-1)) exists because there's only one zero
Part 4: Adding Signed Numbers¶
Two's complement makes addition straightforward. You simply add the binary numbers normally and ignore any carry out of the MSB.
Rules for Two's Complement Addition¶
- Add the numbers as unsigned binary
- Discard any carry out of the MSB
- The result is in two's complement
Worked Examples (4-bit)¶
Example 1: 3 + 2
Result: 5. No overflow. Correct!
Example 2: 5 + (-3)
Result: 2. Correct!
Example 3: 6 + 5
Result: -5 (incorrect!). This is overflow.
Key insight: When adding positive numbers, a negative result indicates overflow. When adding negative numbers, a positive result indicates overflow.
Part 5: Overflow Detection¶
Overflow occurs when the result exceeds the representable range.
The Overflow Rule¶
Overflow occurs when the carry INTO the sign bit is different from the carry OUT OF the sign bit.
Checking Overflow¶
c4 c3 c2 c1
^ ^ ^ ^
0110 (6)
+ 0101 (5)
1011 (-5)
c4 = carry out from bit 3
c3 = carry into sign bit (MSB)
In this example: - Carry into sign bit (c3) = 1 - Carry out of sign bit (c4) = 0 - 1 ≠ 0, so overflow!
Quick Overflow Test¶
| Operation | Expected Sign | Result Sign | Overflow? |
|---|---|---|---|
| + + | Positive | Negative | YES |
| + + | Positive | Positive | NO |
| - - | Negative | Positive | YES |
| - - | Negative | Negative | NO |
| + - | Either | Either | NO |
| - + | Either | Either | NO |
Key insight: Overflow can only occur when adding two numbers of the SAME sign. Adding numbers of opposite signs can never cause overflow.
Practice Problem — Two's Complement Conversion¶
Problem 1: Convert +5 to 4-bit two's complement.
Problem 2: Convert -5 to 4-bit two's complement.
Problem 3: Convert -13 to 8-bit two's complement.
Show Solution
Problem 1: +5 in 4 bits = 0101
Problem 2: 1. Start with +5: 0101 2. Invert: 1010 3. Add 1: 1011 Result: 1011 (-5 in two's complement)
Problem 3: 1. +13 in 8 bits = 00001101 2. Invert: 11110010 3. Add 1: 11110011 Result: 11110011 (-13 in 8-bit two's complement)
Practice Problem — Range Determination¶
Problem: What is the range of values for: a) 5-bit two's complement? b) 10-bit two's complement?
Show Solution
a) 5-bit: -2^(5-1) to +2^(5-1)-1 = -16 to +15
b) 10-bit: -2^(10-1) to +2^(10-1)-1 = -512 to +511
Practice Problem — Two's Complement Arithmetic¶
Problem 1: Calculate 4 + (-2) using 4-bit two's complement. Is there overflow?
Problem 2: Calculate (-6) + (-3) using 4-bit two's complement. Is there overflow?
Show Solution
Problem 1:
Actually: - +2 = 0010 - Two's complement of 2: 1101 + 1 = 1110 (-2)
Carry into sign bit (c3) = 1, Carry out (c4) = 1. 1 = 1, no overflow. Result: +2. Correct!
Problem 2: - -6 = 1010 (two's complement of 6: 0110 -> 1001+1=1010) - -3 = 1101 (two's complement of 3: 0011 -> 1100+1=1101)
Carry into sign bit = 0, Carry out = 1. 0 ≠ 1, so OVERFLOW!
The actual sum should be -9, but -9 cannot be represented in 4-bit two's complement (range -8 to +7).
Practice Problem — Binary Subtraction as Addition¶
Problem: Calculate 5 - 3 using two's complement (compute 5 + (-3)).
Show Solution
- 5 = 0101
- -3: Start with 3 = 0011, invert = 1100, add 1 = 1101
- Add: 0101 + 1101 = 0010 (with carry 1 out)
- Result: 0010 = 2
The carry out (1) and carry into sign bit (1) are equal, so no overflow. Result is correct: 5 - 3 = 2.
Summary¶
- Two's complement is the standard method for representing signed numbers in digital systems.
- To find the two's complement: invert all bits and add 1.
- The range for n-bit two's complement is -2^(n-1) to +2^(n-1)-1.
- Two's complement has only one zero, giving it one more negative value than positive.
- Overflow occurs when adding two numbers of the same sign produces a result with the opposite sign.
- Overflow detection: carry INTO sign bit ≠ carry OUT OF sign bit.
Key Reminders¶
- The MSB in two's complement indicates the sign (0 = positive, 1 = negative).
- The two's complement of a number always adds to 2^n (for n bits) to equal zero.
- Always check for overflow when adding two numbers with the same sign.
- Two's complement subtraction is performed by adding the two's complement of the subtrahend.
Custom activity — adapted from PLTW Digital Electronics