Advertisement
_dinsdale

Lua Negation By Sean Conner

Sep 18th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. Equality in Lua is denoted as
  2. ==
  3.  
  4. > 5==5
  5. true
  6. > 5==6
  7. false
  8.  
  9. > "Mary" == "Mary"
  10. true
  11. > "Mary" == "mary"
  12. false
  13.  
  14.  
  15.  
  16. Lua has 4 forms of negation:
  17.  
  18. -
  19. ~
  20. not
  21. ~=
  22.  
  23. Yet only 2 of them can be overloaded. (Needs more input)
  24.  
  25.  
  26. "-"
  27. '-' is numeric negation. At the CPU level, this is implemented via the
  28. NEG instruction (CPUs that support floating point have a separate
  29. instruction for this). On the x86-64 systems, you can negate 8 bit, 16 bit,
  30. 32 bit and 64 bit quantities.
  31.  
  32. > string.format("%X",-5)
  33. FFFFFFFFFFFFFFFB
  34.  
  35.  
  36. > string.format("%X",-0)
  37. 0
  38.  
  39.  
  40. "~"
  41.  
  42. '~' is bitwise negation. This flips each bit of an integer, and is
  43. implemented by the NOT instruction (some CPUs name this COM, for
  44. "complement"---also note that you can't NOT a floating point value).
  45.  
  46. Also note that for a given integer A:
  47.  
  48. -A is not equal to ~A [1]
  49.  
  50. > string.format("%X",~5)
  51. FFFFFFFFFFFFFFFA
  52.  
  53. > string.format("%X",~0)
  54. FFFFFFFFFFFFFFFF
  55.  
  56. "not"
  57.  
  58. 'not' is boolean negation
  59.  
  60. In Lua, 'not', 'and'
  61. and 'or' are defined for booleans:
  62.  
  63. not (a == b)
  64.  
  65. is true if a is not equal to b.
  66.  
  67. not (5==6)
  68.  
  69. > not (5 == 6)
  70. true
  71.  
  72. > not ( "Mary" == "mary")
  73. true
  74.  
  75.  
  76. Remember to use parenthesis to force the order of precedence because
  77.  
  78. not 5 == 6
  79.  
  80. is equal to
  81.  
  82. (not 5) == 6
  83.  
  84. which is equal to
  85.  
  86. false == 6
  87.  
  88. > not 5 == 6
  89. false
  90.  
  91. (Probably "not" what you expected. Sorry, I couldn't help myself.)
  92.  
  93.  
  94. Also, because only nil and false are false
  95. in Lua:
  96.  
  97. not 3 returns false
  98. not nil returns true
  99. not false returns true
  100. not true returns false
  101.  
  102.  
  103. "~=" is shorthand for "not =="
  104.  
  105.  
  106. Filling this out:
  107.  
  108. not == ~=
  109. not < >=
  110. not <= >
  111. not > <=
  112. not >= <
  113.  
  114. Bitwise Operations
  115.  
  116. In Lua 5.3, we now have operators for bitwise operations like and, or, xor
  117. and not. These are
  118.  
  119. & bitwise and
  120. | bitwise or
  121. ~ bitwise xor (in context)
  122. ~ bitwise not (in context)
  123.  
  124.  
  125. [1] For any system you will probably encounter today. There were
  126. systems where -A does equal ~A, but the chances of coming across
  127. such a system are very slim these days, and probably only in a
  128. museum.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement