I was reading the following explanation of python bytecode. I'm posting a lot in order to give the full context. I'm confused about the part where the bitwise 'or' becomes a '+='.
EXTENDED_ARG 1 CALL_FUNCTION 4
When the interpreter executes EXTENDED_ARG, its oparg (which is 1) is left-shifted by eight bits and stored in a temporary variable. Let’s call it extended_arg (do not confuse it with the opname EXTENDED_ARG):
extened_arg = 1 << 8 # same as 1 * 256
So the binary value 0b1 (the binary value of 1) is converted to 0b100000000. This is like multiplying 1 by 256 in the decimal system and extened_arg will be equal to 256. Now we have two bytes in extened_arg. When the interpreter reaches to the next instruction, this two-byte value is added to its oparg (which is 4 here) using a bitwise or.
extened_arg = extened_arg | 4 # Same as extened_arg += 4
This is like adding the value of the oparg to extened_arg. So now we have:
extened_arg = 256 + 4 = 260
and this value will be used as the actual oparg of CALL_FUNCTION. So, in fact,
EXTENDED_ARG 1 CALL_FUNCTION 4
is interpreted as:
EXTENDED_ARG 1 CALL_FUNCTION 260
Can someone explain how this translates.
If
x
andy
share no set bits, thenx | y
is equivalent tox + y
.