Twos Complement Math
Introduction: Manipulating Binary Data
In our previous lessons, we learned how computers represent negative numbers using Two's Complement and how to build circuits that add bits together. However, in a real computer, data is constantly moving between different parts of the system. Sometimes, you need to move a 4-bit number into an 8-bit slot, and other times, you need to perform subtraction using the same hardware that handles addition.
In this lesson, we will explore the "tricks" that make Two's Complement so powerful. You will learn how to change the size of a binary number without changing its value and how to turn a subtraction problem into a simple addition problem. These techniques allow us to keep our circuit designs small and efficient.
Sign Extension: Growing a Number
Imagine you have a 4-bit number, but the next component in your circuit requires an 8-bit input. To make the number fit, you have to add four extra bits. In standard positive binary, you would just add zeros to the left. But in Two's Complement, the leftmost bit is the sign bit. If we aren't careful, adding zeros could turn a negative number into a positive one.
To grow a number correctly, we use a rule called Sign Extension. You simply look at the sign bit of the original number and "copy-and-paste" it into all the new slots to the left.
| Value | 4-bit Binary | 8-bit Sign Extended |
|---|---|---|
0011 | 0000 0011 | |
1101 | 1111 1101 |
What happened here?
In the first row, the sign bit was 0 (positive). We filled the new spaces with 0s, and the value stayed . In the second row, the sign bit was 1 (negative). By filling the new spaces with 1s, the value stays . If we had filled it with zeros instead, the number would have changed from a small negative number to a very large positive number!
Truncation: Shrinking a Number
Sometimes, we need to do the opposite: fit a large number into a smaller space. This is called Truncation. To truncate a number, we simply discard the bits on the left.
However, this is risky. If the bits we throw away are different from the new sign bit, we lose data or change the sign of the number. This is called an overflow error.
- Safe Truncation: Converting the
8-bitnumber0000 0101() to4 bits. We drop the fourzeroson the left and get0101(). The value is the same. - Unsafe Truncation: Converting the
8-bitnumber0001 0101() to4 bits. We drop thebitson the left and get0101(). The value changed because the original number was too big to fit into4 bits.
When building circuits, you must ensure that the "container" (the number of bits) is always large enough to hold the values you are calculating.
Addition In Two's Complement
One of the best features of Two's Complement is that the circuit doesn't need to know whether a number is positive or negative. A standard binary adder — like the ones we built in Unit 3 — will give you the correct answer regardless.
When adding Two's Complement numbers, there is one important rule: Discard the final carry-out bit. If your circuit is designed for 4-bit numbers, your answer must stay 4 bits.
Example: 5 + (-2)
- in
4-bitbinary:0101 - in
4-bitbinary:1110 - Add them together:
0101 + 1110 = 10011
What happened here?
The result is 10011, which is 5 bits long. Since we are using 4-bit logic, we ignore the leftmost 1. This leaves us with 0011, which is in decimal. As you can see, . The math works perfectly without any special negative logic in the adder.
Subtraction: The "Flip And Add 1" Trick
In digital logic, we rarely build a separate circuit for subtraction. Instead, we use a trick to turn subtraction into addition. To calculate , we actually calculate .
To find the negative version of any number in Two's Complement, we follow two steps:
- Flip: Use a
NOT gateto flip all thebits(0becomes1,1becomes0). - Add 1: Add
1to that result.
Example: 7 - 3
- A = 7 (
0111) - B = 3 (
0011)
First, we turn into :
- Flip
00111100 - Add
11101(This is )
Now, we add them together:
0111(7) +1101() =10100- Discard the extra
carry biton the left0100
Result: 0100 is 4 in decimal. By flipping the bits and adding 1, we successfully performed subtraction using only an addition circuit!
Summary And Practice Prep
In this lesson, we learned how to manage binary data more effectively:
- Sign Extension: To make a number larger, copy the leftmost
bitinto the new spaces. - Truncation: To make a number smaller, remove
bitsfrom the left, but be careful not to change the value. - Addition:
Two's Complementadditionworks just like normaladdition, as long as you discard the finalcarry-out bit. - Subtraction: To subtract, flip the
bitsof the second number, add1, and then add it to the first number.
Now, it is time to put these tricks to work! In the following practice exercises, you will go into the CodeSignal IDE to build circuits that resize numbers and perform subtraction. You will see firsthand how these simple rules allow a computer to handle complex math with ease.
