gpt4 book ai didi

python - &符号在打印功能中如何工作?

转载 作者:太空狗 更新时间:2023-10-30 02:51:09 25 4
gpt4 key购买 nike

print(2&3) 打印 2。但想了解它是如何工作的?

print(2&3)

输出:2

print(7&8)

输出:0

print("a"&"b")

Exception: TypeError: unsupported operand type(s) for &: 'str' and 'str'

请有人帮助理解 & 在 python 中是如何工作的。

最佳答案

&bitwise and operator : 相同等级的每一位都被求值,如果两个位都为1,则该等级的结果是 1

等级是数字的二进制表示中从右到左的位置。

0010
^^^^
||||
|||+-- rank 0
||+--- rank 1
|+---- rank 2
+----- rank 3

2 & 3 可以翻译成二进制形式 0010 & 0011 :

  0010
& 0011
------
0010

结果为 0010,十进制为 2

7 & 8 可以翻译成二进制形式 0111 & 1000 :

  0111
& 1000
------
0000

结果是0000,十进制为0

关于python - &符号在打印功能中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56967302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com