question about converting base 10 to binary

Thread Starter

fishguts

Joined Sep 3, 2007
22
Hello I have one question. We have started to learn how to convert decimal to binary and most of it is easy except one problem i have. I need to convert 22.5625 into decimal. I can convert 22 fine but its the .5625 that I do not know how to deal with. Can anyone point me in the right direction. Thanks
 

Papabravo

Joined Feb 24, 2006
13,936
To the left of the binary point you have positive powers of 2. To the right of the binary point you have neagative powers of 2. The first position to the right of the binary point represents 1/2 or 0.500. Subtracting that from, .5625 leaves 0.0625 which all us mechanics and builders recognize as the decimal equivalent of 1/16 or 2^-4. So

0.5625(base 10) = 0.1001(base 2)

Get the picture?
 

GS3

Joined Sep 21, 2007
408
I have spent a bit of time giving this some thought and here are my conclusions:

- The integral part of a decimal number will always be represented by an integral binary (with no fractional part) while the fractional part of a decimal number will always be represented by a fractional part of a binary number (with no integral part). An integral number will be integral in any integral base and a fractional number will be fractional in any integral base. This means we can split a decimal number into its integral and fractional parts, convert them separately to binary and add them up again in binary. This means we can forget about the integral part and concentrate on converting the fractional part.

- Some decimal fractions may convert into binary exactly (like .5625) but others may not (like .5624) and we may get an infinite number of binary digits in the binary fraction.

- One way of converting a decimal fraction to binary fraction is to first multiply it by a power of 2, then convert to binary and then divide by the same power of 2 (which just means displacing the binary point to the left).

Examples:

- Convert .5625 to binary.
- Multiply .5625 * 2^4 = 9
- Convert 9 to binary = 1001
- Divide 1001 / 2^4 = .1001

- Convert .5625 to binary
- Multiply .5625 * 2^10 = 576
- Convert 576 to binary = 1001000000
- Divide 1001000000 / 2^10 = .1001000000
(Same thing, more precision)

- Convert .5624 to binary
- Multiply .5624 * 2^20 = 589719.1424
- Convert 589719 to binary = 10001111111110010111
- Divide 10001111111110010111 / 2^20 = .10001111111110010111
- (The more precision you want, the higher the power of 2 to use. Use the same power as the number of significant digits you want in the result.)


- Convert 165650.435 to binary
- 165650 = 101000011100010010
- Multiply .435 by 2^20 = 456130.56
- Convert 456131 to binary: 1101111010111000010
- Divide by 2^20: .01101111010111000010
- We now add integral and fractional parts:
The answer is 101000011100010010.01101111010111000010
which we can round off to:
101000011100010010.01101111010111
Converting this back to decimal we get
165650.43499755859375

Roughly three decimal digits are equivalent in precision to 10 binary digits so, if we truncate to ten fractional digits we get.
101000011100010010.0110111101 = 165650.4345703125

So it is a good rule of thumb to use 10 binary digits for every 3 decimal digits.

(I hope not too many mistakes crept in all the foregoing.)
 

ambreen

Joined Mar 5, 2008
6
no actually i want to generate binary seq. of 10^5 bits based on distribution by lempel-ziv algorithim using Matlab.I need to write code for its phrasing & codwords in dictionary contents & then for compression rate using matlab it should run
 

Dave

Joined Nov 17, 2003
6,970
Please do not litter the forums with requests for help with the same question. Keep this in your active thread.

Thank you.

Dave
 
Top