Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Java code to lua code
Started by HiCorona Dec 25 2018 12:00 AM

- - - - -
11 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

HiCorona

[GLOBAL: userInfoPane.html]
HiCorona
  • Observer

  • 2 posts
  • Corona SDK

java
( 0110 & 0001 )
( 65 & 0xEE )
( byte >> 6 )
( byte << 2 )

lua ?

thanks

 



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,806 posts
  • Enterprise

Lua does not provide bitwise operators in version 5.1, which we use. There however is a plugin called "Bit" that will let you do these bitwise operations.

 

Rob



[TOPIC: post.html]
#3

HiCorona

[GLOBAL: userInfoPane.html]
HiCorona
  • Observer

  • 2 posts
  • Corona SDK

Thanks Rob



[TOPIC: post.html]
#4

aroldopc

[GLOBAL: userInfoPane.html]
aroldopc
  • Enthusiast

  • 36 posts
  • Corona SDK

When is Corona going to be upgraded to Lua 5.3?
I tried to compile and install the BitOp from Mike Pall bit I was not successful installing the plug in. i asked for Mike help but there is something wrong with the Install script .
ld: symbol(s) not found for architecture x86_64
Aroldos-MBP:LuaBitOp-1.0.2 aroldocarvalho$ sudo make install
gcc -shared -fPIC -o bit.so bit.o
Undefined symbols for architecture x86_64:
"_luaL_error", referenced from:
_luaopen_bit in bit.o
"_luaL_register", referenced from:
_luaopen_bit in bit.o
"_luaL_typerror", referenced from:
_luaopen_bit in bit.o
_bit_tobit in bit.o
_bit_bnot in bit.o
_bit_band in bit.o
_bit_bor in bit.o
_bit_bxor in bit.o
_bit_lshift in bit.o
...
"_lua_gettop", referenced from:
_bit_band in bit.o
_bit_bor in bit.o
_bit_bxor in bit.o
"_lua_isnumber", referenced from:
_luaopen_bit in bit.o
_bit_tobit in bit.o
_bit_bnot in bit.o
_bit_band in bit.o
_bit_bor in bit.o
_bit_bxor in bit.o
_bit_lshift in bit.o
...
"_lua_pushlstring", referenced from:
_bit_tohex in bit.o
"_lua_pushnumber", referenced from:
_luaopen_bit in bit.o
_bit_tobit in bit.o
_bit_bnot in bit.o
_bit_band in bit.o
_bit_bor in bit.o
_bit_bxor in bit.o
_bit_lshift in bit.o
...
"_lua_tonumber", referenced from:
_luaopen_bit in bit.o
_bit_tobit in bit.o
_bit_bnot in bit.o
_bit_band in bit.o
_bit_bor in bit.o
_bit_bxor in bit.o
_bit_lshift in bit.o
...
"_lua_type", referenced from:
_bit_tohex in bit.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [bit.so] Error 1
Aroldos-MBP:LuaBitOp-1.0.2 aroldocarvalho$

[TOPIC: post.html]
#5

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,806 posts
  • Enterprise

We have no plans to update Corona to Lua 5.3. It would break too many things and too many people's apps.

 

Corona is now open source, so if someone in the community wanted to try and update it to 5.3 (and adapt all of our custom changes) and do so with minimal to no breaking changes, we might consider accepting the pull request.

 

Rob



[TOPIC: post.html]
#6

aroldopc

[GLOBAL: userInfoPane.html]
aroldopc
  • Enthusiast

  • 36 posts
  • Corona SDK

Rob Thank you.

I wrote this code to emulate the bitwise AND and OR.

Can you or some one on the community make changes to this code to be more efficient?

local function dec2bin(n)
  local result = ''
  while n ~= 0 do
    if n % 2 == 0 then
      result = '0' .. result
    else
      result = '1' .. result
    end
    n = math.floor(n / 2)
  end
  return result
end
local function band(bit1,bit2)
	local num1 = dec2bin(bit1)
	local num2 = dec2bin(bit2)
	num1 = string.format("%08s", num1)
	num2 = string.format("%08s", num2)
	print("num1:"..num1.." num2:"..num2)
	local band = "", bitc
	for i=1, #num1 do
		local bita = num1:sub(i,i)
		local bitb = num2:sub(i,i)
		if bita == '0' and bitb == '0' or 
			bita == '0' and bitb == '1' or 
			bita == '1' and bitb == '0'then 
				bitc = '0' band = band..bitc
		elseif bita == '1' and bitb == '1' then 
			bitc = '1' band = band..bitc
		end

		print ("bita:"..bita.." bitb:"..bitb)
	end
	return tonumber(band)
end
local function bor(bit1,bit2)
	local num1 = dec2bin(bit1)
	local num2 = dec2bin(bit2)
	num1 = string.format("%08s", num1)
	num2 = string.format("%08s", num2)
	print("num1:"..num1.." num2:"..num2)
	local bor = "", bitc
	for i=1, #num1 do
		local bita = num1:sub(i,i)
		local bitb = num2:sub(i,i)
		if (bita == '0' and bitb == '1') or 
			(bita == '1' and bitb == '0') or 
			(bita == '1' and bitb == '1') then 
				bitc = '1' bor = bor..bitc
		elseif (bita == '0' and bitb == '0') then 
			bitc = '0' bor = bor..bitc
		end

		print ("bita:"..bita.." bitb:"..bitb)
	end
	return tonumber(bor)
end
function bin2dec(num)
	print ("num:"..num)
	local rem, dec , base = 0,0,1
	while (num > 0) do
        rem = math.floor(num % 10)
        dec = dec + rem * base
        num = num / 10 
        base = base * 2
    end
    --print ("dec:"..dec)
    return dec
end

local res = band(10,2)
print ("res:"..res)
res = bin2dec(res)
print ("band:"..res)
local res2 = bor(10,5)
print ("bor:"..res2)


[TOPIC: post.html]
#7

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,337 posts
  • Enterprise

I wrote this code to emulate the bitwise AND and OR.

Can you or some one on the community make changes to this code to be more efficient?

 

Why bother???  Use bit plugin instead - it's tested, and has native performance.  Done.

 

if you really truly legitimately need a pure-lua solution then look around here

(or, also welcome to borrow from my pure-lua mersenne twister here)



[TOPIC: post.html]
#8

aroldopc

[GLOBAL: userInfoPane.html]
aroldopc
  • Enthusiast

  • 36 posts
  • Corona SDK

dave bollinger,

 

Mike, built the source with macosx 

 

my directory structure is this

-Root

 -Corona

 -LuaBipOp-1.0.2

 

 

Now the install does this error:

 

install -p bit.so `lua installpath.lua bit`

/bin/sh: lua: command not found

usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]

               [-o owner] file1 file2

       install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]

               [-o owner] file1 ... fileN directory

       install -d [-v] [-g group] [-m mode] [-o owner] directory ...

 

make: *** [install] Error 64



[TOPIC: post.html]
#9

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,807 posts
  • Corona SDK

When davebollinger says plugin, he means this:

 

https://docs.coronalabs.com/plugin/bit/index.html



[TOPIC: post.html]
#10

aroldopc

[GLOBAL: userInfoPane.html]
aroldopc
  • Enthusiast

  • 36 posts
  • Corona SDK

nick_sherman, thanks for your reply.

 

You are correct but you have to build and install the BitOp plug-in on the machine that is running Corona.

I was able to build it, but I am having the above error when I try to install.



[TOPIC: post.html]
#11

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,807 posts
  • Corona SDK

I'm pretty sure the point of the Corona plugin is that you don't have to do that - all the heavy lifting has been done.

 

You add the plugin to build.settings, require it and you're away.



[TOPIC: post.html]
#12

aroldopc

[GLOBAL: userInfoPane.html]
aroldopc
  • Enthusiast

  • 36 posts
  • Corona SDK

nick_sherman,

 

Thank you and all that helped , that worked. For some reason I miss understood the instructions.




[topic_controls]
[/topic_controls]