Denary to binary · 十进制转二进制
Denary to binary
Go the other way: from a denary number to its binary digits.
Divide by 2 again and again. The remainder n % 2 is the next bit, and n // 2 is what is left to convert. The remainders come out right to left, so put each new bit at the front of the string. 0 needs its own answer, because the loop would never run.
十进制转二进制
反过来转换:把十进制数变成二进制各位。
一次又一次地除以 2。余数 n % 2 是下一位,n // 2 是剩下还要转换的部分。余数是从右往左得到的,所以要把每个新的位加在字符串前面。0 需要单独处理,因为循环一次也不会运行。
Write to_binary(n) that returns the binary digits of a whole number n (0 or more) as a string, without using bin(). 11 gives "1011" and 0 gives "0". · 编写 to_binary(n),把非负整数 n 的二进制各位作为字符串返回,不能使用 bin()。11 得到 "1011",0 得到 "0"。
Click Run to see the output here. · 点击“运行”查看此处输出。