Having a way to represent positive decimal integers in machine code is a good start, but not nearly enough to fulfil our needs. There is so much more information we would like in machine code which is much more complex. Finding a representation will not always be easy! Lucky for us, we have already overcome a lot of challenges — one of them being the representation of negative numbers.
Signed and Unsigned Numbers
Numbers can be signed or unsigned. Unsigned numbers are always positive while signed numbers can be positive or negative.
Consider a 3-bit register. There are eight different combinations of ones and zeros (these are 000, 001, 010, 011, 100, 101, 110 and 111). These eight combinations can be made to represent the range of numbers from 0 to 7 or the numbers from -4 to 3. We say that the range “from 0 to 7” is unsigned and the range “from -4 to 3” is signed.
Sign and Magnitude
How can negative numbers be represented? One method is called sign and magnitude where the leftmost bit is reserved to represent the sign.
1 indicates negative and 0 indicates positive.
Let us take an example with 8 bits:
Notice how the only difference between the representations for +77 and -77 is the first bit, which is used as the sign bit.
Let's consider just 3 bits. These are the possible representations that cover the range from -3 to +3:
Apart from the repeated zero, another problem with sign and magnitude representation is that it is not efficient for computation e.g. performing addition of two numbers etc. In fact, the computer uses another system to represent negative numbers. It is called two’s complement representation.
Two’s Complement
Two’s complement of a number is obtained in two steps:
1. Flip all the bits.
2. Add 1 to it.
In this representation, the negative of a number is its two’s complement.
A Worked Example
Having one byte to write a number, express -87 in two’s complement.
Step 1 Convert 87 to Binary 87 = 0 1 0 1 0 1 1 1
Step 2 Get -87 by applying Two's Complement Flip bits: 1 0 1 0 1 0 0 0 Add one: 1 0 1 0 1 0 0 0 + 1 = 1 0 1 0 1 0 0 1
Therefore the binary value of -87 in two's complement is 1 0 1 0 1 0 0 1.
We have to keep in mind that the left-most bit is negated. We can check our answer for -87 by looking at the weights below:
This represents -128 + 32 + 8 + 1, which equals -87!
Subtraction using Two’s Complement and Addition
Suppose we want to calculate num1 - num2. We follow these steps:
1. First convert num1 and num2 into binary.
2. We then get negative num2 using two’s complement.
3. Finally, add both binary numbers normally but ignore the overflow bit.
The best way to learn this is through examples. So let's have a go!
A Worked Example
Perform 88 – 67 using two’s complement, note that numbers take up one byte.
Step 1 Convert 88 and 67 to Binary 88 = 0 1 0 1 1 0 0 0 67 = 0 1 0 0 0 0 1 1
Step 2 Get -67 in Binary by applying Two's Complement Using two's complement, we flip all the bits and add 1 - 67 = 1 0 1 1 1 1 0 0 + 1 - 67 = 1 0 1 1 1 1 0 1
Step 3 Add both Binary numbers normally We add using the addition method discussed in previous chapters, and in this case, we can ignore the overflow bit. The final answer 0 0 0 1 0 1 0 1, which is 21 in decimal.
Calculating ranges for a register
In this post we have seen 3 types of representations:
Unsigned thus all the numbers are positive.
Sign and Magnitude, signed numbers that range from negative to positive.
Two's Complement, signed numbers that also range from negative to positive.
Depending on which of the above we use, a register will be able to represent different ranges of numbers.
Let's take a 4-bit register as an example. The table here specifies the minimums and maximums based on the chosen representation.
| Min Binary | Min Decimal | Max Binary | Max Decimal |
Unsigned | 0000 | 0 | 1111 | 15 |
Sign and Magnitude | 1111 | -7 | 0111 | 7 |
Two's Complement | 1000 | -8 | 0111 | 7 |
A few points to keep in mind when determining the ranges of representation:
Unsigned The minimum binary number is going to be all 0s, and our maximum is going to be all 1s. This is the simplest!
Sign and Magnitude Here, the left-most bit is just used for the sign therefore the minimum is going to be all 1s, and the maximum is going to start with a 0 but all the other digits are 1. Remember in this case we've wasted two representations for 0!
Two's Complement Here, the first weight is negative, so the minimum is going to be the first bit 1, and no positive bits. The maximum is going to be the other way round; first bit 0, and the rest all 1s. Notice how the range is wider because we haven't wasted two representations for 0.
Overflow and Underflow
This part is easy. Our ranges help us know when a calculation is going to cause an overflow or an underflow. If we are using an 8-bit register in two's complement, then our range is from -128 to 127. Be very careful of calculations that can result in an error. Example:
99 + 48 would cause an overflow because the result is too far on the positive side!
-99 - 48 would cause an underflow because the result is too far on the negative side!
Binary Number Representations and Two's Complement are topics that come out in every exam. Repeatedly working out the following will help you be prepared.
Q1. Express the following decimal numbers in an 8-bit binary register using sign and magnitude:
a. -20
b. 50
c. -68
Q2. Assuming you have 8-bits to represent a number and you are working in two’s complement, find the binary expression of:
a. -55
b. -14
c. 94
Q3. Perform the following calculations using two’s complement arithmetic. All numbers are represented in an 8-bit register.
a. 99 – 56
b. 85 – 106
c. -25 – 30
Q4. Write the decimal value of the following numbers expressed in two’s complement:
a. 10010011
b. 00110110
Q5. Given a 6-bit register, work out the possible ranges of decimal numbers for each of the following representations:
a. Unsigned Binary
b. Sign and Magnitude Binary
c. Two's Complement Binary
Answers are available here.
Comments