For the code example:
num_1 = 12 ~ 36
What is the tilde doing in this example? Is it just a shortened way to represent ~= ?
What does tilde by itself mean in Lua?
Operators on their own have no meaning. Only in combination with their operands they make sense.
There are three use-cases for ~ in Lua.
Relational operator ~= unequal: a ~= b is true if a is unequal b
Bitwise operators
~ bitwise XOR: a ~ b resolves to a number where every bit is the XORed value of a's and b's respective bit~ bitwise NOT: ~a resolves to a number where every bit is inverted. So zeros become ones and ones become zeros.In your case num_1 = 12 ~ 36 where ~ is used as a binary operator it is the bitwise XOR.
100100 36
001100 12
------
101000 40
So num_1 = 12 ~ 36 assigns 40 to num_1.
˜ is bitwise exclusive OR. See Bitwise Operators in the Reference Manual.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With