[Dev] Bit Switches - using a variable to store a bunch of switches

patreon_bitSwitches.png


The image above illustrates how you can turn a number into a set of switches.

My "Bit Switches" plugin provides a set of script calls to easily manage these switches.

Say you had 50 chests throughout your dungeon, and you wanted to track whether you opened these chests or not globally, since they affect other parts of the game.

Do you necessarily want to reserve 50 switches, one for each chest?

Chest 1 OpenedChest 2 OpenedChest 3 Opened...Can you just use a single variable to accurately determine whether each chest has been opened or not?This is what bit switches are for.
Its funny but I just read up on this yesterday. 😛

I think that this could be pretty useful.
Its funny but I just read up on this yesterday. 😛

I think that this could be pretty useful.
Let me know if this logic is correct lol

CVhe0M4UYAAuOF0.png


I'm looking for a way to check if multiple switches are OFF using a bitmask.

My logic is

1. AND mask the input. This will turn off irrelevant bits while leaving the bits I want intact

2. NEGATE the resulting input. This will turn all bits ON.

3. The ones that were already OFF will now be ON. The ones that were ON will now be off. By comparing it with my desired mask, if the ones I wanted OFF were originally OFF, then they should be the only ones ON now?

So for example let's say I started with

0b1001

Which means the first and 4th switches are ON, and I want to check if 1st and 2nd bits are OFF.

0b1001 & 0b0011 gives 0b0001~0b0001 gives 0b11100b1110 & 0b0011 gives 0b0010Which does not match my desired mask.Therefore, 1st and 2nd bits were NOT originally off.

Maybe there's an easier way to do it.
wouldn't an easier way be to do something like this?

Code:
flag = 3;if (0b1000 & flag){ //because 3 is binary 0011 this will evaluate to false//do stuff};
wouldn't an easier way be to do something like this?

flag = 3;if (0b1000 & flag){ //because 3 is binary 0011 this will evaluate to false//do stuff};
I might be over-complicating things.

Here are my use cases for multiple bit checking (assuming AND logic)

1. bit 1 and 2 are ON (both ON)

2. bit 1 and 2 are OFF (both OFF)

3. bit 1 is ON, bit 2 is OFF

The third one can be broken down into two parts, so it's really just two cases.

I don't think I can handle it using a simple if/else...

So for example if I wanted to check that 0b0011 switches are OFF, this would also evaluate to true

Code:
0b0010 & 0b0011
If you want to check one bit at a time, it is as easy as 0b1111 & flag. Checking multiple really won't be much different, you'll just want to check that they are all on this time, not just nonzero (because some bits could be set while some aren't, and you can get a nonzero). In this case, I'd opt to use

(0b1111 & flag) === flag

Assuming that flag is an OR'd together number for the bits you want to check.
I could be missing something but wouldn't (0b1111 & flag) === flag always evaluate to true if just one flag bit was a one?

I'm going to have to think on the logic of this (still to new to this concept lol).
Now that I think about it, while it is an interesting brain exercise, it may not be AS practical.

If you're planning to treat a variable as a bit-array anyways and will be managing it using script calls, you might as well just use an actual array...

Though I like the idea of using bit masks to check if multiple switches are ON, since it's very fast to say

0b101011To check whether bits 1, 2, 4, and 6 are ON.You could even assign it to a string so that you can identify it by name

masks["those 6 chests"] = 0b101011And then refer to "those 6 chests" by name in all of your code.But then you could do the same thing with arrays.

areSwitchesOn([1,2,4,6])masks["those 6 chests"] = [1,2,4,6]areSwitchesOn("those 6 chests")Just seems like it makes MORE SENSE to use arrays in the end.Anyways, here's what I have for the two cases

1. To check if the specified bits are ON, Zalerinian's approach works

0b1101 & 0b1001 == 0b1001The bits that you want to be ON, must be ON in order to equal your mask.2. To check if the specified bits are OFF, we can just negate the input, and then just check if they're ON.

For example, I want the first two bits to be ON

Code:
~0b1100 & 0b0011 == 0b0011
I think that should work.
This all seems really cool if you ask me!  BD
if 0 is off, then you compare to 0.

if (0bnnnn & 0b0011 == 0) // both switches are off

if (0bnnnn & 0b0011 == 0b0011) // both switches are on

if (0bnnnn & 0b0011) // at least one switch is on
I am going to cast resurrection on this plugin idea...

Bit masks, will help keep conditional flagging and checking manageable when needing to manipulate complex combinations of switches.

Especially when compared to event commands. Interacting with an event may check a combination of 3 switches and in turn flag up to 3 others... All done in a few script calls. Rather than a few dozen conditional commands. Multiplied over dozens of events... And something like this plugin could save a ton of time and effort.
The idea of using a "named mask" is the direction I'm going right now.

For example, let's say you had a mask called "three apples"

This checks whether three specific switches are turned on.

It adds an extra layer of abstraction that allows you to manage those three switches in a single location, rather than putting it in every event.

So your condition can be

Switch.mask("three apples") === trueAnd then the game would look up what that means in the mask table, and apply the appropriate checks.The underlying implementation MAY use bit-switches, or it may just use arrays of regular switches.
The main problem with bitwise operations (in my eyes) is that you can only store 32 bits in an integer before reaching the maximum integer size. Also, bitwise operators on numbers are actually kind of slow, since they have to be converted between floats and integers.


For more information, see here: http://www.i-programmer.info/programming/javascript/2550-javascript-bit-manipulation.html


You can find a few sample operations using bitwise operators here: http://stackoverflow.com/questions/1436438/how-do-you-set-clear-and-toggle-a-single-bit-in-javascript
The main problem with bitwise operations (in my eyes) is that you can only store 32 bits in an integer before reaching the maximum integer size.
Javascript supports up to 53-bit integers, but that's not much of an improvement.

That's the main reason why I'm considering just storing an array of numbers, rather than using bits in a number directly.

There's also the possibility of using a string to represent a bit array, but I'm not sure how that might work, or what the performance would be compared to simply doing a loop over an array:

"10001101010101" There's also the possibility of using bit-wise operations for any masks that are smaller than 53 bits, and alternatives otherwise in case you actually need more than the provided number of bits.

Also, bitwise operators on numbers are actually kind of slow, since they have to be converted between floats and integers.
I have seen that claim made in several stackoverflow questions on the performance of bitwise operators, but no references were provided to show that such conversion is required, or how much slower it is compared to straight comparison or other alternatives.

What 0xABCD represents as a float or an integer does not seem relevant when all you care about is what happens when you change the bits.
If you are using an array of numbers, you would need to split the provided mask and decide, which number to use.


Using Strings doesn't make sense, since bitwise operations will convert them to their number representation (so, you win nothing) and accessing string indizes wins you nothing over using an array of booleans.


In the end, i don't think it's worth the effort for 1-of-a-kind applications like global switches. It does make sense for flags, like RM's tileset, which would use up a lot more memory, if they were implemented as arrays.
If you are using an array of numbers, you would need to split the provided mask and decide, which number to use.

Using Strings doesn't make sense, since bitwise operations will convert them to their number representation (so, you win nothing) and accessing string indizes wins you nothing over using an array of booleans.

In the end, i don't think it's worth the effort for 1-of-a-kind applications like global switches. It does make sense for flags, like RM's tileset, which would use up a lot more memory, if they were implemented as arrays.
Well, performance is a secondary consideration here. It would just be nice to have a very fast solution lol

My current solution is to use a hash-like structure

Code:
myMask = { 1: true, 3: false, 5: true}
And then you would use that to check an array of booleans which represent switches.
What exactly would be the benefit of using an additional object compared to the existing switches, which are stored in an array (also an object)?

You could just build on top of the existing Game_Switches and provide new methods, like this:

Code:
Game_Switches.prototype.any = function() {    return Array.prototype.some.call(arguments, function(a) { return this._data[a]; }, this);};Game_Switches.prototype.all = function() {    return Array.prototype.every.call(arguments, function(a) { return this._data[a]; }, this);};
and use them like this:
Code:
$gameSwitches.all(1, 2, 3);  // Returns true, if switches 1, 2 and 3 are ON.
What exactly would be the benefit of using an additional object compared to the existing switches, which are stored in an array (also an object)?

and use them like this:

$gameSwitches.all(1, 2, 3); // Returns true, if switches 1, 2 and 3 are ON.
It would be like this

Code:
Masks["myMask"] = { 1: true, 2: false, 3: true}$gameSwitches.maskValue("myMask")
Which returns true or false if all of the required states are met.
Implementation could be like this:

Code:
GameSwitches.prototype.maskValue = function(mask) {    return Object.keys(mask).every(function(key) { return this.value(key) === !!mask[key]; }, this);};
You will probably get better performance, when manually iterating over the mask, but i just wanted a short solution ^^

Original thread: https://forums.rpgmakerweb.com/threads/dev-bit-switches-using-a-variable-to-store-a-bunch-of-switches.52329/