[英]Python bitmask doesn't give expected output
Im puting a bitmask over a register read value and execute a logical and on the value.我在寄存器读取值上放置了一个位掩码,并对值执行逻辑和。 what i expect is:
clear_reg_val: 00000000000000000100100000000000
My actual output is:我期望的是:
clear_reg_val: 00000000000000000100100000000000
我的实际输出是:
# reg_value is: 00000000000000000100100000001100
# mask_inverted: 11111111111111111111100000000011
# clear_reg_val: 00000000000000000100100000001000
# reg_value is: 00000000000000000100100000001100
# mask_inverted: 11111111111111111111100000000011
# clear_reg_val: 00000000000000000100100000001000
So i don't know why bit 4 of clear_reg_val becomes a '1' instead of a '0' The Python code of the function is added in the screenshot below:所以我不知道为什么 clear_reg_val 的第 4 位变为 '1' 而不是 '0' 下面的屏幕截图中添加了该函数的 Python 代码:
The error is in the way you are creating the inverted mask.错误在于您创建反转掩码的方式。
You are doing it as a string operation, and converting the string back to an integer using int
.您将其作为字符串操作执行,并使用
int
将字符串转换回整数。 You forgot to specify base=2
and the string is treated as a decimal number.您忘记指定
base=2
并且字符串被视为十进制数。
The proper way to invert a 32 bit value is:反转 32 位值的正确方法是:
MASK32 = 2**32-1
invx = ~x & MASK32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.