Lua bitshift/bitmask on 16 bit integer

Hi,

I have a 16bit integer and now want to exact first the lower 8 bits as number and then the upper 8 bits as number.

I was trying:

tonumber(noteColumn.effect_number_value) >> 8

and

tonumber(noteColumn.effect_number_value) & 0xFF

but both does not seem to work. What am I doing wrong?

Maybe you can convert to string and split
then reconvert to numbers

bricolage

Have you tried

fx=tonumber(noteColumn.effect_number_value)
low=bit.band(fx,0xff)
high=bit.band(bit.rshift(fx,8),0xff)
1 Like

Binary is too low level for me

Your method is much more faster for CPU

This i why I said ā€˜bricolage’ :slightly_smiling_face:

The biggest joke is that euclidean rhythm is just bit shifting

It looks like the latest Renoise was built with lua 5.1, so it doesn’t have the bitwise operators from lua 5.3.

A less CPU friendly (but shorter) version maybe:

low=fx%0x100
high=math.floor(fx/0x100)
1 Like

This isn’t correct for now. Latest Renoise comes with LuaJIT, so it does ship with bit (note, the bit module was available in previous versions as well but it was added by Renoise before whereas now it is provided by LuaJIT).

Bitwise infix operators don’t exist but you can do bit.lshift and so on, see the bit module docs for more (you don’t have to require the module, bit is in global scope for tools)

1 Like

Hi @unless I love your tools, I have a few installed :slight_smile:

Ok, I was just going by the result of ā€œprint(_VERSION)ā€ which gave ā€œ5.1ā€. I didn’t know the team was adding the bit module themselves.

Cheers!

Yeah, LuaJIT somewhat confusingly reports being lua 5.1, I guess it is largely compatible with that so can be treated as such. You can try running print(jit.version) in Renoise 3.5 to get the actual LuaJIT version being used.

Thx @euel and @unless, very helpful!

Anyway I’ve now took a different approach. I think my main confusion was about noteColumn vs effectColumn. Notecolumn also contains effect columns, but those are empty… :thinking: Or whatever. Now it’s kinda fixed:

You don’t have to use tonumber, just use the _value fields instead of the _string ones. The values already have the integer you are after, adding strings and parsing as hexadecimal is unnecessary.

These are the same thing

fxColumn.number_value == tonumber("0x" .. fxColumn.number_string, 16)
fxColumn.amount_value == tonumber("0x" .. fxColumn.amount_string, 16)

Note, you might want to verify that the midi message is actually a valid one before exporting it, that is, the Mx should be in the right-most note column and both the param index and the cc value is in the range of 0-127.

For example out of range values for the fx column here might give you nil as a result of the tonumber or give you a nonexistent midi parameter.

1 Like