>> 在 Python 中是什么意思?
What Does >> mean in Python?
有人发给我这个等式,但我不明白它是什么意思。
result = ((~c1) >> 1) & 0x0FFFFFF
它与从 wiegand 转换二进制文件有关 reader。
Python中的>>
运算符是按位右移。如果你的数字是 5,那么它的二进制表示是 101。当右移 1 时,它变成 10,或 2。它基本上是除以 2,如果结果不准确则向下舍入。
您的示例是按位补码 c1
,然后将结果右移 1 位,然后屏蔽除 24 个低位以外的所有位。
这条语句的意思是:
result = # Assign a variable
((~c1) # Invert all the bits of c1
>> 1) # Shift all the bits of ~c1 to the right
& 0x0FFFFFF; # Usually a mask, perform an & operator
~
运算符执行 two's complement.
示例:
m = 0b111
x = 0b11001
~x == 0b11010 # Performs Two's Complement
x >> 1#0b1100
m == 0b00111
x == 0b11001 # Here is the "and" operator. Only 1 and 1 will pass through
x & m #0b 1
有人发给我这个等式,但我不明白它是什么意思。
result = ((~c1) >> 1) & 0x0FFFFFF
它与从 wiegand 转换二进制文件有关 reader。
Python中的>>
运算符是按位右移。如果你的数字是 5,那么它的二进制表示是 101。当右移 1 时,它变成 10,或 2。它基本上是除以 2,如果结果不准确则向下舍入。
您的示例是按位补码 c1
,然后将结果右移 1 位,然后屏蔽除 24 个低位以外的所有位。
这条语句的意思是:
result = # Assign a variable
((~c1) # Invert all the bits of c1
>> 1) # Shift all the bits of ~c1 to the right
& 0x0FFFFFF; # Usually a mask, perform an & operator
~
运算符执行 two's complement.
示例:
m = 0b111
x = 0b11001
~x == 0b11010 # Performs Two's Complement
x >> 1#0b1100
m == 0b00111
x == 0b11001 # Here is the "and" operator. Only 1 and 1 will pass through
x & m #0b 1