For integer portion of a number, Divide by 2 and note remainder. Shifts right and reveals the next significant digit. works from least significant digit to most. Converting fractional portion of a number to binary by multiplying by 2. Shifts left to reveal next significant digit. works from most signficant digit to least. Representable precission determines when to stop. Example : Assume 8 bits of data storage.
Decimal.Decimal to binary.431 . .431 . 0 .862 0 1 .724 1 1 .448 1 0 .896 0 1 .792 1 1 .584 1 1 .168 1 0 .336 0 .01101110b |
  |
Binary to Decimal.Decimal using shift and subtract
0.01101110 sum= 0 0 * .5 + 0 = 0 1 * .25 + 0 = 0.25 1 * .125 + 0.25 = 0.375 0 * .0625 + 0.375 = 0.375 1 * .03125 + 0.375 = 0.40625 1 * .015625 + 0.40625 = 0.421875 1 * .0078125 + 0.421875 = 0.4296875 0 * .00390625 + 0.4296875 = 0.4296875 sum= 0.4296875 |
Note the difference between values 0.431 - 0.4296875 = .0013125 (.3% difference) |
.431 .431 - 0 * .5 = .431 .431 - 1 * .25 = .181 .181 - 1 * .125 = .056 .056 - 0 * .0625 = .056 .056 - 1 * .03125 = .02475 .02475 - 1 * .015625 = .009125 .009125 - 1 * .0078125 = .0013125 .013125 - 0 * .00390625 = .0013125 |