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.
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.
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() to . We drop the on the left and get (). The value changed because the original number was too big to fit into .
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 :
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:
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.
