πŸ“•

Mathematical Operations with the Binary Base

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

10410+210=011010002+000000102=011010102=10610104_{10}+ 2_{10}= 0110 1000_{2}+ 0000 0010_{2}= 0110 1010_{2}= 106_{10}
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.
10410+2410=011010002+000110002=100000002=010104_{10}+ 24_{10}= 0110 1000_{2}+ 0001 1000_{2}= 1000 0000_{2}= 0_{10}
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

βˆ’1042=βˆ’(10410+210)=βˆ’(011010002+000000102)=111010102=βˆ’10610-104_{2}= -(104_{10}+ 2_{10})= -(0110 1000_{2}+ 0000 0010_{2}) = 1110 1010_{2}= - 106_{10}
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:
βˆ’10410βˆ’2410=βˆ’(10410+2410)=βˆ’(011010002+000110002)=100000002-104_{10}-24_{10}= -(104_{10}+ 24_{10}) = -(0110 1000_{2}+ 0001 1000_{2}) = 1000 0000_{2}
The next example shows a subtraction of a positive and a negative number.
βˆ’10410+2410=βˆ’(10410βˆ’2410)=βˆ’(011010002βˆ’000110002)=110100002-104_{10}+ 24_{10}= -(104_{10}-24_{10}) = -(0110 1000_{2}- 0001 1000_{2}) = 1101 0000_{2}

Multiplication

210βˆ—6010=000000102βˆ—001111002=011110002=120102_{10}* 60_{10}= 0000 0010_{2}* 0011 1100_{2}= 0111 1000_{2}= 120_{10}βˆ’210βˆ—6010=βˆ’000000102βˆ—001111002=βˆ’011110002=111110002=βˆ’12010-2_{10}*60_{10}= -0000 0010_{2}*0011 1100_{2}= -0111 1000_{2}= 1111 1000_{2}= -120_{10}
In none of the cases shown above did an overflow occur.

Division

6010/210=001111002/000000102=000111102=301060_{10}/2_{10}= 0011 1100_{2}/ 0000 0010_{2}= 0001 1110_{2}= 30_{10}
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:
00002Β andΒ 100020000_{2}\space and\space 1000_2

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,
y2=(2nβˆ’x)2 y_2= ( 2^n - x)_2
For example we are working with 3 bits and we want to know the two complement of binary -010, doing the operation,
y2=10002–0102=1102,y_2 = 1000_2 – 010_2 = 110_2,
and the result is -010 in binaryΒ represented in two complement.
Adding and subtracting numbers becomes easy.
310–210=0112–0102=0112+(10002βˆ’0102)=0112+1102=10012=0102,3_{10} – 2_{10} = 011_2 – 010_2 = 011_2 + (1000_2 - 010_2) =011_2 + 110_2 = 1001_2 = 010_2,
in this case we have to discard the MSB, the result is correct as the operands had different signs.
310+210=0112+0102=1012,3_{10} +2_{10} = 011_2 + 010_2 = 101_{2},
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 0112=310011_2=3_{10}).
βˆ’310–210=1012+1102=10112,-3_{10} – 2_{10} = 101_2 + 110_2 = 1011_2,
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 1002=βˆ’410100_2=-4_{10}).
310–110=1012+1112=11002=1002,3_{10} – 1_{10} = 101_2 + 111_2 = 1100_2 = 100_2,
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 241024_{10}Β 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:
2410=00011000224_{10} = 00011000_2
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:
βˆ’2410=111010002-24_{10} = 11101000_2
If we calculate -2410Β with the previously mentioned formula y2=(2nβˆ’x)2y_2=(2^n-x)_2 we get:
2410=1000000002–000110002=11101000224_{10} = 100000000_2 – 00011000_2 = 11101000_2
Β 
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
Β