Bitwise shift and logical operators in Lua script inside V-REP v3.4

Typically: "How do I... ", "How can I... " questions
Post Reply
ravi
Posts: 85
Joined: 24 Oct 2016, 08:00

Bitwise shift and logical operators in Lua script inside V-REP v3.4

Post by ravi »

This question isn't directly related to V-REP.

I have little experience of programming in Lua. I am trying to convert the following statement from C to Lua language-

Code: Select all

int rgb = r << 16 | g << 8 | b;
From the Google, I found that the following bit = require('bit') library can perform the bitwise operations. The function bit.lshift(x, n) perform bitwise shift operation. However, it shows following error in my machine-

Code: Select all

Lua runtime error: [string "SCRIPT myScript"]:29: module 'bit' not found:
	no field package.preload['bit']
	no file './bit.lua'
	no file '/usr/local/share/lua/5.1/bit.lua'
	no file '/usr/local/share/lua/5.1/bit/init.lua'
	no file '/usr/local/lib/lua/5.1/bit.lua'
	no file '/usr/local/lib/lua/5.1/bit/init.lua'
	no file '/home/ravi/V-REP/lua/bit.lua'
	no file '/home/ravi/V-REP/BlueWorkforce/bit.lua'
	no file '/home/ravi/Desktop/V-REP/bit.lua'
	no file './bit.so'
	no file './libbit51.so'
	no file '/usr/local/lib/lua/5.1/bit.so'
	no file '/usr/local/lib/lua/5.1/libbit51.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
My goal is to write int rgb = r << 16 | g << 8 | b in Lua script inside V-REP v3.4.

Please note that I am using Ubuntu 14.04 LTS 64 Bit OS.

fferri
Posts: 1217
Joined: 09 Sep 2013, 19:28

Re: Bitwise shift and logical operators in Lua script inside V-REP v3.4

Post by fferri »

A left shift by n bits is equivalent to multiplying the number by 2^n.
The resulting numbers have zeros in their lower bits, so you can replace | with +.

Code: Select all

rgb = r * 65536 + g * 256 + b
Also make sure that r, g, and b are in the valid range of 0..255

Post Reply