lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


from manual:

Operator precedence in Lua follows the table below, from lower to higher priority:
[[cut]]
     <<    >>
     ..
     +     -
     *     /     //    %
[[cut]]

test example with wrong result:

Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> i = 0
> i = i + 1 * 1 << 8
> print(i)
256
> i = i + 1 * 1 << 16
> print(i)
16842752
> i = i + 1 * 1 << 24
> print(i)
282574505115648

but with braces all ok:

Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> i = 0
> i = i + 1 * (1 << 8)
> print(i)
256
> i = i + 1 * (1 << 16)
> print(i)
65792
> i = i + 1 * (1 << 24)
> print(i)
16843008