20.10.2011, 18:33
c_bit (char bits)
A simple bit system for cases, when you only have to use less than 25 slots.
Memory usage:
1-7 slots: 1 byte
8-15 slots: 2 bytes
16-23 slots: 3 bytes
24-32 slots: 4 bytes (you can use any other bit systems if your task fits into this category)
Example usage
In this example, we do an ALS hook with cbits
Credits
I have used Y-Less' y_bit to make the basic structure of tags.
Code
p.s. I originally wanted to post it in the Includes section, but this is far from an include in size.
A simple bit system for cases, when you only have to use less than 25 slots.
Memory usage:
1-7 slots: 1 byte
8-15 slots: 2 bytes
16-23 slots: 3 bytes
24-32 slots: 4 bytes (you can use any other bit systems if your task fits into this category)
Example usage
In this example, we do an ALS hook with cbits
pawn Код:
new CBitArray:hooks[2]; /* you have 2 callbacks to hook */
#define HAS_OPC (0)
#define HAS_OPDC (1)
public OnScriptInit()
{
CBit_Set(hooks, HAS_OPC, (funcidx("S_OnPlayerConnect") != -1));
CBit_Set(hooks, HAS_OPDC, (funcidx("S_OnPlayerDisconnect") != -1));
}
public OnPlayerConnect(playerid)
{
// your code
if (CBit_Get(hooks, HAS_OPC)) return CallLocalFunction("S_OnPlayerConnect", "i", playerid);
}
public OnPlayerDisconnect(playerid, reason)
{
// your code
if (CBit_Get(hooks, HAS_OPDC)) return CallLocalFunction("S_OnPlayerDisconnect", "ii", playerid, reason);
}
I have used Y-Less' y_bit to make the basic structure of tags.
Code
pawn Код:
#define CBitArray:%1[%2] CBit:%1[%2]
#define CBit_Bits(%1) (((%1) + 7) / 8) char
#define CBit_Get(%1,%2) !!(%1{(%2)>>>3} & CBit:(1 << ((%2) & 7)))
#define CBit_Let(%1,%2) (%1{(%2)>>>3} |= CBit:(1 << ((%2) & 7)))
#define CBit_Vet(%1,%2) (%1{(%2)>>>3} &= CBit:~(1 << ((%2) & 7)))
stock CBit_Set(CBitArray:array[], slot, bool:set)
{
if (set) CBit_Let(array, slot);
else CBit_Vet(array, slot);
}
#undef CBitArray
#define CBitArray:%1[%2] CBit:%1[CBit_Bits(%2)]