# 1.1.3. 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.

## 1.1.3.1. Sum

10410+ 210= 0110 10002+ 0000 00102= 0110 10102= 10610

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= 0110 10002+ 0001 10002= 1000 00002= 010

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.

## 1.1.3.2. Subtraction

-10410- 210= -(10410+ 210)= -(0110 10002+ 0000 00102) = 1110 10102= - 10610

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) = -(0110 10002+ 0001 10002) = 1000 00002

The next example shows a subtraction of a positive and a negative number.

-10410+ 2410= -(10410- 2410) = -(0110 10002- 0001 10002) = 1101 00002

## 1.1.3.3. Multiplication

210* 6010= 0000 00102* 0011 11002= 0111 10002= 12010

-210* 6010= -0000 00102* 0011 11002= -0111 10002= 1111 10002= - 12010

In none of the cases shown above did an overflow occur.

## 1.1.3.4. Division

6010/210= 0011 11002/ 0000 00102= 0001 11102= 3010

If the division results in a decimal number, the decimal places are discarded.

## 1.1.3.5. 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 00002and 10002represent +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= ( $$2^n$$ - x)2. For example we are working with 3 bits and we want to know the two complement of -0102, doing the operation,

y2 = 10002 – 0102 = 1102, and the result is -0102 represented in two complement.

Adding and subtracting numbers becomes easy.

310 – 210 = 0112 – 0102 = 0112 + (10002 - 0102) =0112 + 1102 = 10012 = 0102, in this case we have to discard the MSB, the result is correct as the operands had different signs.

310 +210 = 0112 + 0102 = 1012, 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= 310).

-310 – 210 = 1012 + 1102 = 10112, 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= -410).

-310 – 110 = 1012 + 1112 = 11002 = 1002, 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 equvalent of -2410 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 = 000110002

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

If we calculate -2410 with the previously mentioned formula (y2= ( $$2^n$$ - x)2) we get:

-2410 = 1000000002 – 000110002 = 111010002

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