Operations in the binary base are equal to those in the decimal base. The difference is when we need to store numbers with a finite amount of bits. Let’s do some operations with 8 bits, where the MSB is the sign bit and the other 7 bits hold the magnitude of the number. We should be careful to operate with MSB separately as we do with plus and minus signs in the decimal base.
Sum
As both summands are positive (their MSBs are 0), the most significant bit of the result is also 0. The result also isn’t greater than the maximum possible number we can write with 7 numerical bits 0111 11112– so an overflow didn’t occur.
In this example the second number is bigger than in the first example, but the result is unusually small – that is because an overflow occurred: we only have enough bits to store binary numbers with 7 digits, but our result has 8 digits (not counting the MSB, as it is used to store the sign) and due to that the highest, eight, digit gets cut off, as we do not have enough space to store it – this is called an overflow and the result we get is incorrect.
Subtraction
Here we subtracted two negative numbers so the result is negative (MSB of the result is 1). The overflow can also occur with negative numbers as seen in the following example:
The next example shows a subtraction of a positive and a negative number.
Multiplication
In none of the cases shown above did an overflow occur.
Division
If the division results in a decimal number, the decimal places are discarded.
Two Complement
The way we do arithmetic operations on paper, like carrying numbers over and deciding which number is greater to influence the sign of the result, when performing a subtraction, is difficult to implement in a digital system. To overcome that there is another way to represent finite binary numbers and it’s called two complement. It is easier to perform calculations of sum and subtraction using two complement representation.
The main advantage of two complement is that we do not have two numbers representing 0 when working with signed numbers (in signed magnitude notation the numbers:
represent +0 and -0 (written with 4 bits), which makes computer calculations more complex). So the two complement always has one negative number more when written with finite bits (0 is defined as a positive number).
First we define how many bits we are working with. After that we perform an operation to find the two complement of a negative number. If you have to do the complement of a number x of n bits, the operation is this,
For example we are working with 3 bits and we want to know the two complement of binary -010, doing the operation,
and the result is -010 in binary represented in two complement.
Adding and subtracting numbers becomes easy.
in this case we have to discard the MSB, the result is correct as the operands had different signs.
the result is wrong as the sign of the operands are equal, but the sign of the result is different => an overflow occurred (maximum positive number is ).
the result is wrong as the sign of the operands are equal, but the of the result is different => an overflow occurred (the maximum negative number is ).
the result is correct as the sign of the operand are equal and the sign of the result is equal (we still need to discard the MSB).
There exists a way easier way of getting a 2’s complement of a number. For example, we want to get the equivalent of in the binary base, written as a 2’s complement with 8 bits:
Firstly we write the negative number as a positive one in the binary base:
Then, going from LSB to MSB we just copy the digits until we get to the first 1, we also copy the fist 1, afterwards we just negate all the remaining bits and we get:
If we calculate -2410 with the previously mentioned formula we get:
Decimal number | Binary number | 3 bits complement two |
3 | 011 | 011 |
2 | 010 | 010 |
1 | 001 | 001 |
0 | 000 | 000 |
-1 | 100 | 111 |
-2 | 101 | 110 |
-3 | 110 | 101 |
-4 | 111 | 100 |