Skip to content Skip to sidebar Skip to footer

How To Read Off 1 Flag Bit And Get Integer At Same Time

Say I have an 8-bit number with a flag at either side: 0101011 (decimal 43) 0101011 (decimal 43) Not sure which is more optimal for what's next. Then say I

Solution 1:

You could do something like this using the flag at the end:

var num = 0b01010110;
var flag = num & 1;
var int = num >> 1;
console.log(flag);
console.log(int);

But binary operations and bit flags in general are kind of an abuse of JavaScript since it doesn't really have integer types. If you do need to do it though, it seems like a flag at the end would be more reliable since you can't explicitly guarantee a number to be 8 bits by its type. This way the flag will always be in the same position and the rest will always be your number.

Post a Comment for "How To Read Off 1 Flag Bit And Get Integer At Same Time"