• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Bit operator conversion?

Neptune

Member
I'm struggling to wrap my head around converting this code. I'm very new to bit-wise operations, and no where near ready to understand how to convert this mathematically.
Any help appreciated. I would like to have this same code, but with no bit operations / bit shifting. I tried a few suggestions and the values ended up being undefined for some reason 🤔

All variables are just local within a function/script

sx, cx, xx, sy, cy, yy are all integers


GML:
for(var a = -1; a < 2; a++)
{
     for(var i = -1; i < 2; i++)
     {
         sx = cx+i;
         sy = cy+a;
         if condition
         {
             ds_list_add(rnd_list, sy << 16 | sx);
         }
     }
}

var rand_cell = irandom_range(0,ds_list_size(rnd_list)-1);
var rnd = rnd_list[| rand_cell];

cx = rnd & $FFFF;
cy = rnd >> 16;
There are the two shifts, and a clamp of sorts with the & $FFFF... I think?
 
Last edited:

TheouAegis

Member
ds_list_add(rnd_list, sy << 16 | sx);
This adds (sy*65536)+sx to the list. This is only "valid" if sx is between 0 and 65535, meaning it requires 16 bits, so shifting sy by 16 bits makes it so you can add the two values together without them affecting each other.


cx = rnd & $FFFF;
This retrieves the 16 bit value of cx from rnd.


cy = rnd >> 16;
This retrieves the value of cy from rnd.
 

rytan451

Member
As a note, the code cx = rnd & $FFFF; can now also be written (in GMS 2.3+) as cx = rnd & 0xFFFF. The 0x is actually better, since the dollar sign is actually an accessor, so some_array[$FFFF] would cause an error (undeclared variable FFFF, and also some_array isn't a struct but a struct accessor is being used with it) while some_array[0xFFFF] would work as expected (equivalent to some_array[65535].
 
Top