These are the operators of bit arithmetic operations. They change the LH value in place (or reassign the lh value of the new calculated value if lh is unchanged) with the same style operators as C.
Python calls them operator transfers ( << and >> ) and binary bitwise operators ( & , ^ and | ).
With the addition of = after the statement, the assignment returns to the original name LH, so |= does in place or and <<= performs a left shift in place.
So, x<<=3 coincides with x=x<<3 or the value of bit x is shifted to the left by 3 places.
Shift Bit:
>>> b=0x1 >>> b<<=3 >>> bin(b) '0b1000' >>> b>>=2 >>> bin(b) '0b10'
Bit or:
>>> bin(b) '0b1' >>> bin(c) '0b100' >>> c|=b >>> bin(c) '0b101'
Technically, if the LH value is unchanged, the result is a rebound to the LH name, rather than the actual conversion of the value in place.
This can be shown by examining this element address:
>>> id(i) 4298178680 # original address (in CPython, id is the address) >>> i<<=3 >>> id(i) 4298178512 # New id; new int object created
Unlike using C (in this case, ctypes ):
>>> import ctypes >>> i=ctypes.c_int(1) >>> ctypes.addressof(i) 4299577040 >>> i.value<<=3 >>> ctypes.addressof(i) 4299577040
Since C integers are mutable, the bit shift is performed in place without a new address for C int.
source share