What does "|=" operation mean in C++?
Asked Answered
L

8

11

I have the following code and I can't understand what does it mean:

var1 |= var2>0 ? 1 : 2;

Anyone can help me please!

Laurustinus answered 17/4, 2010 at 8:50 Comment(4)
In CS, it's called a "compound assignment".Inflict
You should read up on the language.Wicks
FYI, this is also doable in Java given, e.g. int var1, var2.Jadotville
It is called "someone being too clever and creating unreadable code, instead of creating clean logically structured code that can be read and maintained easily". polygenelubricants answer is perfect because it is immediately understandable.Geometrid
J
20
if (var2 > 0)
  var1 = var1 | 1;
else 
  var1 = var1 | 2;

It's bitwise-or.

Jadotville answered 17/4, 2010 at 8:51 Comment(0)
C
11

All the a op= b operators are a shortcut to a = a op b.

However since C++ allows op and op= to be overridden separately you rely on each implementer of custom types to be consistent.

Colored answered 17/4, 2010 at 8:53 Comment(5)
I would deem evil anyone performing different task in @ and @= :sPriesthood
@Matthieu: C++ is (and, IMO, always will be) an language that gives you plenty of rope with which to hang yourself.Colored
I don't know of anyone implementing them to have different behavior (except that the compound assignment obviously changes its left-hand operand). But implementing one without the other is fairly common, e.g. iostreams insertion operators.Loveland
But it takes effort to be stupid enough to tie that rope around your own neck.Abydos
a != b; is not the same as a = a ! b;!Herne
T
6

Its the Assignment by bitwise OR

v1 |= v2;

is short for:

v1 = v1 | v2;
Topi answered 17/4, 2010 at 8:51 Comment(0)
H
5

cond ? x : y returns x if cond is true and y otherwise. Read Ternary Operator

a |= b is shorthand for a = a | b which is assigning a | b to a

a | b is bitwise OR of a and b. ( e.g. 2 | 3 = 3 and 1 | 2 = 3 )

Hatchery answered 17/4, 2010 at 8:57 Comment(0)
F
4

Integers can be represented in binary, so that each digit (bit, switch) is 1 (on) or 0 (off):

00000000  ==  0
00000001  ==  1
00000010  ==  2
00000011  ==  3
00000100  ==  4
00001000  ==  8
00010000  ==  16

Bitwise OR combines two numbers by "merging" the two sets of bits:

First number:     00110000
Second number:    00000010
Result:           00110010

If a bit is 1 in EITHER of the input numbers, then it will be 1 in the result.

Compare with bitwise AND, which finds the "overlap" of the two sets of bits:

First number:     00110100
Second number:    10011110
Result:           00010100

If a bit is 1 in BOTH of the input numbers, then it will be 1 in the result.

If the numbers are in variables a and b, you can place the the bitwise OR/AND results into a new variable c:

unsigned int c = a | b; // OR

unsigned int c = a & b; // AND

Often the result needs to be placed into one of the two variables, i.e.

unsigned int c = a | b; // OR
c = a; // copy

So as a shorthand, you can do this in a single step:

a |= b; // merge b directly into a
Fotheringhay answered 17/4, 2010 at 9:32 Comment(0)
C
3

As others have said it is short for v1 = v1 | v2; Another usage you might come across is with booleans.
Given:

bool b = /*some value*/

Instead of saying:

if(a)
  b = true;

you might see:

  b |= a;
Charleycharlie answered 17/4, 2010 at 8:57 Comment(0)
C
3

As other people before me have mentioned, it means you'll end up with assignments by bitwise OR.

Bitwise OR can be illustrated by taking the left-hand and right-hand side bit-patterns and put them on top of eachother.

In each column: 0 + 0 gives 0, 1 + 0 gives 1, 0 + 1 gives 1, 1 + 1 gives 1.
In the context of booleans: false OR false == false, true OR false == true, false OR true == true, true OR true == true.

Here's an example of bitwise OR and the resulting bit pattern: var1(11) |= var2(14) --> var1(15)

    1011 (11)
OR  1110 (14)  
=   1111 (15)
Chronometer answered 17/4, 2010 at 9:30 Comment(0)
T
1

The operator |= means Assignment by bitwise OR operator

Theotheobald answered 17/4, 2010 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.