Friday, March 29, 2013

How to Convert from Decimal to Binary



  • How to Convert from Decimal to Binary


The decimal (base ten) numeral system has ten possible values (0,1,2,3,4,5,6,7,8, or 9) for each place-value. In contrast, the binary (base two) numeral system has two possible values, often represented as 0 or 1, for each place-value.

http://www.wikihow.com/Convert-from-Decimal-to-Binary



  • Converting Decimal Fractions to Binary


In fact, there is a simple, step-by-step method for computing the binary expansion on the right-hand side of the point. We will illustrate the method by converting the decimal value .625 to a binary representation..

Step 1: Begin with the decimal fraction and multiply by 2. The whole number part of the result is the first binary digit to the right of the point.

Because .625 x 2 = 1.25, the first binary digit to the right of the point is a 1.
So far, we have .625 = .1??? . . . (base 2) .

Step 2: Next we disregard the whole number part of the previous result (the 1 in this case) and multiply by 2 once again. The whole number part of this new result is the second binary digit to the right of the point. We will continue this process until we get a zero as our decimal part or until we recognize an infinite repeating pattern.

Because .25 x 2 = 0.50, the second binary digit to the right of the point is a 0.
So far, we have .625 = .10?? . . . (base 2) .

Step 3: Disregarding the whole number part of the previous result (this result was .50 so there actually is no whole number part to disregard in this case), we multiply by 2 once again. The whole number part of the result is now the next binary digit to the right of the point.

Because .50 x 2 = 1.00, the third binary digit to the right of the point is a 1.
So now we have .625 = .101?? . . . (base 2) .

Step 4: In fact, we do not need a Step 4. We are finished in Step 3, because we had 0 as the fractional part of our result there.

Hence the representation of .625 = .101 (base 2) .

You should double-check our result by expanding the binary representation.






  • Infinite Binary Fractions


the binary representation of the decimal fraction 1/10 is, in fact, infinite.Step 1: Begin with the decimal fraction and multiply by 2. The whole number part of the result is the first binary digit to the right of the point.

Because .1 x 2 = 0.2, the first binary digit to the right of the point is a 0.
So far, we have .1 (decimal) = .0??? . . . (base 2) .

Step 2: Next we disregard the whole number part of the previous result (0 in this case) and multiply by 2 once again. The whole number part of this new result is the second binary digit to the right of the point. We will continue this process until we get a zero as our decimal part or until we recognize an infinite repeating pattern.

Because .2 x 2 = 0.4, the second binary digit to the right of the point is also a 0.
So far, we have .1 (decimal) = .00?? . . . (base 2) .

Step 3: Disregarding the whole number part of the previous result (again a 0), we multiply by 2 once again. The whole number part of the result is now the next binary digit to the right of the point.

Because .4 x 2 = 0.8, the third binary digit to the right of the point is also a 0.
So now we have .1 (decimal) = .000?? . . . (base 2) .

Step 4: We multiply by 2 once again, disregarding the whole number part of the previous result (again a 0 in this case).

Because .8 x 2 = 1.6, the fourth binary digit to the right of the point is a 1.
So now we have .1 (decimal) = .0001?? . . . (base 2) .

Step 5: We multiply by 2 once again, disregarding the whole number part of the previous result (a 1 in this case).

Because .6 x 2 = 1.2, the fifth binary digit to the right of the point is a 1.
So now we have .1 (decimal) = .00011?? . . . (base 2) .

Step 6: We multiply by 2 once again, disregarding the whole number part of the previous result. Let's make an important observation here. Notice that this next step to be performed (multiply 2. x 2) is exactly the same action we had in step 2. We are then bound to repeat steps 2-5, then return to Step 2 again indefinitely. In other words, we will never get a 0 as the decimal fraction part of our result. Instead we will just cycle through steps 2-5 forever. This means we will obtain the sequence of digits generated in steps 2-5, namely 0011, over and over. Hence, the final binary representation will be.

.1 (decimal) = .00011001100110011 . . . (base 2) .
The repeating pattern is more obvious if we highlight it in color as below:

.1 (decimal) = .00011001100110011 . . . (base 2) .

http://cs.furman.edu/digitaldomain/more/ch6/dec_frac_to_bin.htm





  • We can construct binary fractions using a binary "point", in the same was as we do with decimal:


101.101
||| |||
||| ||2-3 (1/8)
||| |2-2  (1/4)
||| 2-1   (1/2)
||20     (1)
|21       (2)
        22        (4)
And so the above number represents 4 + 1 + 1/2 + 1/8 = 5 5/8 (5.625)

http://turing.cs.camosun.bc.ca/COMP166/resources/floatingpoint.html




  • Binary Numbering


0.101

= 0 X 20 + 1 X 2-1 + 0 X 2-2 + 1 X 2-3

= 0 + 1/2 + 0 + 1/8

= 4/8 + 1/8

= 5/8


http://cs.furman.edu/digitaldomain/more/binary/bin1.html




  • 1. Which of the following is the binary representation of 4 5/8?


A. 100.11

ANSWER: D


https://docs.google.com/viewer?a=v&q=cache:00oarkgsJgkJ:site.iugaza.edu.ps/lalsaedi/files/2010/02/Chapter_One.doc+&hl=tr&pid=bl&srcid=ADGEESjzXurOtyhphMb3gIqn3eIZoYb-rzzeM8VZfuwgjEkeUT3qz6Owdai6A5spFoV9glErHyRAj6EarlH-34TTNh2pLRWHfuR1FFwSmZMgIaTCnS1Frc-dZb873BWlEwdPq7zcNzVB&sig=AHIEtbRxYL1hEMxqJSLu9JaEE3mZTjp23w




  • Example: Convert decimal 44 to binary.


DIVIDE
44 / 2 = 22 remainder = 0
22 / 2 = 11 remainder = 0
11 / 2 =  5 remainder = 1
 5 / 2 =  2 remainder = 1
 2 / 2 =  1 remainder = 0
 1 / 2 =  0 remainder = 1


REVERSE THE ORDER OF REMAINDERS
The bits, in the order they were generated is 001101 Reversing the order of bits we get 101100. Properly padded with leading zeroes to fill out one byte, we get 01011000

http://www.inetdaemon.com/tutorials/basic_concepts/number_systems/binary/conversion.shtml




  • Example.  Convert the decimal number 29410 into its binary number equivalent.


Number 294 Dividing each number by "2" gives a result plus a remainder. The binary result is obtained by placing the remainders in order with the least significant bit (LSB) being at the top and the most significant bit (MSB) being at the bottom.
divide by 2
result 147 remainder 0  (LSB)
divide by 2
result 73 remainder 1
divide by 2
result 36 remainder 1
divide by 2
result 18 remainder 0
divide by 2
result 9 remainder 0
divide by 2
result 4 remainder 1
divide by 2
result 2 remainder 0
divide by 2
result 1 remainder 0
divide by 2
result 0 remainder 1  (MSB)

Then, the decimal to binary conversion gives the decimal number 29410 equivalent of 1001001102 in binary, reading from right to left.

http://www.electronics-tutorials.ws/binary/bin_2.html

The shift operators


The shift operators allow programmers to adjust an integer by shifting all of its bits to the left or the right.

The following diagram shows the affect of shifting a value to the left by one digit
  00001111  =  15
SHIFT LEFT
  00011110  =  30


Compound Assignment Operators
The shift operators have equivalents for compound assignment
These are used by adding an equals sign to the operator and using the number of bits to shift by as the operand.


int value = 240;             // 11110000

value >>= 2;      // Result = 60
value <<= 1;      // Result = 120


http://www.blackwasp.co.uk/CSharpShiftOperators.aspx