Thursday, July 15, 2010

Binary Addition & Subtraction - Back to the basic

Binary Addition  : Radix or Base for Binary numbers are 2 and having two digits 0 and 1. so while adding we follow following rules:


0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 and carry 1 (Decimal 2 and Binary 10)
1 + 1 + 1 = 1 and carry 1 (Decimal 3 and Binary 11)
1 + 1 + 1 + 1 = 0 carry 10 (Decimal 4 and Binary 100)




Example : 1 Example : 2
Carry >>
       1 1 1 1
Carry >>
    1  1 1 1      1
    1 1 1 0 1     1 0 1 0 . 1 0 1

    0 1 1 1 1

    1 1 0 1 . 1 0 1
Answer :  1 0 1 1 0 0 Answer :  1 1 1 0 0 . 0 1 0

Binary Subtraction
While subtracting we follow the following rules:
0 - 0     = 0
0 + 1     = 1  borrow 1
1 + 0     = 1
1 + 1     = 0


Example : 1 Example : 2

Borrow >>

    1 1 1

Borrow >>

          1

    1 1 1 0 1

   1 0 1 0 . 1 0 1


    0 1 1 1 1


    1 0 0 1 . 1 0 0
Answer :     0 1 1 1 0 Answer :     0 0 0 1 . 0 0 1




Subtraction often is very tricky, you can solve it by using 1's Compliment or 2's Compliment

Like you have 50 - 12 = 38 same 50 + (-12) = 38



Compliment means Invert or all 0's change to 1 and 1's become 0. So Rules for 1's Compliment is :

Make the both side equal by placing leading 0's. Replace all the 1's with 0's and the 0's with 1's, then add both the numbers.

If there was 4 bits and after adding you getting five, the leading bits known as carry bits. If there is a carry bits then remove that from leading position and add to the result of the addition and write the answer down.



 Example :(10010)2 - (1001)2 Minuend : 10010 and Subtrahend : 1001. Minuend contain 5 bits where Subtrahend having 4, so it will be 01001 (both side with same numbers of digits and done by adding leading zeros) 01001 (decimal 9) 0110 (one's complement) Add




Example : 1  Addition

Carry >>

       1 1 1
   18    1 0 0 1 0

+ -9

    1 0 1 1 0
 1 0 1 0 0 0
    0 1 0 0 0
 +             1   
Answer :+9     0 1 0 0 1

1 is a carry bit because after adding two four bits number we are getting 5 bits, remove the carry from there and add to the remaining part of the result.

Result of : (10010)2 - (1001) = (1001)2 or 18 + (-9) = 9



The two's complement of a binary number is obtained by:
1. Replacing all the 1's with 0's and the 0's with 1's (1's Compliment).
2. Adding 1 to the result of 1's compliment using the rules of binary addition.
Now you have the two's complement. The decimal subtraction 18 - 9 = 9 is the same as adding (18) + (-9) = 9
1. Convert the number to be subtracted to its two's complement:(10010)2 - (1001)2 = (10010)2 - (01001)2, 01001 (decimal 9) 10110 (one's complement) + 00001 (add 1 (two's complement)) 10111 (We get)

10111 now represents -9.


Example : 1  Addition

Carry >>


       1 1 1
  18    1 0 0 1 0

+ -9

    1 0 1 1 1
Answer :+9 1 0 1 0 0 1
Note that the final carry 1 is ignored and dropped permanently. So you get :Result of : (10010)2 - (1001) = (1001)2 or 18 + (-9) = 9



If no carry that indicate a negative result. When a subtraction produce negative result then you have to re-compliment the result of addition to get the correct answer.
(1000)2 - (1001)2 = (1000)2 - (0110)2, (one's complement).

Example : 1  Addition

Carry >>

     8    1 0 0 0

+ -9

    0 1 1 0
    1 1 1 0


If no carry bit then re-compliment the result 1110 = 0001 (after 1's compliment)   Result of : (1000)2 - (1001) = (-1)2 or 8 + (-9) = -1  

Answer :  (1000)2 - (1001)2 = (-0001)2




With above example with 2's compliment :(1000)2 - (1001)2 1001 (decimal 9) after 1'compliment we get 0110 + 1 (2's Compliment ) ------- 0111

Example : 1  Addition

Carry >>


 
    8    1 0 0 0

+ -9

    0 1 1 1
    0 1 1 1


If no carry bit then re-compliment the result

0111 = 1111 (after 1's compliment) 1111+1 = 0001(2's compliment)


The result will be -0001 Result of : (1000)2 - (1001) = (-1)2 or 8 + (-9) = -1



 Binary Multiplication

Binary multiplication is same as normal multiplication only we have to know the rules of addition properly.
(1010.11)2 x (101.10)2



Example : 1
      
            1 0 1 0 . 1 1
 x                   1 0 1 . 1 0
                  0 0 0 0 0 0
               1 0 1 0 1 1
            1 0 1 0 1 1
         0 0 0 0 0 0

     1 0 1 0 1 1
  1 1 1 0 1 1 . 0 0 1 0

Answer : (1010.11)2 x (101.11)2= (1javascript:void(0)11011.0010)2



Binary Division

Binary division too is same as decimal division but binary subtraction rules going used here.
(110011)2 / (111)2

Example : 1
1 1 1   1 1 0 0 1 1 1


     1 1 1    


  1 0 0 0 1


     1 1 1

        1 1 1 1


        1 1 1

        0 0 0

          1 1 1
Answer : (110011)2 / (111)2= (111)2


!!!Hope it will be helpful!!!

No comments: