Thursday, 14 April 2011

Binary Algorithm Topic ----- (C.S.A)




Written by:-- Sanjay tiwari sir,    My main Blog--

Image by FlamingText.com
                           
start:------
Addition: - add the binary numbers 11112 and 1102 similar to the way we add the decimal numbers 1510 and 610.
First, we add the numbers in the rightmost column. One plus zero adds to 1.    01111
  + 00110
        1   
Now we add the next column. One plus one adds to 102, so we carry the one to the next column and write the zero under this column.      1 
    01111
  + 00110
       01   
Notice that the third column now contains three ones. Adding the first two ones gives us 102. Adding this sum to the remaining one gives us a total of 112, so we carry a one to the next column and write one under this column.     11 
    01111
  + 00110
      101   
The two ones in the fourth column add to 102, so we carry a one to the final column, and write zero below this column.    111 
    01111
  + 00110
     0101   
Finally, the carry from the previous column plus the two zeros from this column add to 1.    111 
    01111
  + 00110
    10101   
This gives us a final answer of 101012.    111 
    01111
  + 00110
    10101  

Algorithm:-Adding multiple binary numbers uses the same principles as adding two binary numbers, but we will approach it using a slightly different method.

First, we add the numbers in the rightmost column. Recall that 1 plus 1 adds to 102, so we can add the first pair of 1s together, and mark a carry in the next column.       1
     0111
     0110
     1101
     0101
  +  1110   
Now we cross out these 1s since their value is represented by the carry. The sum of the remaining digits in the first column is 1, so we write 1 below this column.       1
     0111
     0110
     1101
     0101
  +  1110
        1   
The second column has two pairs of 1s, so we cross out the first pair of 1s and mark a carry in the next column to represent these values.      11
     0111
     0110
     1101
     0101
  +  1110
        1   
Then we cross out the second pair of 1s and mark another carry in the next column. Now only zeros remain in this column, so we write 0 below it.      1
      11
     0111
     0110
     1101
     0101
  +  1110
       01   
The third column has seven 1s, so we cross out three pairs of 1s, and mark three carries in the next column to represent them. An unpaired 1 remains, so we write it below this column.     1
     11
     111
     0111
     0110
     1101
     0101
  +  1110
      101   
Adding the fourth column generates two carries. Since there is no fifth column, we will create one with all zeros and mark the carries above it. Next we write the unpaired 1 below the fourth column.     1
    111
    1111
    00111
    00110
    01101
    00101
  + 01110
     1101   
Finally, adding the two carries in the last column gives us 102, so we write this below the column, and we have our answer of 1011012.     1
    111
    1111
    00111
    00110
    01101
    00101
  + 01110
   101101  
Algorithm:-To add binary numbers with fractions, we use exactly the same technique we used to add integer binary values. The only difference is that we need to keep track of the radix point so we can distinguish the fractional part of our answer from the integer part. The example below demonstrates how to add the binary numbers 110.012 and 1.0112.
First, we align the two numbers so that the radix point of each number is located in the same column.    110.01
  +   1.011   
Next, we fill in the blank spaces with 0s and add the two numbers together.    110.010
  + 001.011   
The first column adds to 1.    110.010
  + 001.011
          1   
The second column adds to 102, so we write a 0 below it and carry a 1 to the next column.        1
    110.010
  + 001.011
         01   
All of the remaining columns add to 1, so we write 1 below them.        1
    110.010
  + 001.011
    111.101   
This gives us a final answer of 111.1012.        1
    110.010
  + 001.011
    111.101  

Addition: - subtract the binary number 101012 from 11102 using the borrow method.
First, we subtract the rightmost column. 1 minus 0 equals 1.    10101
  - 01110
        1   
In order to subtract the second column, we need to borrow a 1. So we cross out the 1 in the third column, and represent it as two 1s in the second column.       1
      01
    10101
  - 01110
        1   
We can now subtract 1 from the group of two borrowed 1s. This leaves us with 1, so we write it below the second column.       1
      01
    10101
  - 01110
       11   
Now we subtract the next column. Since we borrowed from this column, the subtraction is 0 minus 1 and we must borrow again. However, our next column has no 1 for us to borrow. So, we must first borrow from the last column.     1 1
    0101
    10101
  - 01110
       11   
Then borrow a 1 from the fourth column into our current column.      1
     111
    0101
    10101
  - 01110
       11   
We can now perform the subtraction for the current column. We take 1 away from our group of two blue 1s. This leaves us with a single 1 which we write below the column.      1
     111
    0101
    10101
  - 01110
      111   
In the fourth column, we subtract 1 from 1 for a result of 0.      1
     111
    0101
    10101
  - 01110
     0111   
Our last column contains contains all zeros, so we write 0 below it. This gives us our answer of 001112.      1
     111
    0101
    10101
  - 01110
    00111  

Algorithm:- multiply the binary numbers 11112 and 10112 using the same rules as decimal multiplication.
First, we multiply the top number by the rightmost digit of the bottom number, just as we would in decimal.      1111
    x 1011   
Since this number is 1 and any number multiplied by 1 equals itself, we can simply record the top number below.      1111
    x 1011
      1111   
Now we multiply the top number by the next digit in the bottom number. Since this is the second multiplication, we use a zero as a placeholder in the least significant digit of our answer.      1111
    x 1011
      1111
         0   
The second digit is 1 so we move the top number down into our answer.      1111
    x 1011
      1111
     11110   
Next, we multiply by the third digit. Since this is the third multiplication, we record two zeros in the answer.      1111
    x 1011
      1111
     11110
        00   
Notice that the third digit is 0. Since any number multiplied by zero is zero, we place a row of zeros as our answer.      1111
    x 1011
      1111
     11110
    000000   
Now we multiply the last digit. Since this is the fourth multiplication, we record three zeros in the answer.      1111
    x 1011
      1111
     11110
    000000
       000   
The last digit is 1 so we record the top number below.      1111
    x 1011
      1111
     11110
    000000
   1111000   
This gives us our answer of 101001012.      1111
    x 1011
      1111
     11110
    000000
   1111000
  10100101  

We can divide the binary number 1000012 by 1102 using the same technique as long division in the decimal system. 

First, we need to find the smallest part of the dividend that is greater than the divisor 1102. Since our divisor has three digits, we begin by examining the first three digits of the dividend. 1002 is less than 1102 so we need to add another digit from our dividend.           
 110|100001   
Next we try the first four digits of the dividend. Since 10002 is greater than 1102 we know we can do our division.           
 110|100001
    
1102 divides 10002 one time, so we write a 1 as the first digit of our quotient, copy the divisor below the dividend, and subtract using the borrow method.        1  
 110|100001
      110
       10   
Now we bring down the next digit of our dividend and write it beside our remainder. Then we check to see if this new number is greater than or equal to our divisor.        1  
 110|100001
      110
       100   
1002 is less than 1102 so we write a 0 in the quotient and add another digit to our reminder.        10 
 110|100001
      110
       1001   
10012 is greater than 1102 so we write a 1 in the quotient and subtract 1102 from 10012.        101
 110|100001
      110
       1001
        110
         11   
Note that we still have a remainder after considering all the digits of the dividend. This means our answer will include a fraction. To finish our problem we need to mark the radix point and append a zero to the dividend.        101. 
 110|100001.0
      110
       1001
        110
         11   
Now we bring down the extra zero and compare the remainder with our divisor. Notice we ignore the radix point in our comparison. 1102 equals 1102 so we write another 1 in the quotient and subtract. This completes our division because we have no more digits in the dividend and no remainder.        101.1
 110|100001.0
      110
       1001
        110
         110
         110
           0  

1’s Complement Algorith:-
Representing a signed number with 1's complement is done by changing all the bits that are 1 to 0 and all the bits that are 0 to 1. Reversing the digits in this way is also called complementing a number. Let's look at an example in 4-bit arithmetic. How can we represent the number -510 in 1's complement?
First, we write the positive value of the number in binary. 0101 (+5)   
Next, we reverse each bit of the number so 1's become 0's and 0's become 1's 1010 (-5)  
Here is a quick summary of how to find the 1's complement representation of any decimal number x. 
If x is positive, simply convert x to binary.
If x is negative, write the positive value of x in binary
Reverse each bit.
2’s Complement Algorith:-
Representing a signed number with 2's complement is done by adding 1 to the 1's complement representation of the number. To illustrate, let's look at the same example we used for 1's complement. How can we represent the number -510 in 2's complement using 4-bits?
First, we write the positive value of the number in binary. 0101 (+5)   
Next, we reverse each bit to get the 1's complement. 1010   
Last, we add 1 to the number. 1011 (-5)  

Here is a quick summary of how to find the 2's complement representation of any decimal number x. Notice the first three steps are the same as 1's complement. 
If x is positive, simply convert x to binary.
If x is negative, write the positive value of x in binary
Reverse each bit.
Add 1 to the complemented number.
Signed Number Subtraction:-
Now that we can represent signed numbers with 1's and 2's complement, let's see how they will help us convert our subtraction problems to addition. Consider the problem of subtracting 110 from 710. We can also think of this problem as adding -110 to 710. In order to do this, we will use the following steps:
Convert 110 to -110 with either 1's or 2's complementation.
Add -110 to 710.
Adjust our answer.
You may be wondering why we need step three. The answer is that sometimes the sum in step two will exceed the number of bits in our representation. This is called overflow, and we handle the extra bit differently in 1's and 2's complement. In 1's complement, we add the overflow bit to our sum to obtain the final answer. In 2's complement, we simply discard the extra bit to obtain the final answer.
The links below illustrate how to solve our problem using 1's complement and 2's complement arithmetic. Click on one of the links below or use the navigation bar to work through both examples in order.
One's Complement   |  Two's Complement 
One's Complement :-
Let's consider how we would solve our problem of subtracting 110 from 710 using 1's complement. 
First, we need to convert 00012 to its negative equivalent in 1's complement.    0111    (7)
  - 0001  - (1)   
To do this we change all the 1's to 0's and 0's to 1's. Notice that the most-significant digit is now 1 since the number is negative.  0001 -> 1110   
Next, we add the negative value we computed to 01112. This gives us a result of 101012.    0111    (7)
  + 1110  +(-1)
   10101    (?)   
Notice that our addition caused an overflow bit. Whenever we have an overflow bit in 1's complement, we add this bit to our sum to get the correct answer. If there is no overflow bit, then we leave the sum as it is.    0101
  +    1
    0110    (6)   
This gives us a final answer of 01102 (or 610).    0111    (7)
  - 0001  - (1)
    0110    (6)  
Now let's look at an example where our problem does not generate an overflow bit. We will subtract 710 from 110 using 1's complement.
First, we state our problem in binary.    0001    (1)
  - 0111  - (7)   
Next, we convert 01112 to its negative equivalent and add this to 00012.    0001    (1)
  + 1000  +(-7)
    1001    (?)   
This time our results does not cause an overflow, so we do not need to adjust the sum. Notice that our final answer is a negative number since it begins with a 1. Remember that our answer is in 1's complement notation so the correct decimal value for our answer is -610 and not 910.    0001    (1)
  + 1000  +(-7)
    1001   (-6)  
The animation below demonstrates how to subtract the 5-bit binary numbers 011012 and 010012 using 1's complement representation
Summary:-
Let's review the steps for subtracting x from y with an n-bit 1's complement representation.
Negate x using 1's complement. 
Reverse all the bits in x to form -x.
Add -x and y.
If the sum exceeds n bits, add the extra bit to the result.
If the sum does not exceed n bits, leave the result as it is.
Two's Complement :-
Now let's consider how we would solve our problem of subtracting 110 from 710 using 2's complement. 
First, we need to convert 00012 to its negative equivalent in 2's complement.    0111    (7)
  - 0001  - (1)   
To do this we change all the 1's to 0's and 0's to 1's and add one to the number. Notice that the most-significant digit is now 1 since the number is negative.  0001 -> 1110
             1
          1111   
Next, we add the negative value we computed to 01112. This gives us a result of 101102.    0111    (7)
  + 1111  +(-1)
   10110    (?)   
Notice that our addition caused an overflow bit. Whenever we have an overflow bit in 2's complement, we discard the extra bit. This gives us a final answer of 01102 (or 610).    0111    (7)
  - 0001  - (1)
    0110    (6)  

The animation below demonstrates how to subtract the 5-bit binary numbers 011012 and 010012 using 2's complement representation
Let's review the steps for subtracting x from y with an n-bit 2's complement representation.
Negate x using 2's complement. 
Reverse all the bits in x.
Add 1 to form -x.
Add -x and y.
Discard any bits greater than n.
Now go back and compare these steps with the steps for 1's complement subtraction. Notice that with 1's complement, you must check for an overflow bit each time you perform a subtraction. If the result has an overflow, you need to add the extra bit to your result to obtain the correct answer. However, with 2's complement, we only need to ignore this extra bit. No other computations are required to find the correct answer. This makes 2's complement a more efficient way of representing signed numbers and performing binary subtraction
Continued.......

       


 My main Blog--- click to watch my full  Blog-------  (http://www.Buyonlineprojects.blogspot.com)