Search code examples
lualuajit

unwelcome if statement on luajit


lua 5.3.5 accepts the code below,

 function isOdd (n)                                                                                                    
     if n & 1 == 1                                                                                                     
         then return true                                                                                              
         else return false                                                                                             
     end                                                                                                               
 end    

 print(isOdd(1), isOdd(2)) 

Why does luajit 2.0.5 refuse it?

line 2:  'then' expected near '&'

Solution

  • Because native bitwise operators are new to Lua 5.3, and LuaJIT is basically Lua 5.1. Use bit.band instead:

    This module is a LuaJIT built-in — you don't need to download or install Lua BitOp. The Lua BitOp site has full documentation for all Lua BitOp API functions.

    Please make sure to require the module before using any of its functions:

    local bit = require("bit")
    

    Source