• 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!

Bitboards: why is MSB equal to -1

Bentley

Member
I am (attempting) to work with bitboards. Does anyone know the reason the most significant bit, when on, equals -1 instead of 1.
Ex:
Code:
// A 64 bit int.
// Every bit is on except the first 4 bits on the LSB / left side
bb = 0xFFFFFFFFFFFFFFF0;
show_message(sign(bb & (1 < 0)));   // Got 0 as expected
show_message(sign(bb & (1 << 62))); // Got 1 as expected
show_message(sign(bb & (1 << 63))); // Got -1.
It's workable, as I can just check for -1 or 1 to see if a bit is on. But I feel like I'm doing something wrong as I've never seen a check for a -1 bit on any of the "bit tricks" I've seen.

I am relatively new to all this. Anyone know what's going on?
 

Bentley

Member
This is because the last bit always determines + or -
This applies for any "signed" bit value.
Well thanks for letting me know. It's nice to know that's the way it is versus a mistake on my part.

With that said, I looked at a lot of bit operations, and none checked for a -1 for the last bit. Which makes me wonder how those operations checked whether the MSB was on.

As an example, this is how I have a "bit_get" function.
Code:
var bb, n, Sign;

bb = argument0;
n  = argument1;

Sign = sign(bb & (1 << n));

return (Sign == 1 || Sign == -1); // Return true if the bit is 1 or -1. Return false if the bit is 0.
If you don't mind, how would you go create a bit_get function (a function that checks whether any bit is on). Would you include the -1 check? Thanks again for the reply as this is pretty new territory so this really helps.
 
Last edited:

chamaeleon

Member
Well thanks for letting me know. It's nice to know that's the way it is versus a mistake on my part.

With that said, I looked at a lot of bit operations, and none checked for a -1 for the last bit. Which makes me wonder how those operations checked whether the MSB was on.

As an example, this is how I have a "bit_get" function.
Code:
var bb, n, Sign;

bb = argument0;
n  = argument1;

Sign = sign(bb & (1 << n));

return (Sign == 1 || Sign == -1); // Return true if the bit is 1 or -1. Return false if the bit is 0.
If you don't mind, how would you go create a bit_get function (a function that checks whether any bit is on). Would you include the -1 check? Thanks again for the reply as this is pretty new territory so this really helps.
Perhaps simply just see if the result is not zero?
Code:
var bb, n;
bb = argument0;
n = argument1;
return (bb & (1 << n)) != 0;
Also posted https://forum.yoyogames.com/index.p...tion-and-display-functions.64855/#post-388601 a while back.
 

Bentley

Member
Perhaps simply just see if the result is not zero?
Code:
var bb, n;
bb = argument0;
n = argument1;
return (bb & (1 << n)) != 0;
Also posted https://forum.yoyogames.com/index.p...tion-and-display-functions.64855/#post-388601 a while back.
THx for the reply and link. I will study that.

So you're thinking...
Code:
/// @function bit_get(bitboard, index)
/// @arg bitboard
/// @arg index

var bb, n;

bb = argument0;
n  = argument1;

return (sign(bb & (1 << n) != 0));
Basically, if the bit is 1 or -1, it returns true, otherwise it returns false.

Edit: and it's funny, I wrote a bit_string script identical to yours just so I could visualize the bitboard in the compiler.

Edit: nevermind, question answered.
 
Last edited:

chamaeleon

Member
THx for the reply and link. I will study that.

So you're thinking...
Code:
/// @function bit_get(bitboard, index)
/// @arg bitboard
/// @arg index

var bb, n;

bb = argument0;
n  = argument1;

return (sign(bb & (1 << n) != 0));
Basically, if the bit is 1 or -1, it returns true, otherwise it returns false.

Edit: and it's funny, I wrote a bit_string script identical to yours just so I could visualize the bitboard in the compiler.

Edit: nevermind, question answered.
No need for the sign() function. The comparison for not-zero is sufficient.
 

TheouAegis

Member
The bit is 1 or 0. The sign of a number is not the sign of a bit. 1<<63 is not -1; -1 is $ffffffffffffffff. Your initial value of $fffffffffffffff0 is -16.
 
Top