在sybase中使用&、~进行逻辑计算
logical calculation using &, ~ in sybase
我正在执行以下语句。
SELECT 10 & ~3 。这给出了输出 8
SELECT 10 & ~5 。这给出了输出 10
SELECT 10 & ~2 。这给出了输出 8
谁能解释一下这背后的逻辑?
这是位运算,~是逆运算。
3 is the same as 0011 in binary, and inverse of that is 1100
2 is the same as 0010 in binary, and inverse of that is 1101
5 is the same as 0101 in binary, and inverse of that is 1010
10 is the same as 1010 in binary.
10 & ~3 => 1010 & 1100 = 1000 => 8
10 & ~5 => 1010 & 1010 = 1010 => 10
10 & ~2 => 1010 & 1101 = 1000 => 8
按位是位对位
你也可以用十进制来思考,但你也必须用二进制来思考(序列 1、2、4、8、16、32、64、128...)。 10 个由“8 | 2”组成,3 个由“1 | 2”组成。 3 的倒数除“1 | 2”外都是。 10 和 3 之间的公共部分是 2,因此您将拥有 10 中不在 3 中的所有部分,从而得到 8。
我正在执行以下语句。
SELECT 10 & ~3 。这给出了输出 8 SELECT 10 & ~5 。这给出了输出 10 SELECT 10 & ~2 。这给出了输出 8
谁能解释一下这背后的逻辑?
这是位运算,~是逆运算。
3 is the same as 0011 in binary, and inverse of that is 1100
2 is the same as 0010 in binary, and inverse of that is 1101
5 is the same as 0101 in binary, and inverse of that is 1010
10 is the same as 1010 in binary.
10 & ~3 => 1010 & 1100 = 1000 => 8
10 & ~5 => 1010 & 1010 = 1010 => 10
10 & ~2 => 1010 & 1101 = 1000 => 8
按位是位对位
你也可以用十进制来思考,但你也必须用二进制来思考(序列 1、2、4、8、16、32、64、128...)。 10 个由“8 | 2”组成,3 个由“1 | 2”组成。 3 的倒数除“1 | 2”外都是。 10 和 3 之间的公共部分是 2,因此您将拥有 10 中不在 3 中的所有部分,从而得到 8。