CodeDoctorJL
CodeDoctorJL

Reputation: 1284

"|=" what does this mean and what is this called? (c++)

void show_node_names()   { display_flags |= ShowNodeNames; } // what is "|="?

I'm not sure what "|=" does or what it's called. Any help?

Upvotes: 0

Views: 271

Answers (3)

Mike Seymour
Mike Seymour

Reputation: 254721

| (which can also be spelt bitor) is the bitwise or operator. It combines the bits of each operand so that each bit of the output is set if the corresponding bit of either operand is set. Compare this with the bitwise and operator, & or bitand, where each bit is set of the corresponding bit of both operands is set.

|= (or or_eq) is the corresponding assignment operator. As with all compound assignment operators, a |= b is equivalent to a = a | b, except that a is only evaluated once. Its effect is to set each bit in a that's set in b, and leave the other bits unchanged.

Upvotes: 2

templatetypedef
templatetypedef

Reputation: 373112

The |= operator is a compound assignment operator like += or *=, but using the bitwise OR operator. The line

display_flags |= ShowNodeNames;

is equivalent to

display_flags = display_flags | ShowNodeNames;

If you haven't seen the bitwise OR operator, you should read up on it for more details. If you're familiar with it, then you can think of display_flags |= ShowNodeNames; as a way of saying "make all the bits set in ShowNodeNames also set in display_flags."

Hope this helps!

Upvotes: 3

merlin2011
merlin2011

Reputation: 75629

That statement is a bitwise or assignment.

It is equivalent to doing display_flags = display_flags | ShowNodeNames.

In particular, it will set every bit in display_flags to 1 if the corresponding bit in ShowNodeNames is 1.

Upvotes: 4

Related Questions