Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
bitwise operations on Android - Gideros Forum

bitwise operations on Android

perrochonperrochon Member
edited April 20 in Code snippets
It's me again... I am sure this has been seen before, but I couldn't find a discussion.

I have tiled generated maps and load them into a Gideros TileMap. Tiled uses the ID of the tile to indicate rotation by setting high bits, which have to be read from the ID, then removed from the ID, then passed on to TileMap. There is code discussing this at tiled, and in the gideros wiki

http://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tile-flipping
https://wiki.giderosmobile.com/index.php/CBump_Template

My problem is that the code similar to the one on that page works in the player on Windows, but not when deployed on Android (Pixel 3)

Here is what I am doing
local FLIPPED_HORIZONTALLY_FLAG = 0x80000000;
local FLIPPED_VERTICALLY_FLAG   = 0x40000000;
local FLIPPED_DIAGONALLY_FLAG   = 0x20000000;
 
--  
-- for each tile ID gid
 
-- Read flipping flags
flipHor = gid & FLIPPED_HORIZONTALLY_FLAG
flipVer = gid & FLIPPED_VERTICALLY_FLAG
flipDia = gid & FLIPPED_DIAGONALLY_FLAG
 
-- Convert flags to gideros style
if(flipHor ~= 0) then flipHor = TileMap.FLIP_HORIZONTAL end
if(flipVer ~= 0) then flipVer = TileMap.FLIP_VERTICAL end
if(flipDia ~= 0) then flipDia = TileMap.FLIP_DIAGONAL end
 
-- Clear the flags from gid so other information is healthy
 
local oldgid = gid -- debug
 
gid = gid & ~ (
FLIPPED_HORIZONTALLY_FLAG |
FLIPPED_VERTICALLY_FLAG |
FLIPPED_DIAGONALLY_FLAG
)
 
local newgid = gid -- debug
 
if oldgid ~= newgid then
  print("flipped gid", oldgid, newgid)
end
If I run this in the Windows player, it works as expected and the output looks about like this
flipped gid	1610612812	76
flipped gid	2684354637	77
If I run it on the Android Gideros player, it flips all the IDs to 0
flipped gid	1610612812	0
flipped gid	77	0
flipped gid	13	0
flipped gid	2684354637	0
Looking for documentation of bitwise operations across different OS? Different architecture? I am pretty sure that my windows box and android are both 64 bit. How do I have to do this so it works across all platforms?
Tagged:

Comments

  • perrochonperrochon Member
    edited April 20
    edit: I got a smaller sample below
  • MoKaLuxMoKaLux Member
    edited April 20
    maybe you need to add the Bitop plugin to your project for it to work on android?
    As hgy29 said: the gideros player includes all the plugins but when you export you need to add the plugins yourself.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • perrochonperrochon Member
    edited April 21
    @MoKaLux thanks for the tip. Doesn't seem to help. I distilled it down to very short code. It doesn't matter if I include the bitop plugin or not (ignore the %x in the string)

    It seems to be using unsigned integers on windows, and signed integers on android for bitwise. If you print out B32 in gideros it's 2147483648 both ways, but maybe for bitwise, the underlying C++ code somewhere does the shift thinking they are signed on Android.
    function f(i) return string.format("%x", i) end
     
    local B1  = 0b00000000000000000000000000000001
    local B31 = 0b01000000000000000000000000000000
    local B32 = 0b10000000000000000000000000000000
     
    print ("B1", f(B1))
    print ("B31", f(B31))
    print ("B32", f(B32))
     
    print ("B1|B31", f(B1|B31))
    print ("B1|B32", f(B1|B32))
     
    print ("B31>>1", f(B31>>1))
    print ("B32>>1", f(B32>>1))
    Windows
    B1	1
    B31	40000000
    B32	80000000
    B1|B31	40000001
    B1|B32	80000001
    B31>>1	20000000
    B32>>1	c0000000
    Android
    B1	1
    B31	40000000
    B32	80000000
    B1|B31	40000001
    B1|B32	7fffffff
    B31>>1	20000000
    B32>>1	3fffffff

    Capture.JPG
    887 x 614 - 79K
  • MoKaLuxMoKaLux Member
    edited April 20
    on my side with your code
    windows
    Uploading finished.
    B1 %x 1
    B31 %x 40000000
    B32 %x 80000000
    B1|B31 %x 40000001
    B1|B32 %x 80000001

    android
    main.lua is uploading.
    Uploading finished.
    B1 %x 1
    B31 %x 40000000
    B32 %x 80000000
    B1|B31 %x 40000001
    B1|B32 %x 80000001

    same!

    Maybe your android player doesn't include the bitop plugin?
    I have a big android player you can try:https://github.com/mokalux/gideros-player-for-android
    Are you testing over wifi?
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • Interesting! I am testing over Wifi. How do I know if my player has the plugin? Wouldn't Gideros upload it if I include it in the project?
  • MoKaLuxMoKaLux Member
    edited April 21
    yes gideros will package the plugin on export but here you are not compiling for export, you are using the plugins of your android player over wifi.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • perrochonperrochon Member
    edited April 21
    Also, I'd like a solution to test for the top bit that is more cross platform, and doesn't require a specific player. Maybe the freedom from number types in Lua gets in the way here?
  • MoKaLuxMoKaLux Member
    edited April 21
    where did you get the android player apk? In the gideros installation folder, on google play https://play.google.com/store/apps/details?id=info.simart.gideros?
    The lighter the apk the less plugins it has :)
    Mine has almost all the plugins 26.3 Mb!
    We could post an up to date android player somewhere here in the forum or in the wiki?
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • I think I got the player that came with the install. Sideloaded the APK.
  • MoKaLuxMoKaLux Member
    I think the bitop plugin is not included in this apk. You can build your own if you want :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • (off-topic) I downloaded the player you linked to from the Play Store. But it doesn't show up in the drop down of Gideros Studio like the one from the Gideros installation folder does. How do I connect to the player from the play store? Can I?
  • MoKaLuxMoKaLux Member
    that should work the same, have you uninstalled the old first? you may have launched to wrong one :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • rrraptorrrraptor Member
    edited April 21
    I have different results too, but with bitop plugin it is the same.
    Windows:
    =====================
    B1	1
    B31	1073741824
    B32	2147483648
    ======= BitOp =======
    B1|B31	1073741825
    B1|B32	-2147483647
    B31>>1	536870912
    B32>>1	1073741824
    ====== Native? ======
    B1|B31	1073741825
    B1|B32	-2147483647
    B31>>1	536870912
    B32>>1	-1073741824
     
    Android:
    =====================
    B1	1
    B31	1073741824
    B32	2147483648
    ======= BitOp =======
    B1|B31	1073741825
    B1|B32	-2147483647
    B31>>1	536870912
    B32>>1	1073741824
    ====== Native? ======
    B1|B31	1073741825
    B1|B32	2147483647
    B31>>1	536870912
    B32>>1	1073741823
    require "bit"
     
    local B1  = 1
    local B31 = 0b01000000000000000000000000000000
    local B32 = 0b10000000000000000000000000000000
    print("=====================")
    print("B1", B1)
    print("B31", B31)
    print("B32", B32)
    print("======= BitOp =======")
    print("B1|B31", bit.bor(B1,B31))
    print("B1|B32", bit.bor(B1,B32))
    print("B31>>1", bit.rshift(B31,1))
    print("B32>>1", bit.rshift(B32,1))
     
    print("====== Native? ======")
    print("B1|B31", B1|B31)
    print("B1|B32", B1|B32)
    print("B31>>1", B31>>1)
    print("B32>>1", B32>>1)

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
  • hgy29hgy29 Maintainer
    Guys, remember that gideros uses lua 5.1, in which all numbers are floats, not integers. Depending on the platform FPU, you may get different results when you exceed that float mantissa resolution.
    To overcome the issue with tiled, I first converted the tiled value to int64 in lua, and they applied bitwise ops on this 64 bit number.

    Likes: antix

    +1 -1 (+1 / -0 ) Share on Facebook
  • perrochonperrochon Member
    edited April 21
    Yes, I feared something like this is the answer. Nice that there is an int64 in Lua. Struggling a bit with the documentation, though.

    http://docs.giderosmobile.com/reference/lua/int64#int64

    So the code now looks like this for reading of the top bits
    gid64 = int64.new(gid)
    local flip = 0
    if gid64[31] ~= gid64[28] then flip = flip | TileMap.FLIP_HORIZONTAL end
    if gid64[30] ~= gid64[28] then flip = flip | TileMap.FLIP_VERTICAL end
    if gid64[29] ~= gid64[28] then flip = flip | TileMap.FLIP_DIAGONAL end
    I tried == 1 but that didn't equal. What do I compare it to? How do I figure out the type?

    Then, to clear the top three bits I do this
    gid64[31]=0
    gid64[30]=0
    gid64[29]=0
    gid = gid64()
    I think I want to put a number back into the gid, and not carry the gid64 forward. but tonumber(gid64) returns nil. is gid64() doing that?

  • hgy29hgy29 Maintainer
    Accepted Answer
    yes, () converts back to an int.
    Here is an extract of my (ugly) code:
    local tgid=int64.new(o.gid)
    local tfliph,tflipv,tflipd=(tgid&#0x80000000)>#0,(tgid&#0x40000000)>#0,(tgid&#0x20000000)>#0
    local tl=(tgid&#0x1FFFFFFF)()

    Likes: perrochon

    +1 -1 (+1 / -0 ) Share on Facebook
  • It's not ugly, just very C++y :-)
  • And it's not even in Wirth Normal Form
    (WNF: no variable can be shortened without generating conflict with another)
Sign In or Register to comment.