Categories
Number System

Subtraction using (r-1)’s compliment

(r – 1) ’s compliment:   (M – N) r

This is similar to r’s compliment. There is a difference while dealing with the final carry we get

If we have both M & N positives, then

The Procedure for doing subtraction using (r – 1) ’s compliment is as follow:

  1. Take (r – 1) ’s compliment of subtrahend N
  2. Add it to minuend M
  3. If we get a carry, add 1 to the result otherwise take (r – 1)’s compliment of the result and place a –ve sign in front of it.

If we have negative M & positive N, then i.e. – m – n where m & n are magnitudes of M&N

The Procedure for doing subtraction using (r – 1) ’s compliment is as follow:

  1. Take (r – 1) ’s compliment of subtrahend N
  2. Add it to minuend M

If we get a carry, add 1 to the result and also take (r – 1’s compliment and place a –ve sign in front of it otherwise if there is no carry then do nothing

Eg. 10101002 – 10001002

1’s compliment of 1000100 is 0111011 and then adds to 1010100

Eg. 10001002 – 10101002

        1’s compliment of 1010100 is 0101011 and it is added to 1000100

As we don’t have carry so we take 1’s compliment of the result and put the –ve sign in front

So answer is -0010000   

We don’t have to worry about whether M is larger or N is larger, carry which we get takes care of this thing. So one has to just follow the procedure and we’ll get the result.

Categories
Number System

Subtraction using compliments

Subtraction method mentioned earlier looks good when we do it on paper and pencil but to implement a subtraction method on a digital platform then subtraction using compliments is better and efficient.

r’s compliment:

If we are given numbers M & N with base r, then we can to have to find M – N then we can apply the following method:

If M and N both are positives then

  1. Take r’s compliment of subtrahend N
  2. Add the compliment to minuend M
  3. If we get a carry then discard it otherwise take r’s compliment of the result we get in step 2 and place –ve sign in front of this.

If M is negative and N is positive then i.e. – m – n where m & n are magnitudes of M&N

  1. Take r’s compliment of subtrahend N
  2. Add the compliment to minuend M
  3. If we get no carry then discard it otherwise if carry is 1 then, take r’s compliment of the result we get in step 2 and place –ve sign in front of this.

Eg. 7654310 – 6654310

 M=76543

N=66543

10’s compliment of N=33457

As both we ignore carry and answer we get is 0010 which is 2 (not -14) hence it is a wrong answer. It may seem very surprising as we have followed the proper procedure and yet not able to get the answer. Why???????????

Because we have to apply the 2nd rule as M is negative and carry is 1 so  we take 2’s compliment of the answer and final answer we get is – (2’s compliment of 00102) = – 11102 = – 1410

Eg.  – 910 – 1010

Here we see – 1010 can be represented in 2’s compliment in 5 bits so we use 5 bits for both 9= 01001              10=01010

2’s compliment of 9 to represent -9 = 101112

2’s compliment of 10 to represent -10 = 101102

As there is a carry so we’ll not ignore it as M is negative and we’ll calculate 2’s compliment of answer to get the actual answer

 Answer= – (2’s compliment of 01101) = – 10011 = – 1910

And we find the correct answer

Eg.  2610 – 610

26 = 11010

6   = 00110

2’s compliment of 6 = 11010

We ignore the carry as M is positive and answer is 10100

Final answer = 10100 = 20 CORRECT

Categories
Number System

COMPLIMENTS

For a number system with base r (with r > 1), there are two types of compliments:

  1. r’s compliment
  2. (r-1) ‘s compliment

r’s Compliment: If we have a positive number N with integer part of n digits  then we define r’s compliment  as 

 10’s compliment of number (5432) 10 is 104– 5432 = 10000 – 5432 = (4568) 10          

  [value of n is 4]

10’s compliment of number (0.5432) 10 is 100– 0.5432 = 1 – 0.5432 = (0.4568) 10       

[value of n is 0 as there is no integer  part in this number]

10’s compliment of number (54.32) 10 is 102– 54.32 = 100 – 54.32 = (45.68) 10             [value of n is 2]

2’s compliment of number (110.01) 2 is (23)2– 110.012= 1000 – 110.01 = 001.11         [value of n is 3]

Eg. Find 2’s compliment of (110.01) 2 (value from above example)

Ans: Following the rule we get the answer as 000.1110

(r-1) ‘s compliment: If we have a positive number N with base r and integer part with digits n and fractional part of m digits,  we define (r-1) ‘s compliment as r– r-m– N         

9’s compliment of number (5432) 10 is 104– 100 – 5432 = 10000 – 1 –  5432 = (4567) 10 

 [Value of n is 4 and of m is 0]

9’s compliment of number (0.5432) 10 is 100– 10-4 – 0.5432 = 1 – 0.0001 – 0.5432 = (0.4567) 10      

 [Value of n is 0 as there is no integer part in this number and of m is 4]

 9’s compliment of number (54.32) 10 is 10– 10-2 – 54.32 = 100 – 0.01 – 54.32 = (45.67) 10 [Value of n is 2 and of m is 2]

1’s compliment of number (110.01) 2 is (23)10– (2-2)10 – 110.012= 1000 – 0.01 – 110.01 = 001.10      [Value of n is 3 and of m is 2]

Applying the rule to above example we get the answer as 001.10, the sameJ

Relation between the r’s Compliment and (r-1) ‘s compliment: We can get r’s Compliment from (r-1) ‘s compliment by just adding r-m  and vice-versa by subtracting   r-m

Compliment of compliment: If we take r’s compliment of r’s compliment we get the original number as

Let the original number be N

So r’s compliment is r– N and compliment of compliment is r– (r– N) = N  which is the original number

and the same is for (r-1) ‘s compliment.

Let the original number be N

So r’s compliment is r– r-m – N and compliment of compliment is r– r-m – (r– r-m – N ) = N  which is the original number

1’s compliment and 2’s compliment:  Following table would show the representation of decimal -7 to +8 in 1’s and 2’s compliment. In the signed number , we take the 4th bit as signed bit and make this bit one(‘1’) when ever we have negative number & make it zero(‘0’) when we have a positive number and rest of  the 3 bits are used to represent magnitude.

Comparison of 1’s compliment and 2’s compliment:

  • 1’s compliment is easier to implement than 2’s compliment
  • While for the subtraction, 2’s compliment is better because for 2’s compliment we need only 1 arithmetic operation while 2 arithmetic operations are needed for 1’s compliment.
  • 2’s compliment has only one representation for zero while  1’s compliment has two representation for zero as shown above
Categories
Number System

SUBTRACTION (without using compliment method)

Decimal system: Let’s now analyze the subtraction of decimal systems. For subtraction we first subtract the least significant digit of subtrahend from that of minuend and if least significant digit of subtrahend is greater than that of minuend then we take borrow from the next digit of minuend and add 10 (base) to the previous digit and then subtract and similarly we proceed further.

Binary system:

There are 4 cases of subtraction of bit by bit:

         Subtraction    borrow

0 + 0 =      0                     0

1 + 0 =      1                     0

0 + 1 =      1                     1

1 + 1 =      0                   0

Let’s now we do subtraction in binary

Octal system: Let’s now apply the same procedure for the octal system

Hexadecimal: We can apply the same rule to this numbering system also

Categories
Number System

ADDITION

Decimal system: Although all of us are doing addition in decimal system since years and this may look a bit odd to study this again but I’ll still emphasize to study it further. Let us analyze the addition for decimal system.  For addition we firstly add least significant digits and keep the least significant digit of the result we get, in the least significant position of the sum and rest of the part is taken as carry and added to the next digits as:

But let me put it in another way which is actually a generalized method for addition. We first add least significant digits and if result we get is equal to or more that ‘10’ (base) then we’ll subtract ‘10’ (b) from the result as many times we can and remainder is then saved in the sum register while the number of times we can subtract is taken as carry.

Now we’ll take this method and apply it to any system for addition

Binary system:
There are 4 cases of addition of bit by bit:
                Sum       carry
0 + 0 =     0                0
1 + 0 =      1               0
0 + 1 =      1               0
1 + 1 =      1             1
Categories
Number System

Binary to octal & hexadecimal And Vice-versa

Binary to octal and hexadecimal:

To convert the number from binary to octal we make groups of 3 bits starting from the decimal point (in case of fractions)  and from right most bit (in case of integers) and write the corresponding octal equivalent of those 3 bits every time.

The process of replacing the long strings of 1 & o with the shorter strings like Octal & hexadecimal equivalents is known as chunking.

Hexadecimal and octal to binary:

To do the above we convert the digits into their corresponding binary equivalents of 3-bits and 4-bits for octal and hexadecimal numbers respectively.

Categories
Number System

Decimal to hexadecimal conversion

Decimal to Hexadecimal: following the same procedure

Convert 26 decimal to hexadecimal: This method is also called Hex-dabble method.

Convert 0.3875 to hexadecimal

We can convert a fraction from a decimal to hexadecimal form by multiplying wit base and following the same procedure.

Fraction is 0.3875

0.3875 * 16                          =6.2000                =6 + 0.2000         a-1=6

0.2000 * 16                          =3.2000                =3 + 0.2000         a-2=3

0.2000 * 16                          =3.2000                =3 + 0.2000         a-3=3

Similarly a-4=3, a-5=3, a-6=3 so on…….

Hence the hexadecimal equivalent is 0.63333…

Categories
Number System

Binary to octal conversion

To convert the number from binary to octal we make groups of 3 bits starting from the decimal point (in case of fractions)  and from right most bit (in case of integers) and write the corresponding octal equivalent of those 3 bits every time.

The process of replacing the long strings of 1 & o with the shorter strings like Octal & hexadecimal equivalents is known as chunking.

Categories
Number System

Decimal to Hexadecimal conversion

Following the same procedure as earlier

Convert 26 decimal to hexadecimal: This method is also called Hex-dabble method.

Convert 0.3875 to hexadecimal

We can convert a fraction from a decimal to hexadecimal form by multiplying wit base and following the same procedure.

Fraction is 0.3875

0.3875 * 16                          =6.2000                =6 + 0.2000         a-1=6

0.2000 * 16                          =3.2000                =3 + 0.2000         a-2=3

0.2000 * 16                          =3.2000                =3 + 0.2000         a-3=3

Similarly a-4=3, a-5=3, a-6=3 so on…….

Hence the hexadecimal equivalent is 0.63333…

Categories
Number System

Decimal to Octal conversion

Let the decimal number be 26 so we can convert it to octal by dividing the number with the base=8  and then write the quotient and remainder as shown below and then again divide the quotient with the base till we get quotient as zero. This method is also called Oct-dabble method.

Conversion of fraction from decimal to octal is similar to the one for binary and follows the following procedure: just multiply the fraction by base=8 and integer which we get to left of the decimal point is saved

Fraction is 0.3875

0.3875 * 8                            =3.1000                =3 + 0.1000         a-1=3

0.1000 * 8                            =0.8000                =0 + 0.8000         a-2=0

0.8000 * 8                            =6.4000                =6 + 0.4000         a-3=6

0.4000 * 8                            =3.2000                =3 + 0.2000         a-4=3

And let me re-write the same facts about the procedure followed above. It may go on till we get the fractional part as zero but this may not be possible in finite no of bits hence usually we restrict the no of bits to represent the value to a finite value and get an approximate value…

So the (0.3875)10 = 0.a-1 a-2 a-3 a-4 ……= (0.3063….) 8 = (0.3063)8

If we convert the value (0.3063)2 back to decimal

=3 * 8-1 + 0 * 8-2 + 6 * 8-3 + 3 *8-4 =0.3777 which is only approximate value of the actual value. So we see the equivalent we get in nay number system for a fraction may only be an approximate value. The same again that we may increase the no of bits to increase the accuracy.