29.09.2010, 13:42
Quote:
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; } 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; } Enjoy! |
![Cheesy](images/smilies/biggrin.png)
I mentioned the first method in previous posts, i just never gave physical code (thank you for providing it heh). You could take it a step further and convert your functions into defines, would be faster that way.
I'm not so sure about the second method though. Im all for saving memory, but i dont think the speed difference would be worth it for 4 unsigned 8 bit integers. That's just my view though, there's nothing wrong with it at all. To be honest, i never even thought about this once. Its pretty neat at the very least.