Lua and bitwise operators

I looked for information on bitwise operators. Stuff like shift left and shift right, masking using and and or…

There is some information to be found but most are extensions which do not work on vera.

I found some example code with rudimentary HasBit examples. I can make do with these routines for now but I think Vera definitely needs these routines natively.

Did I miss something or are these routines just not there?

I found bit.lua on vera in directory /usr/lib/lua

But when I use the following in code:

local bit = require(“bit”)
luup.log( "Bit and(15,12) " … tostring(bit.band( 15, 12)) )
luup.log( "Bit lshift(12,3) " … tostring(bit.blshift( 12, 3)) )

I get an error:

LuaInterface::CallFunction_Startup-1 device 6 function mainStartup failed [string “…”]:57: attempt to index upvalue ‘bit’ (a function value) <0x402>

[quote=“evanes, post:1, topic:167090”]I can make do with these routines for now but I think Vera definitely needs these routines natively.
Did I miss something or are these routines just not there?[/quote]

They’re just not there. Lua 5.1 doesn’t have bitwise operations natively, and that’s what Vera runs.

I ended up just coding the routines I needed from first principles, using multiplication, division and modulus.

Okay. Thanks for the answer.

Disappointing…

How old is your build?

When I run this in my Test Lua code window:

local bit = require("bit")

luup.log("###BitAnd###" .. tostring(bit.band(1, 2)))
luup.log("###BitLeftShift###" .. tostring(bit.lshift(1, 2)))[/code]

I get output like:

[code]50	12/20/10 23:05:05.398	luup_log:0: ###BitAnd###0 <0x2c0c>
50	12/20/10 23:05:05.399	luup_log:0: ###BitLeftShift###4 <0x2c0c>

since the [tt]bit.lua[/tt] is just including [tt]nixio.bit[/tt], and it all seems to be present in my [albeit Beta] MiOS version. There is at least hope that it is coming in UI4’s feature pipeline.

Excellent, it does work.

I had misspelled the function. the working code is:

local bit = require(“bit”)
luup.log( "Bit and(15,12) " … tostring(bit.band( 15, 12)) )
luup.log( "Bit lshift(12,3) " … tostring(bit.lshift( 12, 3)) )

where blshift should have been lshift.

thanks!

Code still doesn’t work in an implementation file.

I did discover that if I change the require to:

bitw = require(“bit”)

and then use bitw instead of bit that it works…

please add sticker to vera “handle with care” !!
its been a long time to have worked with such a trial and error environment…