What does "|=" mean? (pipe equal operator)
I was looking for an answer on what |=
does in Groovy and although answers above are right on they did not help me understand a particular piece of code I was looking at.
In particular, when applied to a boolean variable "|=" will set it to TRUE the first time it encounters a truthy expression on the right side and will HOLD its TRUE value for all |= subsequent calls. Like a latch.
Here a simplified example of this:
groovy> boolean result
groovy> //------------
groovy> println result //<-- False by default
groovy> println result |= false
groovy> println result |= true //<-- set to True and latched on to it
groovy> println result |= false
Output:
false
false
true
true
Edit: Why is this useful?
Consider a situation where you want to know if anything has changed on a variety of objects and if so notify some one of the changes. So, you would setup a hasChanges
boolean and set it to |= diff (a,b)
and then |= dif(b,c)
etc.
Here is a brief example:
groovy> boolean hasChanges, a, b, c, d
groovy> diff = {x,y -> x!=y}
groovy> hasChanges |= diff(a,b)
groovy> hasChanges |= diff(b,c)
groovy> hasChanges |= diff(true,false)
groovy> hasChanges |= diff(c,d)
groovy> hasChanges
Result: true
|=
reads the same way as +=
.
notification.defaults |= Notification.DEFAULT_SOUND;
is the same as
notification.defaults = notification.defaults | Notification.DEFAULT_SOUND;
where |
is the bit-wise OR operator.
All operators are referenced here.
A bit-wise operator is used because, as is frequent, those constants enable an int to carry flags.
If you look at those constants, you'll see that they're in powers of two :
public static final int DEFAULT_SOUND = 1;
public static final int DEFAULT_VIBRATE = 2; // is the same than 1<<1 or 10 in binary
public static final int DEFAULT_LIGHTS = 4; // is the same than 1<<2 or 100 in binary
So you can use bit-wise OR to add flags
int myFlags = DEFAULT_SOUND | DEFAULT_VIBRATE; // same as 001 | 010, producing 011
so
myFlags |= DEFAULT_LIGHTS;
simply means we add a flag.
And symmetrically, we test a flag is set using &
:
boolean hasVibrate = (DEFAULT_VIBRATE & myFlags) != 0;
You have already got sufficient answer for your question. But may be my answer help you more about |=
kind of binary operators.
I am writing table for bitwise operators:
Following are valid:
----------------------------------------------------------------------------------------
Operator Description Example
----------------------------------------------------------------------------------------
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2
^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
----------------------------------------------------------------------------------------
note all operators are binary operators.
Also Note: (for below points I wanted to add my answer)
>>>
is bitwise operator in Java that is called Unsigned shiftbut>>>= operator>>>=
not an operator in Java.~
is bitwise complement bits,0 to 1 and 1 to 0
(Unary operator) but~=
not an operator.Additionally,
!
Called Logical NOT Operator, but!=
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. e.g.(A != B) is true
. where asA=!B
means ifB
istrue
thenA
becomefalse
(and ifB
isfalse
thenA
becometrue
).
side note: |
is not called pipe, instead its called OR, pipe is shell terminology transfer one process out to next..