What does shifting left by one bit do?
Multiplies by two. This is the same as with unsigned integers. Also, shifting one bit right is equivalent to division by two.
Decimal | Binary so far | |
---|---|---|
Start | 0.625 | 0. |
×2 | 1.250 | 0.1 |
.250 | 0.1 | |
×2 | 0.500 | 0.10 |
.500 | 0.10 | |
×2 | 1.000 | 0.101 |
Result | .000 | 0.101 |
Often you need to convert a decimal expression like 7.625 into a binary expression. To do this, first convert the whole number (7 in this case) to binary (111 in this case), append a binary point, and convert the decimal fraction to binary.
To convert a decimal fraction to Base 2: Repeatedly multiply the decimal fraction by two. After each multiplication, copy the bit (0 or 1) that moves into the one's place (the place just left of the binary point) to the right of the binary fraction, then erase it from the decimal fraction. Stop when the decimal fraction is zero. (Note: if this never happens, stop when you have enough bits in the binary fraction.)
For our example: 7.625 is 111.1012. In this case the conversion stopped when the decimal fraction changed to zero. This may not always happen.
(Thought Question: ) Why will no other digit but 0 or 1 move into the one's place? (Hint: consider the biggest possible decimal fraction).