Quote:
Originally Posted by LarzI
Great tutorial!
Learned alot from it!
I still (as I'm only 16 years old) don't know in what situation I can take use of binary, but oh well.. I'll find out sooner or later.
Again, amazingly done!
|
Check this out:
http://www.cplusplus.com/forum/general/1590/
Flags. Study and give the code below a try:
*Note: GetBit is only there for ease of understanding, it'd be simpler just to use the comments in the functions rather than what is currently in use.
Code:
#include <a_samp>
#define FILTERSCRIPT
GetBit(flag)
{
return (1 << flag);
}
FlagOn(&mask, flag)
{
mask |= flag;
// mask |= (1 << flag);
}
FlagOff(&mask, flag)
{
mask &= (~flag);
// mask &= ~(1 << flag);
}
HasFlag(mask, flag)
{
return (mask & flag);
// return (mask & (1 << flag));
}
public OnFilterScriptInit()
{
new
flags = 0x0,
i;
for(i = 0; i < 31; ++i)
{
FlagOn(flags, GetBit(i));
printf("Bit: %b, Flag Active: %i", GetBit(i), HasFlag(flags, GetBit(i)));
}
for(i = 0; i < 31; ++i)
{
FlagOff(flags, GetBit(i));
printf("Bit: %b, Flag Active: %i", GetBit(i), HasFlag(flags, GetBit(i)));
}
return 0;
}
And if you're further interested, study this:
Code:
#include <a_samp>
#define FILTERSCRIPT
Pack(&mask, byte, value)
{
mask |= ((value & 0xff) << (byte * 8));
}
UnPack(&mask, byte)
{
mask &= ~(PackGet(mask, byte) << (byte * 8));
}
PackGet(mask, byte)
{
return ((mask >> (byte * 8)) & 0xff);
}
public OnFilterScriptInit()
{
new
mask = 0x0,
i;
for(i = 0; i < 4; ++i)
{
Pack(mask, i, random(255));
printf("Byte: %i, Value: %i", i, PackGet(mask, i));
}
for(i = 0; i < 4; ++i)
{
UnPack(mask, i);
printf("Byte: %i, Value: %i", i, PackGet(mask, i));
}
return 0;
}
Since Pawn's cell data type is 32-bits wide, you're able to store 4 "unsigned" 8-bit integers in your mask.
Enjoy!