Lua version?

what version of Lua does codea use? over on the Lua downloads, the source code for 5.2.3 were released on 11/11/2013.

5.1

Wow. I need bitwise OPs too. I can get away with

if x > 127 

to catch the top bit, but not the best solution. ( I know I have an unsigned 8-bit int.)

On my wish list is ability to easily index bytewise into a userdata object to read/write it, but that’s probably asking far too much. :slight_smile:

This is what I use as bitwise operators

op = class()

function op:init()

end

function op:andd(a, b)
    c = 0
    for i = 0, 50 do
        c = c + (a%2) * (b%2) * (2^i)
        
        a = math.floor(a/2)
        b = math.floor(b/2)
    end

    return c
    
end

function op:nott(a, b)
    -- b is the bit-range, standard = 16
    b = b or 16
    return 2^b - 1 - a
end

function op:orr(a, b, c)
    -- c = bit-range
    return op:nott(op:andd(op:nott(a, c), op:nott(b,c)), c)
end

function op:xor(a,b , c)
    -- c = bit-range
    return op:andd(op:orr(a,b,c), op:nott(op:andd(a,b), c))
end

i don’t really know how to explain the bit range…

it’s like, if you’d execute a bitwise not on let’s say… 3, then it would return 65532 if the bit range was standard 16 bit, and 252 if the bit range was set to 8… if you get what I mean…?

Any ideas when support for 5.2.3 will be released? I created a binary class to handle bitwise operations in the meantime.

Binary = class()

function Binary:int2bit(number)
    local len = 0
    print( "Binary:int2bit:", number )
    while( number > 0 ) do
        self.data[len] = number % 2
        number = ( number - (number % 2) ) /2
        --number = number / 2
        --print( "   ", number )
        len = len + 1
        self.length = len
    end
    return self.data
end

function Binary:bit2int( bitStruct )
    n = 0
    local add = 0
    for i=0, bitStruct.length do
        if( bitStruct[i] == 1 ) then
            add = 2 ^ i
        else
            add = 0
        end
        n = n + add
    end
    return n  
end

function Binary:str2bit( str )
    -- convert a string like "1110001010010101010010" into a binary struct
    bin = Binary()
    print( "Binary:str2bit(",str,")" )
    for i=1, string.len( str ) do
        --print( i, string.sub(str, i, i ) )
        bin.data[i-1] = string.sub( str, i, i )
        bin:incLen()
    end 
    return bin
end

function Binary:padbits( bitStruct, bits )
    print( "Binary:padbits length", bitStruct.length )    
    for i=0, bits-1 do 
        if( bitStruct.data[i] == nil ) then
            bitStruct.data[i] = 0
            bitStruct:incLen()
        end
    end
    bitStruct:printStruct(1)
    return bitStruct
end

function Binary:printStruct(indent)
    indent = indent or 0
    local str = ""
    local str1 = ""
    for i=1, indent do
        str1 = str1 .. "    "
    end
    for i=0, self.length - 1 do
        --print( i, self.data[i] )
        str = str .. self.data[i]
        if( i % 4 == 3 ) then str = str .. " " end
    end
    print( str1, "Binary:printStruct(): ", str )
    return self
end

function Binary:pad8bits( bitStruct )
    return bitStruct:padbits( bitStruct, 8 )
end

function Binary:pad16bits( bitStruct )
    return bitStruct:padbits( bitStruct, 16 )
end

function Binary:pad32bits( bitStruct )
    return bitStruct:padbits( bitStruct, 32 )
end

function getMaxLength( struct1, struct2) 
    if( struct1.length >= struct2.length ) then
        return struct1
    else
        return struct2
    end
end



--structs might not be the same length, so we gotta make copies and pad them so thy're the same length
--whichever struct is longer defines the length of the new array
function Binary:bitAND( struct1, struct2 )
    local len = getMaxLength( struct1, struct2 ).length
    local bin = Binary()
    for i=0, len-1 do
        if( struct1.data[i] ~= nil and struct2.data[i] ~= nil ) then
            if( struct1.data[i] == 1 and struct2.data == 1 ) then
                bin.data[i] = 1
            else
                bin.data[i] = 0
            end
        else
            bin.data[i] = 0
        end
        bin:incLen()
    end
    return bin
end

function Binary:bitOR( struct1, struct2 )
    local len = getMaxLength( struct1, struct2 ).length
    --if a[i] == 0 and b[i] == 0, bin[i] = 0 else bin[i] = 1
    bin = Binary()
    for i=0, len-1 do
        if( struct1.data[i] ~= nil and struct2.data[i] ~= nil ) then
            if( struct1.data[i] == 0 and struct2.data[i] == 0 ) then 
                bin.data[i] = 0
            else
                bin.data[i] = 1
            end
        else
            bin.data[i] = 1
        end
        bin:incLen()
    end
end

function Binary:bitNOT( struct )
    local bin = Binary()
    for i=0, self.length do
        if( self.data[i] == 1 ) then bin.data[i] = 0 end
        if( self.data[i] == 0 ) then bin.data[i] = 1 end
        bin:incLen()
    end
    return bin
end        


function Binary:bitXOR( struct1, struct2 )
    -- [00 0] [01 1] [10 1] [11 0]
    local len = getMaxLength( struct1, struct2 ).length
    bin = Binary()
    for i=0, len-1 do
        if( struct1.data[i] ~= nil and struct2.data[i] ~= nil ) then
            if( struct1.data[i] == 0 and struct2.data[i] == 0 ) then 
                bin.data[i] = 0
            elseif( struct1.data[i] == 0 and struct2.data[i] == 1) then
                bin.data[i] = 1
            elseif( struct1.data[i] == 1 and struct2.data[i] == 0) then
                bin.data[i] = 1
            else
                bin.data[i] = 0
            end
        else
            bin.data[i] = 1 
        end
        bin:incLen()
    end
end

function Binary:shiftLeft( struct, num )
end

function Binary:shiftRight( struct, num )
    
end

-- returns the low 4/8/16 bits 
function Binary:Low()
    num = self.length / 2
    rtn = Binary()
    --zero based array
    for i=0, num-1 do
        rtn.data[i] = self.data[i]
        rtn:incLen()    end
    return rtn
end

--returns the high 4/8/16 bits
--copy n[len-1] to n[num] into new array
function Binary:High()
    if( self.length <= 4 ) then 
        return self
    end
    num = self.length / 2
    --print( "Binary:high() length", self.length, num ) 
    rtn = Binary()
    for i=num, self.length-1 do
        --print( "Binary:High()", i, self.data[i] )
        rtn.data[i-num] = self.data[i]
        rtn:incLen()
    end
    return rtn
end


function Binary:incLen()
    self.length = self.length + 1
end

function Binary:decLen()
    assert( self.length > 0 )
    self.length = self.length - 1
end

function Binary:init(x)
    -- you can accept and set parameters here
    self.data = {}
    self.length = 0
end

function Binary:draw()
    -- Codea does not automatically call this method
end

function Binary:touched(touch)
    -- Codea does not automatically call this method
end

-- Binary
------------------- This is the "main" tab
-- Use this function to perform your initial setup
function setup()
    int = Binary()
    int:int2bit(25)
    int:printStruct(0)
    int:pad8bits( int )
    int:pad16bits( int )
    int:pad32bits( int )
    int:printStruct(0)
    int = int:High():printStruct(0)
    print( int )
     --int:printStruct(0) 
end

-- This function gets called once every frame
function draw()
    -- Do your drawing here
    
end

it works pretty well because it’s not string based, and also 0-indexed arrays work just fine. So, any comments on that oddity?

I agree that bitwose operations are missing. I use something like this in some code

function bit(p)
  return 2 ^ (p - 1)  -- 1-based indexing
end
 
-- Typical call:  if hasbit(x, bit(3)) then ...
function hasbit(x, p)
  return x % (p + p) >= p       
end
 
function setbit(x, p)
  return hasbit(x, p) and x or x + p
end
 
function bitor(a, b) 
  local res = 0
  for i = 1,4 do
    if hasbit(a, bit(i)) or hasbit(b, bit(i)) then
      res = setbit(res, bit(i))
    end
  end
  return res
end

I guess there are some issues on making the latest lua run smoothly on iOS, no idea why though.