7

I'm trying to translate a code from C to Lua and I'm facing a problem. How can I translate a Bitwise AND in Lua? The source C code contains:

if ((command&0x80)==0)
   ...

How can this be done in Lua?

I am using Lua 5.1.4-8

7
  • 2
    Have you tried searching on the internet for lua bitwise and or lua bitwise operations? Because they should find you the answer you need. Sep 3, 2015 at 23:51
  • Yes, I seached half hour. I see that a library for bitwise was added in 5.2, but my Router where I'm going to run this code, still run 5.1.4 and it sais that it is up-to-date.
    – Fusseldieb
    Sep 3, 2015 at 23:54
  • 1
    The very first link on my Google (and bing and yahoo) search results is to lua-users.org/wiki/BitwiseOperators which has the information you need. Sep 3, 2015 at 23:56
  • Exactly that same link I already found. But there it says "As of version 5.2, Lua ships with the library [...] Previous versions of Lua did not include bitwise operators."
    – Fusseldieb
    Sep 3, 2015 at 23:58
  • 3
    Did you keep reading? Because it has more than that sentence on the page. Specifically, the next sentence and the entire rest of the page are about bitwise opreations libraries for older versions of lua. Sep 4, 2015 at 0:03

5 Answers 5

10

Implementation of bitwise operations in Lua 5.1 for non-negative 32-bit integers

OR, XOR, AND = 1, 3, 4

function bitoper(a, b, oper)
   local r, m, s = 0, 2^31
   repeat
      s,a,b = a+b+m, a%m, b%m
      r,m = r + m*oper%(s-a-b), m/2
   until m < 1
   return r
end

print(bitoper(6,3,OR))   --> 7
print(bitoper(6,3,XOR))  --> 5
print(bitoper(6,3,AND))  --> 2
5

Here is a basic, isolated bitwise-and implementation in pure Lua 5.1:

function bitand(a, b)
    local result = 0
    local bitval = 1
    while a > 0 and b > 0 do
      if a % 2 == 1 and b % 2 == 1 then -- test the rightmost bits
          result = result + bitval      -- set the current bit
      end
      bitval = bitval * 2 -- shift left
      a = math.floor(a/2) -- shift right
      b = math.floor(b/2)
    end
    return result
end

usage:

print(bitand(tonumber("1101", 2), tonumber("1001", 2))) -- prints 9 (1001)
6
  • These two operands are necessary to control a LCD screen. Now I am half way due to your help :)
    – Fusseldieb
    Sep 4, 2015 at 1:30
  • like in the code comments, to shift left once is to multiply by 2. to shift n times: shifted = a * 2^n Sep 4, 2015 at 1:30
  • I'm confused. You mean that I can delete all parts that contains "b" that the code will work shifting left?
    – Fusseldieb
    Sep 4, 2015 at 1:31
  • no. your C code command = command << 1 is the same as command = command * 2 in Lua. Sep 4, 2015 at 1:38
  • Yes, almost. You do command = math.floor(command / 2) so that you won't get decimals. For example, 3 >> 1 should be 1 and not 1.5 so the floor function removes the decimal portion. Sep 4, 2015 at 1:43
1

Here's an example of how i bitwise-and a value with a constant 0x8000:

result = (value % 65536) - (value % 32768)  -- bitwise and 0x8000
1

This answer is specifically for Lua 5.1.X

you can use

if( (bit.band(command,0x80)) == 0) then
...

in Lua 5.3.X and onwards it's very straight forward...

print(5 & 6)

hope that helped 😉

1
  • 2
    Lua 5.1.X doesn't have this capability unless you are using LuaJIT. Feb 13, 2021 at 5:57
0

In case you use Adobe Lightroom Lua, Lightroom SDK contains LrMath.bitAnd() method for "bitwise AND" operation:

-- x = a AND b

local a = 11
local b = 6
local x  = import 'LrMath'.bitAnd(a, b)

-- x is 2

And there are also LrMath.bitOr(a, b) and LrMath.bitXor(a, b) methods for "bitwise OR" and "biwise XOR" operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.