C# |= Operator

  • Thread starter Thread starter Saxena
  • Start date Start date
Saxena formulated on donderdag :
Hi what are the usage scenerio of |= OR Assignment Operator

You can use it is you are working with the individual bits of the
values: int or some enum.

int i = 9;
i |= 3;

// now i = 11

Hans Kesting
 
Same as usual :

a <op>= b is just a notation for a = a <op> b

Else you may want elaborate a bit (is this OR itself you are wondering about
? it does a bitwise or, allowing to make sure bits are set if they were not
previously set and doesn't changing the value if already set). See :
http://en.wikipedia.org/wiki/Bitwise_operation
 
Joe said:
a = a | b;

Forgive my ignorance, but what does this mean? I can see that it’s
assigning some value to “a†based upon the outcome of the RHS, but what
does the RHS actually do? Could you give some examples, and possibly a
scenario where this would be used?

Cheers,
 
Forgive my ignorance, but what does this mean? I can see that it's
assigning some value to "a" based upon the outcome of the RHS, but what
does the RHS actually do? Could you give some examples, and possibly a
scenario where this would be used?

It operates on the values at the bit level. Let's say a = 16 and b = 3.
Don't think of them as 16 and 3, but rather envision their binary
representation: 00010000 and 00000011. The | (pronounced "or" in this case)
operator compares bits from the two numbers that occupy the same positions
(bits are numbered right to left starting at 0) and outputs the result to
the same position. Or is just a mathematical operation, like addition,
except it has its own rules* and there's no concept of "carry." To do the
math yourself, just stack the two numbers:

00010000
00000011
========
00010011

In the above case, the result is exactly the same as if you had added the
numbers together (19). This is because there were no instances where the
bits in a given position were 1 in both numbers. When that happens, you get
a different result from addition. Take 12 and 9, for example:

00001100
00001001
========
00001101

The result is 13. Remember when I said there's no concept of "carry"? In
addition, when the bits at position 3 are added, they would produce 10 and
the 1 would carry to position 4. No such thing happens in an or operation.
All that really results from or'ing 12 with 9 is that bit 0 gets set, and
that in fact is or's entire purpose in life: to TURN BITS ON. Conversely,
"and" is used to turn bits off, and "xor" is used to swap their state.

* The rules are: 0 | 0 yields 0, any other combination yields 1, meaning if
either bit or both are 1, the result is 1.
 
Jeff said:
It operates on the values at the bit level. …
* The rules are: 0 | 0 yields 0, any other combination yields 1, meaning if
either bit or both are 1, the result is 1.

Ah, I get it now. Thanks.
 
Dylan said:
Forgive my ignorance, but what does this mean? I can see that it’s
assigning some value to “a†based upon the outcome of the RHS, but what
does the RHS actually do? Could you give some examples, and possibly a
scenario where this would be used?

http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx

explains it.

The short version is:
integer types - bitwise OR
bool - OR without shortcut

Arne
 
Back
Top