Home All Groups Group Topic Archive Search About
Author
9 Jul 2009 12:37 PM
Saxena
Hi what are the usage scenerio of |= OR Assignment Operator

Author
9 Jul 2009 12:55 PM
Hans Kesting
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
Are all your drivers up to date? click for free checkup

Author
9 Jul 2009 12:59 PM
Patrice
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

--
Patrice

"Saxena" <Sax***@discussions.microsoft.com> a écrit dans le message de
groupe de discussion : 1FE2EEDD-9293-4F20-9CFC-D48E92619***@microsoft.com...
Show quoteHide quote
> Hi what are the usage scenerio of |= OR Assignment Operator
Author
9 Jul 2009 1:05 PM
Joe Cool
On Jul 9, 8:37 am, Saxena <Sax***@discussions.microsoft.com> wrote:
> Hi what are the usage scenerio of |= OR Assignment Operator

a = a | b;

is equiavilent to

a |= b;
Author
9 Jul 2009 1:14 PM
Dylan Parry
Joe Cool wrote:

> 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,

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

“If I had my life to live over again, I'd be a plumber” – Einstein
Author
9 Jul 2009 2:05 PM
Jeff Johnson
"Dylan Parry" <use***@dylanparry.com> wrote in message
news:h34qg0$bjn$1@news.eternal-september.org...

>> 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?

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.
Author
9 Jul 2009 2:58 PM
Dylan Parry
Jeff Johnson wrote:

> 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 Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

“If I had my life to live over again, I'd be a plumber” – Einstein
Author
9 Jul 2009 10:47 PM
Arne Vajhøj
Dylan Parry wrote:
> Joe Cool wrote:
>> 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?

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

explains it.

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

Arne

Bookmark and Share