ニルスの答えをさらに拡張するために、リチャード・シュロッペルがこれを発明しました。
http://www.inwap.com/pdp10/hbaker/hakmem/boolean.html#item23
アイテム23(シュロエッペル):
(A AND B)+(A OR B)= A + B =(A XOR B)+ 2(A AND B)。
(A + B)/2 = ((A XOR B) + 2(A AND B))/2
= (A XOR B)/2 + (A AND B)
= (A XOR B)>>1 + (A AND B)
avg(x,y){return((x^y)>>1)+(x&y);}
(A AND B) + (A OR B) = A + B
なぜならA AND B
、2の共有(AとBの間)の累乗の合計を与えるので、共有さA OR B
れるものと共有されないものの両方を与えるので、次のようになります。
(A AND B) + (A OR B) =
(sum of shared powers of two) +
((sum of shared powers of two) + (sum of unshared powers of two)) =
(sum of shared powers of two) +
((sum of shared powers of two) + (sum of powers of two of A only) +
(sum of powers of two of B only)) =
((sum of shared powers of two) + (sum of powers of two of A only)) +
((sum of shared powers of two) + (sum of powers of two of B only))
= A + B.
A XOR B
AとBの間で異なるビットのマップを提供します。したがって、
A XOR B = (sum of powers of two of A only) + (sum of powers of two of B only).
したがって:
2(A AND B) + (A XOR B) =
((sum of shared powers of two) + (sum of powers of two of A only)) +
((sum of shared powers of two) + (sum of powers of two of B only))
= A + B.