Operations on binary numbers

Addition on binary numbers

Addition in binary numbers works same as the decimal ones. Basically:

  • 0 + 1 = 1
  • 1 + 1 = 10 (since in decimal, 1+ 1 = 2 and the binary equivalent of 2 is 10)

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:

    1 1  \ \ \ \ 1\ 1\
  0 0 1 1 1 \ \ 0\ 0\ 1\ 1\ 1
+ 1 0 1 0 1 +\ 1\ 0\ 1\ 0\ 1


  1 1 1 0 0 \ \ 1\ 1\ 1\ 0\ 0

Subtraction

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.

Signed Binary Numbers

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:

  • Invert all the bits, that means the number will become 0101
  • Then add the bit 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,

  • we have number 5
  • we add 1, we get 6, which is within range and when we subtract 1, we get 4, which is also within the range.
  • consider 15, when we subtract 1, we get 14 which is fine, but when we add 1, we get 16. The binary of 16 is 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 1000 1001 1010 1011 1100 1101 1100 1111 0000 0001 0010 0011 0100 0101 0110 0111
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