Divide each bit by 2 and modulo 2.
It is the reverse operation process of converting binary to decimal, except for two digits, that is, the number of that digit.
After the remainder, the remainder cannot enter one. Every bit is calculated, and finally the high bit is obtained.
Ideas:
The integer part adopts the method of "dividing by 2 to take the remainder and arranging it in reverse order". The specific method is: divide the decimal integer by 2 to get a quotient and remainder; Divide the quotient by 2 to get a quotient and a remainder, and so on until the quotient is less than 1. Then, the first obtained remainder is used as the low-order significant bit of the binary number, and the last obtained remainder is used as the high-order significant bit of the binary number, which are arranged in turn.
Its principle is that less than two are not enough to carry away.
When calculating decimal, the level is opposite to each digital value, and the principle is the same.
You can refer to this code:
Define binary (decimal):
temp = []
Result =''
Although December:
Quo = December% 2
dec = dec // 2
Temporary attachment (status quo)
When the temperature:
result += str(temp.pop())
return result
Print (Dec2Bin(62))
One thing can be regarded as a whole. It's not in place.