1. Convert decimal to binary
Calculation method:
- Divide a decimal number by 2 to get the quotient and remainder (remainder is 0 or 1)
- Then the obtained quotient continues to be divided by 2 to obtain the quotient and remainder until the quotient is 0
- Finally, sort these remainders in reverse order
Note: The remainder of dividing 1 by any positive integer greater than 1 is 1
// 十进制数256 转二进制为100000000
256/2=128 余0
128/2=64 余0
64/2=32 余0
32/2=16 余0
16/2=8 余0
8/2=4 余0
4/2=2 余0
2/2=1 余0
1/2=0 余1 // 1除以任何一个大于1的正整数的余数都是1
将余数倒序排序就是二进制为:100000000
// 十进制数107转二进制为1101077
107/2=53 余1
53/2=26 余1
26/2=13 余0
13/2=6 余1
6/2=3 余0
3/2=1 余1
1/2=0 余1 // 1除以任何一个大于1的正整数的余数都是1
将余数倒序排序就是二进制为:1101011
2. Convert binary to decimal
Calculation method:
- Counting from the last digit, multiply by 2 to the nth power
- The last digit is 2 to the 0th power, the penultimate digit is 2 to the power of 1, and the power of 2 is always + 1 until the first digit
- Finally add up all calculated sums
Note: Any non-zero number to the power of 0 is equal to 1
// 二进制100000000 转为十进制256
// 从最后一位开始算,为2的0次方,之后每次乘以2的上一次次方+1
0*2的0次方=0 // 任何非零数的0次方都等于1
0*2的1次方=0
0*2的2次方=0
0*2的3次方=0
0*2的4次方=0
0*2的5次方=0
0*2的6次方=0
0*2的7次方=0
1*2的8次方=256 // 等于1*2*2*2*2*2*2*2*2=256
// 最后相加 256+0+0+0+0+0+0+0+0=256
// 二进制1101011转为十进制107
// 从最后一位开始算,为2的0次方,之后每次乘以2的上一次次方+1
1*2的0次方=1 // 任何非零数的0次方都等于1
1*2的1次方=2
0*2的2次方=0
1*2的3次方=8
0*2的4次方=0
1*2的5次方=32
1*2的6次方=64 // 等于1*2*2*2*2*2*2=64
// 最后相加 64+32+0+8+0+2+1=107