Operations on binary numbers
Addition in binary numbers works same as the decimal ones. Basically:
Similarly, 1 + 1 + 1 = 11 (binary equivalent of 3 is 11)
1 + 1 + 1 = 10 + 1 = 11
While adding big numbers, we carry forward the first bit:
To do the subtraction, A - B
,
we can write this as A + (-B)
, that means B has to be converted into a negative number and then to be added with A.
Let’s understand signed binary numbers first.
Contains positive and negative numbers. The way decimals have both positive and negative numbers for example, -6, 4, -99, 56 etc., the same way binary numbers can also have both positive and negative numbers. But they have different way of representation.
The most common method of representing negative numbers is two’s complement method.
Suppose we want to get the two’s complement of number 1010
. We will follow these steps:
0101
1
to the inverted number, that means 0101 + 1 = 0110
So, two’s complement of 1010
will be 0110
.
Generally, when the binary numbers are used in computers, or represented in the memory, then, depending upon range, they can have certain amount of digits or bits.
For example, if we take 4 bits binary number, then we can only represent numbers from 0 to 15, this is the case of unsigned numbers.
The lowest number that can be represented is 0
and the highest number that can be represented is 15
.
4 bit binary number | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0100 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Decimal Equivalent | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
Now consider this,
10000
, it has now 5 digits, but we have only 4-bits capacity. So the leftmost bit 1
will be discarded and this is called ‘overflow’.Overflow happens when there are more bits than the capacity. So it will discard the leftmost bits and fit the rightmost bits up to capacity, similarly, there is something called underflow which is the opposite.
So for 16, after overflow, we get 0000
, that means we go back to 0.
So, there is a sort of circular path that we traverse while addition and subtraction.
For signed numbers, we shift the overflow and underflow points. We keep the leftmost bit for determining the sign of the number.
Let"s take the 4-bit number scenario, subtracting 1
from 0
, we get 1111
, So, we say 1111
is -1
, So we change our range to accommodate the negative numbers depending upon the number of bits.
4 bit binary number | 1 000 |
1 001 |
1 010 |
1 011 |
1 100 |
1 101 |
1 100 |
1 111 |
0 000 |
0 001 |
0 010 |
0 011 |
0 100 |
0 101 |
0 110 |
0 111 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Decimal Equivalent | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Here the leftmost bit determines if the number is a negative (1
) or positive(0
) and the range will be from -8 to 7. For unsigned numbers, the leftmost bit is also used to represent numbers and the numbers are all positive after 0. So the will be from 0 to 15.
© progshala.in