Justin's Words

Transfer decimal number to binary number

Transfer decimal number to its corresponding binary number in a quick way.

Let’s show some powers of two limited between 1 and 10:

Power Result
2^0 1
2^1 2
2^2 4
2^3 8
2^4 16
2^5 32
2^6 64
2^7 128
2^8 256
2^9 512
2^10 1024

And some decimal number to its corresponding binary number showed below:

Decimal Binary
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111

Taking 15 as example, let’s transfer 15 to binary number. Decompose 15 to some decimal numbers that equal to corresponding power of two. You are supposed to have a good memory of some powers of 2 in this step. 15 can be decomposed to 8 + 4 + 2 + 1. 8 is 2 power 3, which means the binary number of 15 consists of four digits and its fourth digit (also known as the position in the highest position, counted forwards) is 1. 4 is 2 power 2, so the third digit of the binary number of 15 is 1. 2 is 2 power 1, so the second digit of the binary number of 15 is 1. Because 15 is an odd number, the first digit of its corresponding binary number should be 1. Thus 1111 is the binary number of 15. Let’s try some bigger number like 367. 367 can be decomposed to 256 + 64 + 32 + 8 + 4 + 2 + 1, that is 2^8 + 2^6 + 2^5 + 2^3 + 2^2 + 2^1 + 2^0. Therefor 101101111 is the binary number of 367. You can verify it in Python REPL:

1
2
>>> bin(367)
'0b101101111'