What does |= mean? (pipe equal operator)
|=
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;
What does |= (pipe equal) sign do in python?
As @AChampion already mentioned in the first question comment, it could be "bitwise or" or "set union". While this question has Odoo as context, it is "set union" for the Odoo class RecordSet
.
This class was introduced with the new API on Odoo 8. For other operators look into the official doc of Odoo.
What does |= (single pipe equal) and &=(single ampersand equal) mean
They're compound assignment operators, translating (very loosely)
x |= y;
into
x = x | y;
and the same for &
. There's a bit more detail in a few cases regarding an implicit cast, and the target variable is only evaluated once, but that's basically the gist of it.
In terms of the non-compound operators, &
is a bitwise "AND" and |
is a bitwise "OR".
EDIT: In this case you want Folder.Attributes &= ~FileAttributes.System
. To understand why:
~FileAttributes.System
means "all attributes exceptSystem
" (~
is a bitwise-NOT)&
means "the result is all the attributes which occur on both sides of the operand"
So it's basically acting as a mask - only retain those attributes which appear in ("everything except System"). In general:
|=
will only ever add bits to the target&=
will only ever remove bits from the target
In PHP what does |= mean, That is pipe equals (not exclamation)
|=
is to |
as +=
is to +
; that is, $a |= $b;
is the same as $a = $a | $b;
. The |
operator is the bitwise OR operator.
What does the |= Operator mean?
You know how x += 1
means x = x + 1
, well x |= 1
means x = x | 1
. Of course |
means bitwise OR.
Java Operator: |=
=|
is a compound assignment operator, similar to +=
, -=
, /=
, or *=
, but with bitwise OR instead.
This is equivalent to:
sequ = (int) (sequ | element.sequence);
where |
is the bitwise OR operation, meaning that it independently ORs all bits in the left operand with those in the right operand, to get a result. The cast is not necessary if element.sequence
is already an int
.
Note: Your original code wouldn't make sense:
int sequ |= element.sequence
You can't declare it there and then and or it with something else. It would need to have been declared and assigned before, such as in:
int sequ = 0; /* or some other value */
sequ |= element.sequence;
What is the difference between the | and || or operators?
Just like the &
and &&
operator, the double Operator is a "short-circuit" operator.
For example:
if(condition1 || condition2 || condition3)
If condition1 is true, condition 2 and 3 will NOT be checked.
if(condition1 | condition2 | condition3)
This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions, you can get a good performance boost by using them.
There is one big caveat, NullReferences or similar problems. For example:
if(class != null && class.someVar < 20)
If class is null, the if-statement will stop after class != null
is false. If you only use &, it will try to check class.someVar
and you get a nice NullReferenceException
. With the Or-Operator that may not be that much of a trap as it's unlikely that you trigger something bad, but it's something to keep in mind.
No one ever uses the single &
or |
operators though, unless you have a design where each condition is a function that HAS to be executed. Sounds like a design smell, but sometimes (rarely) it's a clean way to do stuff. The &
operator does "run these 3 functions, and if one of them returns false, execute the else block", while the |
does "only run the else block if none return false" - can be useful, but as said, often it's a design smell.
There is a Second use of the |
and &
operator though: Bitwise Operations.
Pipe (|) operator in Java
It's a bitwise OR operation. It's modifying things at a binary level.
011 3
in binary: | 100 in decimal: | 4
___ ___
111 7
Open Windows calc using scientific mode. You can flip between decimal and binary (and hex) and perform bitwise operations including or, and, xor, etc.
To do a bitwise or in your head or on paper, compare each digit of the same ordinal. If either number is a 1, the result at that ordinal will be 1.
What does |= operator do?
dirtyFlag |= 0x0001
is equivalent to dirtyFlag = dirtyFlag | 0x0001
. The |
operator is the bitwise OR operator. In your case, it sets the lowest binary bit. Some further examples:
1 | 2 = 3 (0001 | 0010 = 0011)
2 | 4 = 6 (0010 | 0100 = 0110)
5 | 1 = 5 (0101 | 0001 = 0101)
Related Topics
Find Duplicate Value in Array List and Print the Duplicated Value
Fill an Array With Random Numbers
Jaspersoft Studio 6.2 Build Path Warning
File Not Found Exception When Reading File from Linux Extreme Vps
Ugly Fonts in Java Applications on Ubuntu
What's Java Hybrid - Applet + Application
Multiple Row Selection in Jtable
How to Make the Method Return Type Generic
Gson: How to Exclude Specific Fields from Serialization Without Annotations
Remote Debugging a Java Application
Difference Between Getattribute() and Getparameter()
How to Sort Map Values by Key in Java
Determine If a String Is an Integer in Java
Create a Java Executable with Eclipse
Installing Jdk8 on Ubuntu- "Unable to Locate Package" Update Doesn't Fix