l=(2<<2 | 3<<2)在Python中的意义

2024-04-20 10:22:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道上面的表达式在Python中是什么意思。做了一些谷歌搜索,但还是一无所获。l的值是12。你知道吗

谢谢你的帮助。你知道吗


Tags: 表达式
2条回答

它们是位操作。(Binary bitwise operationsShifting operations

如果你用二进制来表示数字,那就更容易理解了。你知道吗

>>> bin(2)
'0b10'
>>> bin(3)
'0b11'
>>> bin(2 << 2)  # << : Shift left
'0b1000'
>>> bin(3 << 2)
'0b1100'
>>> bin(2 << 2 | 3 << 2)
'0b1100'

>>> int('1100', 2)
12

这就是简单的位移位运算符和二进制OR,意思是

2 << 2 # shift 2 which is 0b00010 left by two positions

3 << 2 # shift 3 which is 0b00011 left by two positions

(2 << 2 | 3 << 2) take OR of these values

相关问题 更多 >