[英]Coincidence of bitwise and and bitwise or operators?
So, while playing with the bitwise AND and bitwise OR operators, I noticed the following:因此,在使用按位 AND 和按位 OR 运算符时,我注意到以下几点:
(a & b) + (a | b) = a + b
and there is a corresponding proof which mainly relies on the fact for any two bits x and y,并且有一个相应的证明,它主要依赖于任意两位 x 和 y 的事实,
(a,b) = (0,0) --> (a&b, a|b) = (0, 0) = (a, b)
(a,b) = (0,1) --> (a&b, a|b) = (0, 1) = (a, b)
(a,b) = (1,0) --> (a&b, a|b) = (0, 1) = (b, a)
(a,b) = (1,1) --> (a&b, a|b) = (1, 1) = (b, a)
Now, I was wondering - is this a mere coincidence, or are these bitwise operations actually used in this way?现在,我想知道——这仅仅是巧合,还是这些按位运算实际上是以这种方式使用的? I don't think that computers actually compute addition in this way, since it would be a recursive definition... but it seems too nice of a property to have been random!我不认为计算机实际上以这种方式计算加法,因为这将是一个递归定义......但它似乎太好了,不能随机!
AND and OR are idempotent and commutative: AND 和 OR 是幂等且可交换的:
a & a = a
a | a = a
a & b = b & a
a | b = b | a
They also absorb each other:它们还相互吸收:
a & (a | b) = a
a | (a & b) = a
Thus:因此:
a + b = (a | a) + (b & b)
a + b = (a | (a & b)) + (b & (a | b))
(a | a) + (b & b) = (a | (a & b)) + (b & (a | b))
a + b = (a & b) + (a | b)
Another way to see it is that, for two bits x
and y
, x + y = x | y
另一种看待它的方式是,对于两个位x
和y
, x + y = x | y
x + y = x | y
and x & y = 0
unless both bits are set, and then x & y
adds the missing bit to x | y
x + y = x | y
和x & y = 0
除非这两个位都设置,然后x & y
将丢失的位添加到x | y
x | y
. x | y
的。
when a and b value only 0 or 1 it is not surprising to have (a & b) + (a | b) == a + b
当a和b的值仅为 0 或 1 时, (a & b) + (a | b) == a + b
也就不足为奇了
having both a and b valuing 1 (a & b)
and (a | b)
value 1, so you do 1+1
on each side of ==
让a和b的值为 1 (a & b)
和(a | b)
值为 1,所以你在==
的每一边都做1+1
else (a & b)
is obviously 0 so you compare a|b
and a+b
while there is no possible carry on a+b
and in that case a and b being a boolean do use +
or |
else (a & b)
显然是 0 所以你比较a|b
和a+b
而没有可能进行a+b
并且在这种情况下a和b是 boolean 使用+
或|
is the same是一样的
Note when a and b value only 0 or 1 then a&b
equals a*b
in all cases请注意,当a和b的值仅为 0 或 1 时, a&b
b 在所有情况下都等于a*b
(a & b) + (a | b) = a + b
is indeed true in general, not just for 0 and 1. (a & b) + (a | b) = a + b
通常确实是正确的,而不仅仅是 0 和 1。
An other way to look at it, is that whenever the bits in a
and b
at a particular position are different (so one of them is zero and the other is one), then in (a & b) + (a | b)
the 0 is put on the left hand side of the +
and the 1 is put on the right hand side.另一种看待它的方式是,只要特定 position 中a
和b
中的位不同(因此其中一个为零,另一个为 1),则在(a & b) + (a | b)
0 放在+
的左侧, 1 放在右侧。 If the bits are the same then it doesn't make any difference.如果位相同,则没有任何区别。
It's like a more granular form of min(a, b) + max(a, b)
, at the bit level intead of the word level.它就像一个更细化的min(a, b) + max(a, b)
形式,在位级别而不是字级别。
Reordering bits like that has no effect on the sum.像这样重新排序位对总和没有影响。 Consider that both a
and b
are already sums, of the form a[0] + 2*a[1] + 4*a[2]...
.考虑到a
和b
都已经是和,形式a[0] + 2*a[1] + 4*a[2]...
。 a + b
is a bigger sum, and (a & b) + (a | b)
merely reordered the terms of that sum. a + b
是一个更大的总和,并且(a & b) + (a | b)
只是重新排序了该总和的各项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.