# COnfused about the |= symbol?

• 01-20-2011
shrink_tubing
Hi read this in a book. What does this symbol mean the vertical line and the equals sign?

|=
• 01-20-2011
Eman
| is bitwise OR
|= means you're setting a bit..i think
• 01-20-2011
Salem
It's very much like +=

Only it's bitwise-or rather than addition.

Look up assignment operators.
• 01-20-2011
CommonTater
Quote:

Originally Posted by shrink_tubing
Hi read this in a book. What does this symbol mean the vertical line and the equals sign?

|=

It's a shorthand version of : x = x | y;

It does a bitwise OR...

The logic "truth table" is like this...

0 OR 0 = 0
1 OR 0 = 1
0 OR 1 = 1
1 OR 1 = 1

and it's done on every bit of the variable...

That is to say that the two numbers are compared at the binary level and all the 1 bits are merged, like this...
Code:

``` char a = 3;            // in binary  00000011 char b = 24;            // in binary  00011000 a |= b;                // in binary  00011011 Printf("%d",a);      // should display 27; char a = 27;          // in binary  00011011 char b = 24;          // in binary  00011000 a |= b;                // in binary  00011011 Printf("%d",a);        // should still display 27;```
It gets into binary logic which isn't really used in programming for general purpose things, but when working with I/O ports (etc) at the bit level it can be helpful.
• 01-20-2011
shrink_tubing