开发者

bitwise AND in arithmetic operation

开发者 https://www.devze.com 2023-03-06 12:29 出处:网络
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.

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

精彩评论

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