开发者

Arithmetic + and Bitwise OR

Is there any difference between Arithmetic + a开发者_StackOverflownd bitwise OR. In what way this is differing.

uint a = 10;
uint b = 20;

uint  arithmeticresult = a + b;

uint bitwiseOR = a | b;

Both the results are 30.

Edit : Small changes to hide my stupidity.


(10 | 20) == 10 + 20 only because the 1-bits do not appear in the same digit.

       1010 = 10
   or 10100 = 20
   ————————
      11110 = 30

However,

    11 = 3        11 = 3
or 110 = 6     + 110 = 6
——————         ——¹——————
   111 = 7      1001 = 9
#   ^             ^
# (1|1==1)      (1+1=2)


Counterexample:

2 + 2 == 4
2 | 2 == 2

Bitwise OR means, for each bit position in both numbers, if one or two bits are on, then the result bit is on. Example:

0b01101001
|
0b01011001
=
0b01111001

(0b is a prefix for binary literals supported in some programming languages)

At the bit level, addition is similar to bitwise OR, except that it carries:

0b01101001
+
0b01011001
=
0b11000010

In your case, 10+20 and 10|20 happen to be the same because 10 (0b1010) and 20 (0b10100) have no 1s in common, meaning no carry happens in addition.


Try setting a = 230 and b = 120. And you'll observer the difference in results.

The reason is very simple. In the arithmentic addition operation the bit-wise add operation may generate carry bit which is added in the next bit-wise addition on the bit-pair available on the subsequent position. But in case of bit wise OR it just performs ORing which never generates a carry bit.

The fact that you're getting same result in your case is that the numbers co-incidentally don't generate any carry-bit during addition.

Bit-wise arithmetic Addition

alt text http://www.is.wayne.edu/drbowen/casw01/AnimAdd.gif


Bitwise OR goes through every bit of two digits and applies the following truth table:

A  B  | A|B
0  0  |  0
0  1  |  1
1  0  |  1
1  1  |  1

Meanwhile the arithmetic + operator actually goes through every bit applying the following table (where c is the carry-in, a and b are the bits of your number, s is the sum and c' is the carry out):

C  A  B  | S  C'
0  0  0  | 0  0
0  0  1  | 1  0
0  1  0  | 1  0
0  1  1  | 0  1
1  0  0  | 1  0
1  0  1  | 0  1
1  1  0  | 0  1
1  1  1  | 1  1

For obvious reasons, the carry-in starts-off being 0.

As you can see, sum is actually a lot more complicated. As a side effect of this, though, there as an easy trick you can do to detect overflow when adding positive signed numbers. More specifically, we expect that a+b >= a|b if that fails then you have an overflow!

The case when the two numbers will be the same is when every time a bit in one of the two numbers is set, the corresponding bit int he second number is NOT set. That is to say that you have three possible states: either both bits aren't set, the bit is set in A but not B, or the bit is set in B but not A. In that case the arithmetic + and the bit-wise or would produce the same result... as would the bitwise xor for that matter.


Using arithmetic operations to manipulate bitmasks can produce unexpected results and even overflow. For instance, turning on the n-th bit of a bitmask if it is already on will turn off the n-th bit and turn on the n+1-th bit. This will cause overflow if there are only n-bits.

Example of turning on bit 2:

Arithmetic ADD      Bitwise OR
          0101            0101                                           
        + 0100          | 0100     
          ----            ----                                           
          1001            0101    //expected result: 0101                              

Like-wise, using arithmetic subtract to turn off the n-th bit will fail if the n-th bit was not already on.

Example of turning off bit 2:

Arithmetic SUB      Bitwise AND~
          0001              0001 
        - 0100           &~ 0100
          ----              ----
          0001              0001
        + 1100           &  1011
          ----              ----
          1101              0001   //expected result: 0001

So bitwise operators are safer than arithmetic operators when you are working with bitmasks.

The following bitwise operations have analogous arithmetic operations:

                    Bitwise            Arithmetic 
Check n-th bit      x  &  (1 << n)   !(x  - (1 << n))
Turn on n-th bit    x |=  (1 << n)     x += (1 << n)
Turn off n-th bit   x &= ~(1 << n)     x -= (1 << n)


Try a = 1 and b = 1 ;) + and | have different when two bits at the same positions are 1


  00000010

OR

  00000010

Result

  00000010

VS

  00000010

+

  00000010

Result

  00000100
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜