如何描述这个函数背后的数学(How to describe the maths behind this function)
这是一个十进制到二进制转换器。 我需要帮助解释背后的数学,因为我不知道如何解释所有的移位等。
number = int(raw_input("Enter the Number:")) binary = '' while number > 0: binary = str(number % 2) + binary number >>=1 print(binary)This is a Decimal to Binary converter. I need help explaining the math behind is as I don't have a clue how to explain all the Shifts etc.
number = int(raw_input("Enter the Number:")) binary = '' while number > 0: binary = str(number % 2) + binary number >>=1 print(binary)最满意答案
循环构建一个表示二进制值的字符串。
str(number%2)找到该数字的最低位(0或1)。
binary = str(number%2)+ binary将该位添加到字符串二进制文件的左端
数字>> = 1删除了低位,因为我们已经完成了它
数字> 0继续,直到数字为0
The loop builds up a string which represents a binary value.
str(number % 2) finds the lowest bit of the number (either 0 or 1).
binary = str(number % 2) + binary adds the bit to the left end of string binary
number >>=1 removes the low bit now that we're done with it
while number > 0 continues until the number is 0
更多推荐
发布评论