24.01.2014, 16:02
Using bit arrays
Introduction
Since I like to write tutorials about saving memory, I decided to come up with this tutorial, which teaches you the real meaning of boolean arrays, aswell as bit arrays, and how to use them!
What are boolean arrays?
Boolean arrays look like this:
However, "bool" is just a predefined tag, "true" and "false" are the values for 1 and 0 respectively, so in reality these arrays can actually hold up to 2,147,483,647 values, just like normal variables. If MAX_PLAYERS is 500, the array above is 2,000 bytes.
There are also "char arrays":
The array above is a mere 500 bytes. Still, not bad, but there is still some sparse data left in the array.
Now let's take a bit at bit arrays:
Believe it or not, the array above is only 29 bytes! Bit arrays use a complex method of packing data into the array, by tightly packing the data into a 1-bit array using specialized bit manipulation, rather than each 4-byte cell (as opposed to regular arrays) or each byte (as opposed to char arrays, I still need clarification on this though).
Let's get started!
You can get y_bit from the latest YSI version here:
https://sampforum.blast.hk/showthread.php?tid=321092
You can either use "Bit:" or "BitArray:" to initialize bit arrays:
These arrays only hold 1-bit values, which are either "true" or "false". Here are a list of relevant functions:
This will set the value in the specified slot to "true".
This will set the value in the specified slot to "false".
This will set the value in the specified slot to "value", which can be either true or false.
Returns either "true" or "false", depending on the value set.
Multiple dimensions
You can also use 2D arrays with these bit arrays:
Setting the value is slightly different, since you need to specify both indices:
The same goes for the other functions.
Multiple bits
y_bit supports 1 bit arrays, and that's all that you'll probably need. If you feel that you need more bits, you might want to check out "rBits":
https://sampforum.blast.hk/showthread.php?tid=275142
It supports 1 bit, as well as 2, 4, 8 and 16 bits (a normal variable is 32 bits).
Conclusion
You've reached the end of this tutorial, that's cool.
The reason why I write these type of tutorials is to point out better methods of saving memory in PAWN, because nobody wants a cluttered, 500 MB AMX file!
Introduction
Since I like to write tutorials about saving memory, I decided to come up with this tutorial, which teaches you the real meaning of boolean arrays, aswell as bit arrays, and how to use them!
What are boolean arrays?
Boolean arrays look like this:
pawn Код:
new bool:gPlayerArray[MAX_PLAYERS];
There are also "char arrays":
pawn Код:
new bool:gPlayerArray[MAX_PLAYERS char];
Now let's take a bit at bit arrays:
pawn Код:
new BitArray:gPlayerArray<MAX_PLAYERS>;
Let's get started!
You can get y_bit from the latest YSI version here:
https://sampforum.blast.hk/showthread.php?tid=321092
You can either use "Bit:" or "BitArray:" to initialize bit arrays:
pawn Код:
new BitArray:gUsingPhone<MAX_PLAYERS>;
pawn Код:
Bit_Let(BitArray:array<>, slot);
pawn Код:
Bit_Vet(BitArray:array<>, slot);
pawn Код:
Bit_Set(BitArray:array<>, slot, bool:value);
pawn Код:
Bit_Get(BitArray:array<>, slot);
Multiple dimensions
You can also use 2D arrays with these bit arrays:
pawn Код:
new BitArray:gVehicleStreamed[MAX_VEHICLES]<MAX_PLAYERS>;
pawn Код:
Bit_Let(gVehicleStreamed[vehicleid], playerid, true);
Multiple bits
y_bit supports 1 bit arrays, and that's all that you'll probably need. If you feel that you need more bits, you might want to check out "rBits":
https://sampforum.blast.hk/showthread.php?tid=275142
It supports 1 bit, as well as 2, 4, 8 and 16 bits (a normal variable is 32 bits).
Conclusion
You've reached the end of this tutorial, that's cool.
The reason why I write these type of tutorials is to point out better methods of saving memory in PAWN, because nobody wants a cluttered, 500 MB AMX file!