Hi, Eärendil brought me here. The
neg^multiplayer_is_server
thing is very misleading. This is processed by the Python-based compiler when generating the weird lines that end up in the
.txt
files and it only works at all because the bits of both numbers don't touch.
Python:
>>> hex(0x80000000 | 417)
'0x800001a1'
>>> hex(0x80000000 ^ 417)
'0x800001a1'
The game sees the same M&B operation code / opcode. So what? Why? ¯\_(ツ)_/¯
An exclusive OR (XOR) works like a plain OR whenever both bits are not one,
see the truth table here and compare XOR with OR. So using this for flags is a bit like falling sideways while going down a rail but sticking the landing;
^
technically produces the same result, but when you want to combine flags in a bitfield with the rest of a number you
really want to use the
|
operation to merge all the 1 bits from top and bottom into the same number, you don't really want that your bits return zero whenever the top and bottom bits are one, even if in this case does not happen, so you don't get this unintentional behavior with these particular module system numbers.
And I don't want to sound mean, but don't think the precision argument has any merit in this case because you are not shifting/rotating bits left or right, they stay in place, and they are of the same type/size. Sounds a bit like programming snake oil.
Python:
10000000000000000000000000000000 # 0x80000000 => 2147483648 in decimal => neg
110100001 # 0x1a1 => 417 in decimal => multiplayer_is_server
-------------------------------- # ----------
10000000000000000000000110100001 # 0x800001a1 = (0x80000000 | 0x1a1) which is the same as doing (0x80000000 | 417)
In the table above, if you do an OR operation, as long as there is some
1
in the input (e.g.
1|1
,
0|1
,
1|0
) it will output
one, for XOR if both are one (
1^1
) will be
zero, this is generally used to negate or toggle boolean variables easily. Turning
True
into
False
and vice versa.
TL;DR: Don't get me wrong, XOR is very useful for many other low-level and fun operations, but it shouldn't be used for concatenating flags. I can use other unrelated operations that also give the same result and look cool, but that doesn't make it right.
Python:
>>> hex(0x80000000 + 417)
'0x800001a1'
>>> hex((0x80000000 - ~417) - 1)
'0x800001a1'
>>> hex(~(~0x80000000 & ~417)) # swy: same as OR, but obfuscated with NAND
'0x800001a1'
Hope that helps at least a bit.
