Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Equality in Lua is denoted as
- ==
- > 5==5
- true
- > 5==6
- false
- > "Mary" == "Mary"
- true
- > "Mary" == "mary"
- false
- Lua has 4 forms of negation:
- -
- ~
- not
- ~=
- Yet only 2 of them can be overloaded. (Needs more input)
- "-"
- '-' is numeric negation. At the CPU level, this is implemented via the
- NEG instruction (CPUs that support floating point have a separate
- instruction for this). On the x86-64 systems, you can negate 8 bit, 16 bit,
- 32 bit and 64 bit quantities.
- > string.format("%X",-5)
- FFFFFFFFFFFFFFFB
- > string.format("%X",-0)
- 0
- "~"
- '~' is bitwise negation. This flips each bit of an integer, and is
- implemented by the NOT instruction (some CPUs name this COM, for
- "complement"---also note that you can't NOT a floating point value).
- Also note that for a given integer A:
- -A is not equal to ~A [1]
- > string.format("%X",~5)
- FFFFFFFFFFFFFFFA
- > string.format("%X",~0)
- FFFFFFFFFFFFFFFF
- "not"
- 'not' is boolean negation
- In Lua, 'not', 'and'
- and 'or' are defined for booleans:
- not (a == b)
- is true if a is not equal to b.
- not (5==6)
- > not (5 == 6)
- true
- > not ( "Mary" == "mary")
- true
- Remember to use parenthesis to force the order of precedence because
- not 5 == 6
- is equal to
- (not 5) == 6
- which is equal to
- false == 6
- > not 5 == 6
- false
- (Probably "not" what you expected. Sorry, I couldn't help myself.)
- Also, because only nil and false are false
- in Lua:
- not 3 returns false
- not nil returns true
- not false returns true
- not true returns false
- "~=" is shorthand for "not =="
- Filling this out:
- not == ~=
- not < >=
- not <= >
- not > <=
- not >= <
- Bitwise Operations
- In Lua 5.3, we now have operators for bitwise operations like and, or, xor
- and not. These are
- & bitwise and
- | bitwise or
- ~ bitwise xor (in context)
- ~ bitwise not (in context)
- [1] For any system you will probably encounter today. There were
- systems where -A does equal ~A, but the chances of coming across
- such a system are very slim these days, and probably only in a
- museum.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement