Bit flags/indicators are extra practicle in many ways:
- They take little space (1 bit as opposed to 4 bytes for an INT)
- Stacking bit indicators in a file is just like compressing, you're saving 32 times the space that you'd usually take with INT values.
- Bit indicators are harder to hack into, as they are hidden in bytes and ints.
- grouping them in an INT makes it for easy maintenance of all those indicators
With using the bits that are located within an int value, all you need to define is your flip indicator values by using simple boolean and/or math calculations. This way, a full row of flip indicators can be stored within each int value, making them (1)very small, (2) practical for writing into files, (3) easy to maintain.
How does it work? Here is a simple look at how you get/set bit values from a "32 bit" int:
An example of bit flipping:
Let's say that you're making a game that involves flipping tiles. But, at some point, your game needs to be informed about which tiles have been flipped. Let's presume that you have a grid of 25 tiles by 25 tiles. All there is to store/retrieve is this singular value of 'true' or 'false'.
The usual common method would be to define a table with the DIM instruction, such as 'Dim tiles%(25, 25)'. Let's say that we need to write this into a file. Basically, what we will do would be to create a routine containing two embedded "For...Next" loops. And within that loop, we put a write instruction of the int values (containing simply 1's and 0's) down to a file.
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
Now, here is a more efficient way. For our tile example, we will initiallize our table in the following manner:
The reason we put % is to see the bits that we're initializing. That way, we know what bits are equal to what values. If we put some bits to 1 (True) or 0 (False), it becomes quite easier and more accessible this way.
Next, how to look at the bits: As seen on the first code snippet, we can interrogate the int values and see what they contain. It's always good to make some small functions that helps us perform modifications tasks to our table or to interrogate their contents. Here is an example code:
Finally, we make a function to permit tile flipping!
Thanks for reading! Hope this was helpful and easy to learn.
- They take little space (1 bit as opposed to 4 bytes for an INT)
- Stacking bit indicators in a file is just like compressing, you're saving 32 times the space that you'd usually take with INT values.
- Bit indicators are harder to hack into, as they are hidden in bytes and ints.
- grouping them in an INT makes it for easy maintenance of all those indicators
With using the bits that are located within an int value, all you need to define is your flip indicator values by using simple boolean and/or math calculations. This way, a full row of flip indicators can be stored within each int value, making them (1)very small, (2) practical for writing into files, (3) easy to maintain.
How does it work? Here is a simple look at how you get/set bit values from a "32 bit" int:
mask% = %11111111111111111111111111111111 ; this is very useful for And operations value% = %11010101110101110001010101110100 ; here's the starting value Print "the full 32bits:" + Bin$(value) Print "" pos = 2 Print "looking at position " + pos Print "what we find :" + Bin$(value And (1 Shl pos)) Print "" Print "Setting bit " + pos + " to false" value = value And (mask - (1 Shl pos)) Print "the full 32bits:" + Bin$(value) Print "" pos = 9 value = value Or (1 Shl pos) Print "Setting bit " + pos + " to true" Print "the full 32bits:" + Bin$(value) WaitKey()
An example of bit flipping:
Let's say that you're making a game that involves flipping tiles. But, at some point, your game needs to be informed about which tiles have been flipped. Let's presume that you have a grid of 25 tiles by 25 tiles. All there is to store/retrieve is this singular value of 'true' or 'false'.
The usual common method would be to define a table with the DIM instruction, such as 'Dim tiles%(25, 25)'. Let's say that we need to write this into a file. Basically, what we will do would be to create a routine containing two embedded "For...Next" loops. And within that loop, we put a write instruction of the int values (containing simply 1's and 0's) down to a file.
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
Now, here is a more efficient way. For our tile example, we will initiallize our table in the following manner:
Dim tile_row%(25) For i = 1 to 25 tile_row(i) = %0000000000000000000000000 Next
The reason we put % is to see the bits that we're initializing. That way, we know what bits are equal to what values. If we put some bits to 1 (True) or 0 (False), it becomes quite easier and more accessible this way.
Next, how to look at the bits: As seen on the first code snippet, we can interrogate the int values and see what they contain. It's always good to make some small functions that helps us perform modifications tasks to our table or to interrogate their contents. Here is an example code:
Function Get_tile_flipped(col%, row%) ; returns 0 if tile is not flipped ; > 0 (or true) if tile is flipped Return tile_row(row) And (1 Shl col) End Function
Finally, we make a function to permit tile flipping!
Function Flip_tile(col%, row%, flipped% = True) ; flipped = 0 to unflip the tile ; flipped = 1 to flip the tile (default) Local mask% If flipped then mask = (1 Shl col) tile_row(row) = tile_row(row) Or mask Else mask = %1111111111111111111111111 - (1 Shl col) tile_row(row) = tile_row(row) And mask EndIf End Function
Thanks for reading! Hope this was helpful and easy to learn.