'How to convert a fraction to binary?
I don't know how to convert from a fraction to binary. When I search it, there has a solution shows that:
1 1
-- (dec) = ---- (bin)
10 1010
0.000110011...
-------------
1010 | 1.0000000000
1010
------
01100
1010
-----
0010000
1010
-----
01100
1010
-----
0010
I don't know how and why to do it.
Solution 1:[1]
Let's take a look at converting the decimal value of 0.625 to binary.
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) .
Decimal 1/10 converts to an infinite binary fraction.
In your question you said that 1/10 in decimal equals 1/1010 in binary. .1 (1/10) in decimal actually equals 0.00011001100110011... in binary.
Solution 2:[2]
Fractional value to Binary number conversion
The fraction value is multiplied by 2 and result has a decimal (1 or 0) and a fraction value.
take the faction value for step 1 operation.
The repeat process until the fraction value reached to 0.
collects a decimal value from bottom to up
fraction = .125
= .125 x 2 = 0.250 x 2 = 0.50 x 2 = 1.0fraction = 0.125 = 100
Results
given fraction value (base 10)= 0.125
into binary bits (base 2) = 0.100
Solution 3:[3]
In a binary weighted fraction each digit to the right of the decimal point is a power of (1/2) (or the negative of power of 2) smaller than the one to the left. The first rightward digit has a weight of 1/2, the second is 1/4, the third 1/8, and so on.
So a 0.111 (base-2) is:
1*(0.5) + 1*(0.25) + 1*(0.125) = 0.875
And a 0.0101 (base-2) is:
0*(0.5) + 1*(0.25) + 0*(0.125) + 1*(0.0625) = 0.3125
It's no different from binary integers, except we're just extending it to negative powers of 2 as we move right of the decimal point.
I hope that addresses at least part of your question.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | clcoder |
| Solution 3 | jongo45 |
