为什么 Matlab 的 `bitxor(1000,10)` 给出 994?
Why does Matlab's `bitxor(1000,10)` gives 994?
我知道bitxor
是逐位加模2,所以bitxor(1000,10)
不是应该给1010吗?为什么给我994?
问题是 1000 和 10 不是二进制数。
在十进制中,0b1000 将是 8,而 0b10 将是 2。
试试这个:
bitxor(8,2)
ans = 10
现在你可能认为它是错误的,但 ans 也不是二进制,它的十进制 10 等于二进制 1010。
编辑:让它按照你想要的方式工作试试这个:
dec2bin(bitxor(bin2dec('1000'), bin2dec('10'))
ans = 1010
我知道bitxor
是逐位加模2,所以bitxor(1000,10)
不是应该给1010吗?为什么给我994?
问题是 1000 和 10 不是二进制数。 在十进制中,0b1000 将是 8,而 0b10 将是 2。 试试这个:
bitxor(8,2)
ans = 10
现在你可能认为它是错误的,但 ans 也不是二进制,它的十进制 10 等于二进制 1010。
编辑:让它按照你想要的方式工作试试这个:
dec2bin(bitxor(bin2dec('1000'), bin2dec('10'))
ans = 1010