为什么是(A | B),相当于一个 - (一和b)+ B? [英] Why is (a | b ) equivalent to a - (a & b) + b?
问题描述
我一直在寻找一种方式做一个BITOR()与Oracle数据库并在一个建议来只使用BITAND()代替,替换BITOR(A,B)与A + B - BITAND(A,B)
I was looking for a way to do a BITOR() with an Oracle database and came across a suggestion to just use BITAND() instead, replacing BITOR(a,b) with a + b - BITAND(a,b).
我手工测试了几次,并验证它似乎所有的二进制数我能想到的工作,但我想不出为什么这是正确的了快速的数学证明。
有人能告诉我吗?
I tested it by hand a few times and verified it seems to work for all binary numbers I could think of, but I can't think out quick mathematical proof of why this is correct.
Could somebody enlighten me?
推荐答案
A和B是位集是在A和B. A的 - (A和B),让你与所有那些只在A.添加B到了位,你会得到一切都在位在A或那些在B.
A & B is the set of bits that are on in both A and B. A - (A & B) leaves you with all those bits that are only on in A. Add B to that, and you get all the bits that are on in A or those that are on in B.
简单添加A和B都不会因为携带在哪里工作都具有1位。首先取出A和B共同位,我们知道,(A-(A和B))将与B公共无位,所以加在一起保证不会产生套利
Simple addition of A and B won't work because of carrying where both have a 1 bit. By removing the bits common to A and B first, we know that (A-(A&B)) will have no bits in common with B, so adding them together is guaranteed not to produce a carry.
这篇关于为什么是(A | B),相当于一个 - (一和b)+ B?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!