Hi read this in a book. What does this symbol mean the vertical line and the equals sign?
|=
This is a discussion on COnfused about the |= symbol? within the C++ Programming forums, part of the General Programming Boards category; Hi read this in a book. What does this symbol mean the vertical line and the equals sign? |=...
Hi read this in a book. What does this symbol mean the vertical line and the equals sign?
|=
| is bitwise OR
|= means you're setting a bit..i think
You ended that sentence with a preposition...Bastard!
It's very much like +=
Only it's bitwise-or rather than addition.
Look up assignment operators.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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...
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.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;
Last edited by CommonTater; 01-20-2011 at 10:03 AM.
Awesome answers!!!!! Thanks