Decoding then re encoding bit masks on int with bitwise operations

0

so I have two flags, and I can decode them correctly... but when I try to encode it again.. I'm getting a negative number.. but when I try to decode that negative number it decodes it correctly... so I'm confused why it's working but isn't getting the expected result

const encoded = 3221225525;
const  FLIPPED_HORIZONTALLY_FLAG = 0x80000000;
const  FLIPPED_VERTICALLY_FLAG   = 0x40000000;
const  FLIPPED_DIAGONALLY_FLAG   = 0x20000000;

function decodeTileId(tileId){
    let flipHor = !!(tileId & 0x80000000);
    let flipVer = !!(tileId & 0x40000000);
    let flipDiag = !!(tileId & 0x20000000);
    tileId &= ~(FLIPPED_HORIZONTALLY_FLAG |
        FLIPPED_VERTICALLY_FLAG |
        FLIPPED_DIAGONALLY_FLAG);

    return { tileId, flipDiag, flipVer, flipHor }
}


var obj = decodeTileId(encoded);
var { tileId, flipVer, flipHor, flipDiag } =obj;
//tile id is 53 here and flags are correct.

if(flipVer && flipHor) { // these were the flags set to true... try to re-encode
  tileId |= FLIPPED_VERTICALLY_FLAG;
  tileId |= FLIPPED_HORIZONTALLY_FLAG;
 //tileId becomes -1073741771 which if i put through the decode function... i get the same result as 3221225525
}
bitmap
bit-manipulation
asked on Stack Overflow May 24, 2020 by gottimaneee

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0