在 luajit 上不受欢迎的 if 语句

unwelcome if statement on luajit

lua 5.3.5接受下面的代码,

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

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

为什么luajit 2.0.5拒绝它?

line 2:  'then' expected near '&'

因为本机按位运算符是 Lua 5.3 的新增功能,而 LuaJIT 基本上是 Lua 5.1。使用 bit.band 代替:

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