Bit Manipulation: Adding and Subtracting two bits.

Adding and Subtracting two bits:

Adding two bits:

Adding two binary is easy. And is similar to adding a decimal number. Here are the rules for adding binaries:
  • When adding two ones $1 + 1 = 1 0$. The1 is carried to the bit at left.
  • When adding zero and one: $1+0=1$ similarly $0+1=1$
  • When adding two zeros: $0+0=0$
Thus when adding $1010 + 0110$ these steps are to be followed:
  • Add the least significant bits/last bits first. It gives $0+0=0$.Thus the last bit of result is going to be zero.
  • Second last bits: $1+1=0$ and 1 is left as a carry for the third last bit.
  • The third last bit is summing a zero, a one and a carry of one from previous steps. Thus $1+ 0 + 1 = 0$ and 1 is left as a carry for our first bit.
  • Finally, for the first bit, we have to add a one from the carry, a one and a zero. That is: $1 + 1 +0 = 10$ This is written as it is. 
  • The result is thus: $10000$
Try your hand at the following sums:

  1. $10110110 + 10101000 = 101011110$
  2. $100110 + 101 = 101011$
  3. $101101 + 1110 = 111011$

Subtracting two bits:

Subtracting two bits is fairly similar to decimal subtraction. Here are the basic rules for bit subtraction.
  1. $1-0=1$
  2. $1-1=0$
  3. $0-0=1$
  4. $0-1=1$ with a carry of one. This step needs explanation. 
Let us take an example of $1010-0001$ This is $10-1$ in decimal notation.


  1. We start with the last binaries: which is $0-1$ Now this is something which can't be done normally. We look at the binary at the left of the greater binary which is here, 1010. The bit at the left of last bit is a one. We cut it replace it with a zero and place two ones over the last bit. What we have done is, written the last two bits as a sum: $10 = 1+1$. Thus the last bit becomes $1 + 1 + 0$ Now subtraction can be done. We have to subtract one from it. Thus $1 + 1 + 0 - 1 = 1$.
  2. Now, coming to the second last bits. Now the second last bit of 1010 is turned into a 0 and we have to subtract a zero from it. It is easy, add a zero at the left of your answer from the previous step.
  3. The second bit is again zero minus zero. Add a zero at the left of your answer.
  4. Now, we finally have one minus zero. Add one to your answer. Your answer now, it: 1001.
Let us try a different number: $1000 - 0001$.
  1. The last bits are zero and one, can not subtract normally.
  2. the left bit of 0 in 1000 is a zero. Can not carry normally.
  3. Again a zero bit as the second bit in 1000.
  4. Finally, we have one. We cut it replace it with a zero and place two ones at the second bit in 1000. Thus the second bit is : $1 + 1 + 0$. What we have done essentially is replaced 1000(8) with a sum of two fours. We now take one of these ones, cut it to make this bit as $0 + 1 + 0$and add two ones at the second last bit. Which is now, $1 + 1 + 0$. This time we have replaced a four with two twos. Again we take one of these ones. And place two ones on the last bits. Here we replaced a one with two ones. The last bit is now $1 +1 + 0$.
  5. Now our last bit is $1 + 1 + 0$. Second Last bit is $0 + 1 + 0$. Second bit is $0 +1+0$. And the first bit is : $0$.
  6. We can subtract 0001 now easily. The last bit of our answer is $1+1+0-1=1$. The second last bit is 1, the second bit is 1 again and the first bit is 0, The answer is $0111$.

No comments:

Post a Comment

Installing albert on ubuntu 19.04

Installing Albert on Ubuntu 19.04... Albert is not still released for ubuntu 19.04. But still, you can install it using the following ...