开发者

bitwise AND in arithmetic operation

Bitwise OR(|) is similar to arithmetic addition(+) i.e. A|B = A+B (if A!=B)

Like, 2|4 = 6 and 2+4 = 6

That means there is a way to get "OR" result in by doing addition in arithmetic context.

Is there a similar way to get bitwise "AND" result by doing arithmetic operation.

i.e. A&B = aithmetic_op(A,B)

A = 2^k (k=1,2,3,4...)

B = 2^k (k=1,2,3,开发者_开发问答4...)

Thanks


Just as OR is analogous to +, AND is analogous to *

0 AND 0 = 0 * 0 = 0

0 AND 1 = 0 * 1 = 0

1 AND 0 = 1 * 0 = 0

1 AND 1 = 1 * 1 = 1

Note that this only works for a single bit (as does the analogy between OR and + that you mention), due to the effects of arithmetic carries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜