- Remember long division
263 R1
------
2|527
4
-
12
12
--
7
6
-
1
- We need to know both the answer and the remainder.
- The algorithm to convert from base 10 to base 2, or any other base really is
- Let x be the number to be converted.
- Let a be the final answer
- Let b be the base we are converting to.
- While x is larger than the base
- x/b = n R r
- let a = r || a (|| is concatenation)
- let x = n
- a = x || a (if x is not 0)
- Example:
Convert 527 to binary
x = 527
b = 2
a = ""
527/2 = 263 R 1 a = 1
263/2 = 131 R 1 a = 11
131/2 = 65 R 1 a = 111
65/2 = 32 R 1 a = 1111
32/2 = 16 R 0 a = 01111
16/2 = 8 R 0 a = 001111
8/2 = 4 R 0 a = 0001111
4/2 = 2 R 0 a = 00001111
2/2 = 1 R 0 a = 000001111
1 is less than 2 a = 1000001111
- Example: Convert 90 to binary
x = 90
b = 2
a = ""
90/2 = 45 r 0 0
45/2 = 22 r 1 10
22/2 = 11 r 0 010
11/2 = 5 r 1 1010
5/2 = 2 r 1 11010
2/0 = 1 r 0 011010
1 1011010
- I tend to use a somewhat modified version of this. I do division upside down, then read the result bottom to top.
2|90 0
2|45 1
2|22 0
2|11 1
2|5 1
2|2 0
1