可以捕获 16 位二进制中与整数值对应的第 i 位的逻辑表达式?
Logical expression that can catch i'th bit in 16-bit binary that corresponds to an integer value?
我目前正在处理 Nand2Tetris 课程的最后一个项目。
我一直在试图弄清楚逻辑表达式在实现按位计算时是如何工作的。
当 twoToThe[i] == 0
(5 在二进制中是 101,因此 twoToThe[0] == 0
和 twoToThe[2] == 0
是 False
并且是正确答案)
但是为什么在 twoToThe[i] == 1
?
时不显示完全相反的结果
我认为 return True
twoTwoThe[0] == 1
和 twoTwoThe[2] == 1
下面是代码
# Calculating x * y
x = 3 # doesn't matter here
y = 5
# Array that holds i'th bit's integer values in 16-bit binary (e.g. twoToThe[4] = 16)
twoToThe = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768]
## Set twoToThe[i] == 0
print(y & twoToThe[0] == 0) # False (101 is 5, so False is right)
print(y & twoToThe[1] == 0) # True
print(y & twoToThe[2] == 0) # False (101 is 5, so False is right)
print(y & twoToThe[3] == 0) # True
print(y & twoToThe[4] == 0) # True
print(y & twoToThe[5] == 0) # True
...
print(y & twoToThe[15] == 1) # False
## Set twoToThe[i] == 1
print(y & twoToThe[0] == 1) # True
print(y & twoToThe[1] == 1) # False
print(y & twoToThe[2] == 1) # False (I expected True here, but it's false)
print(y & twoToThe[3] == 1) # False
print(y & twoToThe[4] == 1) # False
print(y & twoToThe[5] == 1) # False
...
print(y & twoToThe[15] == 1) # False
y & twoToThe[0]
表达式的计算结果为一个新整数(作为 5
的位和 twoToThe[i]
的位之间的按位与运算的结果),而不是布尔值正如我想你所期待的。检查一下:
y = 5
# Array that holds i'th bit's integer values in 16-bit binary (e.g. twoToThe[4] = 16)
twoToThe = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768]
print(type(y & twoToThe[0])) #int
print(y & twoToThe[0]) # 1
print(y & twoToThe[1]) # 0
print(y & twoToThe[2]) # 4
print(y & twoToThe[3]) # 0
print(y & twoToThe[4]) # 0
print(y & twoToThe[5]) # 0
我目前正在处理 Nand2Tetris 课程的最后一个项目。
我一直在试图弄清楚逻辑表达式在实现按位计算时是如何工作的。
当 twoToThe[i] == 0
(5 在二进制中是 101,因此 twoToThe[0] == 0
和 twoToThe[2] == 0
是 False
并且是正确答案)
但是为什么在 twoToThe[i] == 1
?
时不显示完全相反的结果
我认为 return True
twoTwoThe[0] == 1
和 twoTwoThe[2] == 1
下面是代码
# Calculating x * y
x = 3 # doesn't matter here
y = 5
# Array that holds i'th bit's integer values in 16-bit binary (e.g. twoToThe[4] = 16)
twoToThe = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768]
## Set twoToThe[i] == 0
print(y & twoToThe[0] == 0) # False (101 is 5, so False is right)
print(y & twoToThe[1] == 0) # True
print(y & twoToThe[2] == 0) # False (101 is 5, so False is right)
print(y & twoToThe[3] == 0) # True
print(y & twoToThe[4] == 0) # True
print(y & twoToThe[5] == 0) # True
...
print(y & twoToThe[15] == 1) # False
## Set twoToThe[i] == 1
print(y & twoToThe[0] == 1) # True
print(y & twoToThe[1] == 1) # False
print(y & twoToThe[2] == 1) # False (I expected True here, but it's false)
print(y & twoToThe[3] == 1) # False
print(y & twoToThe[4] == 1) # False
print(y & twoToThe[5] == 1) # False
...
print(y & twoToThe[15] == 1) # False
y & twoToThe[0]
表达式的计算结果为一个新整数(作为 5
的位和 twoToThe[i]
的位之间的按位与运算的结果),而不是布尔值正如我想你所期待的。检查一下:
y = 5
# Array that holds i'th bit's integer values in 16-bit binary (e.g. twoToThe[4] = 16)
twoToThe = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768]
print(type(y & twoToThe[0])) #int
print(y & twoToThe[0]) # 1
print(y & twoToThe[1]) # 0
print(y & twoToThe[2]) # 4
print(y & twoToThe[3]) # 0
print(y & twoToThe[4]) # 0
print(y & twoToThe[5]) # 0