Divide by 2
To convert decimal to binary.
While number > 0
Divide number by 2
Set aside remainder
End_While
Read string of remainders in reverse order.
|
To convert binary to decimal.
Multiply by 2 (shift left) and add
Sum = 0.
Take left most bit and add to sum.
Discard left most bit.
While more bits
Multiply sum by 2 (shift left)
Add left most bit to sum.
Discard left most bit.
End_While
|
477
238 1 LSB number of 1s
119 0 number of 2s
59 1 number of 4s
29 1 number of 8s
14 1 number of 16s
7 0 number of 32s
3 1 number of 64s
1 1 number of 128s
0 1 MSB number of 256s
When using divde by 2,
Remember to invert column
111011101
# A word on padding.
When working with unsigned binary, it is unneccesary to left pad with zeros
unless you need to be aware of working storage size for other reasons..
e.g 0000 0001 1101 1101 # number above displayed as 32 bit word.
|
Initial Resulting
value sum
0
1 0*2 + 1 = 1
1 1*2 + 1 = 3
1 3*2 + 1 = 7
0 7*2 + 0 = 14
1 14*2 + 1 = 29
1 29*2 + 1 = 59
1 59*2 + 1 = 119
0 119*2 + 0 = 238
1 238*2 + 1 = 477
|