如何描述这个函数背后的数学(How to describe the maths behind this function)

编程入门 行业动态 更新时间:2024-10-23 17:35:36
如何描述这个函数背后的数学(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

更多推荐

本文发布于:2023-08-04 22:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424212.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   数学   describe   maths   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!