What does the "|=" operator in Python mean?

13

I was analyzing a code and I came across the |= operator, I would like to know what that means, and what is its practical application.

Example:

x |= y 
    
asked by anonymous 18.09.2017 / 13:48

2 answers

13

Just as the += operator increments the value of a variable, something like x +=y is equivalent to x = x + y , the |= operator runs the or binary between the current value and the other parameter, something like x |= y is equivalent to x = x | y .

The or binary is nothing more than the operation or executed bit by bit. For example, let's take two positive integers:

x = 5
y = 13

We can check the binary representation of each value through the native function bin .

print(bin(x), bin(y)) # 0b101 0b1101

That can be expanded to 8 bits for 00000101 and 00001101 . O or binary will do the operation or bit by bit, returning 1 when at least 1 of the operands is 1 or 0 when both are 0.

00000101 | 00001101 = 00001101

Note that the result, in this example, will be the same as the second operand, so doing 5 | 13 results in 13. Thus:

x |= y
print(x) # 13
  

See working at Ideone .

Application

One of the most common applications of this operator is to define flags used in the configurations of some system. Flags are commonly Boolean values, which can only receive values of 0 or 1. Using the boolean type, it is common for compilers and interpreters to allocate more than just 1 bit for such a type, even if it actually occupies , a single bit. To work around this, you can store all the flags into another variable type, using the | operator. Considering that the type char is represented by 8 bits, which is the default in C, it would be possible to store 8 different flags in a char . With Python, because it's dynamic typing, this ends up getting a bit abstract, but the philosophy behind it ends up being the same.

Let's look at an example with code:

Let's consider that our application has 8 different configuration options. For simplicity, I'll give generic names from A to H:

CONFIG_A = 1     # bin: 0b00000001
CONFIG_B = 2     # bin: 0b00000010
CONFIG_C = 4     # bin: 0b00000100
CONFIG_D = 8     # bin: 0b00001000
CONFIG_E = 16    # bin: 0b00010000
CONFIG_F = 32    # bin: 0b00100000
CONFIG_G = 64    # bin: 0b01000000
CONFIG_H = 128   # bin: 0b10000000

Notice that with these given integer values, each configuration will represent only one bit in the binary representation. This will allow that if our configuration has the 0b00000101 value, it means that the CONFIG_A and CONFIG_C settings have been set. This setting can be done through the | operator:

config = CONFIG_A | CONFIG_C  # bin: 0b00000101
  

See working at Ideone .

In the code, to check if a given configuration has been defined, simply use the inverse operation & (and bit by bit). For example:

def foo(config):
    if (config & CONFIG_A): print("Configuração A foi definida")
    if (config & CONFIG_B): print("Configuração B foi definida")
    if (config & CONFIG_C): print("Configuração C foi definida")
    if (config & CONFIG_D): print("Configuração D foi definida")
    if (config & CONFIG_E): print("Configuração E foi definida")
    if (config & CONFIG_F): print("Configuração F foi definida")
    if (config & CONFIG_G): print("Configuração G foi definida")
    if (config & CONFIG_H): print("Configuração H foi definida")
    print(" -- + --")

Testing the function:

foo(CONFIG_A)
foo(CONFIG_B | CONFIG_D)
foo(CONFIG_H | CONFIG_E | CONFIG_F)

The output will be:

Configuração A foi definida
 -- + --
Configuração B foi definida
Configuração D foi definida
 -- + --
Configuração E foi definida
Configuração F foi definida
Configuração H foi definida
 -- + --
  

See working at Ideone .

    
18.09.2017 / 14:02
9

For integers, it is the bitwise or OR compound assignment operator. Like all composite assignment operators, a | = b is the same as a = a | b . Where | is the symbol of OR bit by bit.

  

| = Bitwise inclusive OR    link

     

| = (bitwise OR assignment) Performs bitwise OR and assigns value to   the left operand.    link

My understanding after searching ... The |= will assign the value after executing a OR between two variables, but bit by bit.

For example:

   0101 (decimal 5)
OR 0011 (decimal 3)
 = 0111 (decimal 7)

Another example:

a |= 10 //Supondo que a vale 12.

   1100 (decimal 12)
OR 1010 (decimal 10)
 = 1110 (decimal 14)

//Dessa forma a será igual a 14.

Take a look at this link I took it as a base. It's the same question as yours with plenty of examples and use case: link

    
18.09.2017 / 13:56