What is happening when you use the |= operator in C#?
Asked Answered
S

6

5

If I'm using something like this:

xr.Settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;

What precisely is the |= accomplishing?

Squirrel answered 19/12, 2012 at 18:5 Comment(0)
R
15

|= is a shortcut for OR'ing two values together and assigning the result to the first variable.

xr.Settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;

Is equivalent to:

xr.Settings.ValidationFlags = xr.Settings.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings;

| is the OR operator in C#, so the code above effectively sets the ReportValidationWarnings flag on the value xr.Settings.ValidationFlags.

Rainproof answered 19/12, 2012 at 18:7 Comment(4)
How does the comparison after the assignment operator actually occur? I know it's comparing the bits, but I'm having a little difficulty getting my head around it.Squirrel
It's not comparing bits at all. If you have two bytes 1001 1101 and you OR it with 1100 1011 you would get 1101 1111. If a bit it set in either operand, then it is set in the result. The shorthand of |= just states that the left operand of the OR is the operand to the left of the |=.Wier
Doesn't the link below indicate that the bitwise comparison is taking place? msdn.microsoft.com/en-us/library/h5f1zzaw%28v=vs.71%29.aspx. I suppose my confusion lies in something like this: bool b = true; b |= false; and yet b still returns true. I feel stupid, but I'm not seeing how that translates into what's happening above.Squirrel
| means OR; If either parameter is true the result is true. In this case it is a bitwise OR, which means that if any bit is 1 in either parameter, it stays 1.Incapacious
T
1

In this case you are setting XmlSchemaValidationFlags.ReportValidationWarnings flag in your ValidationFlags.

|= is generally bitwise or operator, in case of Flags it's used to set flag.

Tow answered 19/12, 2012 at 18:6 Comment(0)
W
1

This is boolean OR-Equals.

Equivilent to:

xr.Settings.ValidationFlags = xr.Settings.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings;

Which is ensuring that the bits in XmlSchemaValidationFlags.ReportValidationWarnings are set.

Wier answered 19/12, 2012 at 18:7 Comment(0)
S
1

That is a compound assignment. Essentially you are doing:

xr.Settings.ValidationFlags = 
    xr.Settings.ValidationFlags | 
    XmlSchemaValidationFlags.ReportValidationWarnings;

Which will essentially add XmlSchemaValidationFlags.ReportValidationWarnings to the set of flags contained in xr.Settings.ValidationFlags.

Springs answered 19/12, 2012 at 18:7 Comment(1)
So, why does it do that? I'm having trouble getting my head around the operation.Squirrel
T
0

As others have mentioned, a |= b is shorthand for a = a | b, in much the same way as a += b is short for a = a + b. Now what does the | operator do? It can be overloaded, but the general use of it is bit-wise OR. It's similar to the || operator, but it works on a bit by bit basis (think of each bit as a boolean):

false || true is true

0100 | 0110 is 0111

Then the last thing is that one of the classic ways to pass a bunch of boolean flags is to encode them in an integer. A single 32 bit integer can hold 32 individual flags, one per bit. to set a flag, you set the corresponding bit to 1.

So 0000 has no flags set, while 1001 has flags at position 1 and 4 set.

Then |= is a convenient way to set a specific flag.

int my_flags = 0;

my_flag |= validate_flag;

Tracay answered 19/12, 2012 at 18:44 Comment(0)
A
0

driis' answer is correct.

|= is the same as using the | operator in a conditional test, as in the following examples:

xr.Settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings

is semantically identical to

xr.Settings.ValidationFlags = xr.Settings.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings

Additional note about | vs ||

One additional tidbit I found to be useful was the difference between the logical OR (|) and the conditional OR (||):

The logical OR will always evaluate both operands, even if the first is true. The conditional OR will only evaluate the second if the first is false, effectively short-circuiting if the execution of the second operand is unnecessary to determine the final result.

Ancestress answered 14/10, 2019 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.