<<< Binary to Decimal Conversion | Index | Decimal to Binary Conversion Summary >>> |
Consider a decimal integer 105, which we would like to convert to a signed 8-bit quantity:
position: 76543210 value: ????????
105 a positive, odd number.
Our task is to find values of bits a7 through a0 as follows:
105 = a7*27 + a6*26 + a5*25 + a4*24 + a3*23 + a2*22 + a1*21 + a0*20
a7 is zero, because 105 is positive:
position: 76543210 value: 0???????
a0 is one, because 105 is odd:
position: 76543210 value: 0??????1
Subtracting one from 105 yields 104:
104 = a6*26 + a5*25 + a4*24 + a3*23 + a2*22 + a1*21
Dividing both sides of the equation by 2 yields
52 = a6*25 + a5*24 + a4*23 + a3*22 + a2*21 + a1*20
Since 52 is even, so a1 equals to zero:
position: 76543210 value: 0?????01
Next, we can iterate back to
step 5, if the number is odd, and subtract again last digit, or
step 6, if the number is even, and divide both sides of the equation by two.
In our example dividing both sides by 2 yields
26 = a6*24 + a5*23 + a4*22 + a3*21 + a2*20, therefore a2 = 0:
position: 76543210 value: 0????001
Dividing both sides by 2 yields
13 = a6*23 + a5*22 + a4*21 + a3*20, therefore a3 = 1:
position: 76543210 value: 0???1001
Subtracting one from both sides yields
12 = a6*23 + a5*22 + a4*21
Dividing both sides by 2 yields
6 = a6*22 + a5*21 + a4*20, therefore a4 = 0:
position: 76543210 value: 0??01001
Dividing both sides by 2 yields
3 = a6*21 + a5*20, therefore a5 = 1:
position: 76543210 value: 0?101001
And, finally,
1 = a6*20, therefore a6 = 1:
position: 76543210 value: 01101001
<<< Binary to Decimal Conversion | Index | Decimal to Binary Conversion Summary >>> |