Thursday, July 29, 2010

Arithmetic Operation -Octal & Hexadecimal

Octal Addition and Subtraction : Radix or Base for Octal numbers are 8 and consists 0 to 7. While adding we follow following rules:

Addition

We already know that 8 is the base or radix of the Octal Number System, after adding of two digit if you get below 8 then write down it, but if it is more than 7 then divide the result by 8 and write down the remainder in the place of result and the quotient as carry numbers.

Example-1

(447)8 + (622)8
                      
                                                                    Rules

       1                     1) 7 + 2 = 9 (above 7) 9/8 Quotient=1 Remainder=1 (Carry = 1)
 ------------            2) 4 + 2 + 1(carry)=7 is below 8 (Carry = 0)
    4 4 7                   3) 4 + 6 = 10 (above 7) 10/8 Quotient=1 Remainder=2(Carry = 1)
    6  2 2
 ------------
  1 2 7 1
 ------------
  Answer :  (447)8 + (622)8= (1271)8



Example-2

(237.54)8 + (753.74)8
    
                                                            Rules

    1 1 1 1                1) 4 + 4 = 8 (8/8 Quotient=1 Remainder=0)(Carry = 1)
---------------          2) 5 + 7 + 1 (carry) = 13(13/8 Quotient=1 Remainder=5)Carry = 1)
   
3 7 . 5 4               3) 7 + 2 + 1 (carry) = 10(10/8 Quotient=1 Remainder=2)(Carry = 1)
    7 2 . 7 4              4) 3 + 7 + 1 (carry) = 11(11/8 Quotient=1 Remainder=3)(Carry = 1)
---------------
 1 3 2 . 5 0
---------------

  Answer :  (37.54)8 + (72.74)8= (132.50)8


Subtraction


Octal Subtraction : While subtracting if minuend digit smaller than subtend then add 8 to the minuend digit and then subtract and in the process 1 should be borrow from the minuend digit at the end.

Example-1

(345)8 - (126)8

                                                    Rules
     1                     1) 5 is less than 6 so 5+8 = 13 - 6= 7 (Borrow = 1)
------------          2) 4 -1(borrow) = 3 is bigger than subtend so 3 - 2=1(Borrow = 0)
  
3 4 5                 3) 3 -1 = 2 (Borrow = 0)
   1 2 6
 ------------
   2 1 7
 ------------
  Answer :  (345)8 - (126)8 = (217)8



Example-2

(246.24)8 - (157.56)8

                                                           Rules


   1 1 1    1           1) 4 + 8 - 6 = 6 (Borrow = 1)
 --------------      2) 2-1(borrow) =1 + 8 - 5 = 4 (Borrow = 1)
    2 4 5 .2 4         3) 5-1(borrow) =4 + 8 - 7 = 5 (Borrow = 1)
   1 5 7 . 5 6         4) 4-1(borrow) =3 + 8 - 5 = 6 (Borrow = 1)
 ---------------       5) 2-1(borrow) =1 - 1 = 0 (Borrow = 0)
   0 6 5 . 4 6
 ---------------

  Answer :  (246.24)8 - (157.56)8 = (65.56)8



Hexadecimal Addition and Subtraction : Radix or Base for Octal numbers are 16 and consists 0 to 15 (0 - 9, A, B, C, D, E, F). While adding we follow following rules:

Addition

We already know that 16 is the base or radix of the Hexadecimal Number System, after adding of two digit if you get below 16 then write down it, but if it is more than 15 then divide the result by 15 and write down the remainder in the place of result and the quotient as carry numbers. Replace Alphabet with its equivalent decimal numbers. e.g.. A = 10 or E = 14

Example-1

B83)16 + (A2D)16 = (11 8 3)16 + (10 2 13)16

                                                             Rules

         1                 1) 3 + 13 = 16(above 15) 16/16 Quotient=1 Remainder=0(Carry=1)
 ------------          2) 8 + 2 + 1(carry)= 11 is below 15 (Carry=0)
    11   8   3         3) 10 + 11 = 21(above 15) 21/16 Quotient=1 Remainder=5(Carry=1)
    10   2 13
 ------------
  1 5   11 0  = 1 5 B 0
 ------------

  Answer :(B83)16 + (A2D)16 = (15B0)16



Example-2

(AB.CD)16 + (D5.2F)16 = (10 11.12 13)16 + (13 5.2 15)16

                                                      Rules

   1          1                1) 13 + 15 = 28 (28/16 Quotient=1 Remainder=12(Carry = 1))
 -----------------        2) 12 + 2 + 1(carry) = 15 is below 15 (Carry = 0))
   10 11 .12 13          3) 11 + 5 = 16 (16/16 Quotient=1 Remainder=0 (Carry = 1))
   13  5  .  2 15         4) 10 + 13 + 1(carry)= 24 (24/16 Quotient=1 Remainder=8 (Carry = 1))
-----------------
  1 8 0  . 15 12       = 180.FC16
------------
  Answer : (AB.CD)16 + (D5.2F)16  = (180.FC)16



Subtraction


Hexadecimal Subtraction : While subtracting if minuend digit smaller than subtend then add 16 to the minuend digit and then subtract and in the process 1 should be borrow from the minuend
digit at the end.

Example-1

(BA3)16 - (AB4)16 = (11 10 3)16 - (10 11 5)16)

                                                             Rules

         1              1) 3 is less than 5 so 3 + 16 = 19 - 4= 15 (F) (Borrow = 1)
-------------       2) 10 - 1(borrow) = 9 + 16 = 25 - 11 = 14 (E) (Borrow = 1)
  11 10  3           3) 11 -1 = 10 = 10 - 10 = 0 (Borrow = 0)
 
10 11  4
------------
   0   E   F
------------

  Answer : (BA3)16 - (AB4)16 = (EF)16



Example-2

(C5.A2)16 - (A7.B3)16 =  ((12 5.10 2)16 - (10 7.11 3)16)

                                               Rules

   1    1    1                 1)  2 + 16 - 3 = 15 (F) (Borrow = 1)
---------------              2)  10-1(borrow) =9 + 16 - 11 = 14 (E) (Borrow = 1)
  12  5 . 10  2             3)  5-1(borrow) =4 + 16 - 7 = 13 (D) (Borrow = 1)
  10  7 . 11  3             4)  12-1(borrow) =11 - 10 = 1
---------------
  1  D .  E   F
---------------
  Answer : (C5.A2)16 - (A7.B3)16 = (17.EF)8



After fortnight coming back with some more with system only. !

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!!!

Thursday, July 1, 2010

Number Systems-II - This one not that systematic, do not get lost.


Last time we have seen the methods and way to convert the Decimal numbers to Binary, Octal and Hexadecimal. Today I will tell you how you can convert the Binary, Octal and Hexadecimal numbers to Decimal

Binary to Decimal

(101)2=(?)10  (Convert binary 101 to decimal). Now we have 101 means three digit, so let us consider n=3.

101 = 1 x 2n-1+ 0 x 2n-2 + 1 x 2n-3

= 1 x 23-1 + 0 x 23-2 + 1 x 2n-3
= 1 x 22 + 0 x 21 + 1 x 20
= 1 x 4 + 0 x 2 + 1 x 1
= 4 + 0 + 1 = 5
= (101)2 = (5)10


For fractional Numbers :

(.11)2=(?)10

Here 'n' is 2,
.11 = 1 x 2n-(n+1) + 1 x 2n-(n+2)
= 1 x 2-1 + 1 x 2n-2
= 1/21 + 1/22
= 1/2 +1/4
= 3/4 = .75
= (.11)2 - (.75)10


Another example : (11.011)2 - (?)10
= 1 x 21+ 1 x 20             + 0 x 2-1 + 1 x 2-2 + 1 x 2-3
 = 1 x 21 + 1 x 20           + 0 x 2-1 + 1 x 2-2 + 1 x 2-3
 = 2 + 1                         + 1/4 + 1/8
= 3.3/8  = 3.375
= (11.011)2 - (3.375)10


Easy way out:

8 4 2 1   2 4
1 0 0 1 . 1 1
Check the numbers on the top, its 20 onwards 2n-1, values written on the top for whole number right most digit to start with and for fractional numbers left most digit. For whole numbers add the values for non zero elements like 1 and 8 because 4 and 2 having a '0' below it. So by adding 8 + 1 =9 and divide the values for fractional numbers: 1/2 + 1/4 = 3/4 = .75.

(1001.11)2=(9.75)10



Octal to Decimal

if you understand the conversion of Binary to decimal then other number system will be same process with different power.

Octal to Decimal
(56.34)8=(?)10

Two digit means n=2,
 = 5 x 8n-1+ 6 x 8n-2          + 3 x 8n-(n+1)+ 4 x 8n-(n+2)
= 5 x 81+ 6 x 80                 + 3/81+ 4 x 82
= 5 x 8 + 6 X 1                  + 3/8 + 4/ 64
= 40 + 6                             + 3/8 + 4/ 64
= 46.4375
= (56.34)8 = (46.4375)10



Hexadecimal to Decimal

(75.55)16=(?)10
Two digit means n=2, so
 = 7 x 16n-1+ 5 x 16n-2         + 5 x 16n-(n+1)+ 5 x 16n-(n+2)
= 7 x 161+ 5 x 160               + 5/161+ 5 x 162
= 7 x 16 + 5 x 1                   + 5/16 + 5/256
= 112 + 5 + 5/16 + 5/256
= 117.332031

(75.55)16 = (117.332031)10



Another Example :

(3A.B)16=(?)10

Here 'A to F' to be considered as 10 to 15 and as a single unit
3 x 16n-1+ 10 x 16n-2        +11 x 16n-(n+1)
= 3 x 16 + 10 x 1              + 11/16
=  48 + 10                        + 11/16
=   58.6875
= (3A.B)16 = (58.6875)10



Octal to Binary and Binary to Octal

Last number of Octal number system is 7. 7 equivalent to binary is 111, that means three digit. so while converting Octal to binary just write down the binary value of the octal number.

(56.34)8=(?)2

   5     6   .  3    4
101 110   011 100(56.34)8=(101110.011100)2


Same for Binary to Octal, only first you have to three digits of binary nose, where for whole number start from last digit and fractional numbers start from first digit. At the end if numbers of digits less than three than put a leading zero for whole number and a trailing zero for fractional part.

(1011011101.1101101)2=(?)8

001 011 011 101 . 110 110 100

1      3      3     5   .   6    6     4

(1011011101.1101101)1=(13335.664)8



Hexadecimal to Binary and Binary to Hexadecimal

Last number of Octal number system is F means 15. 15 equivalent to binary is 1111, that means four digit. so while converting Hexadecimal to binary just write down the binary value of the Hexadecimal number.
(7A.3B)16=(?)2

0111 1010   . 0011 1011

7         A      .    3       B

(7A.3B)16=(01111010.00111011)2


Same for Binary to Hexadecimal, only first you have to group four bits for whole numbers right to left and for fractions left to right  At the end if numbers of digits less than three than put a zero at leading position for whole numbers and a trailing zero for fractional part.
(1010011100.1101101)2=(?)8

 0010 1001 1100  .  1101 1010

2          9       12    .    13      10

(1011011101.1101101)2=(29C.DA)16