[Tutorial] An in-depth look at binary and binary operators.
#25

Quote:
Originally Posted by Tannz0rz
View Post
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!
Nice post, thanks for contributing .

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.
Reply


Messages In This Thread
An in-depth look at binary and binary operators. - by Kyosaur - 18.09.2010, 10:50
Re: An in-depth look at binary and binary operators. - by Kyosaur - 18.09.2010, 10:52
Re: An in-depth look at binary and binary operators. - by LarzI - 18.09.2010, 11:07
Re: An in-depth look at binary and binary operators. - by Hiddos - 18.09.2010, 11:18
Re: An in-depth look at binary and binary operators. - by Leeroy. - 18.09.2010, 11:19
Re: An in-depth look at binary and binary operators. - by Kyosaur - 18.09.2010, 11:22
Re: An in-depth look at binary and binary operators. - by LarzI - 18.09.2010, 11:23
Re: An in-depth look at binary and binary operators. - by Mimic - 18.09.2010, 11:26
Re: An in-depth look at binary and binary operators. - by Hiddos - 18.09.2010, 11:27
Re: An in-depth look at binary and binary operators. - by Kyosaur - 18.09.2010, 11:34
Re: An in-depth look at binary and binary operators. - by DiddyBop - 18.09.2010, 12:53
Re: An in-depth look at binary and binary operators. - by Simon - 19.09.2010, 08:55
Re: An in-depth look at binary and binary operators. - by Kyosaur - 19.09.2010, 22:21
Re: An in-depth look at binary and binary operators. - by Backwardsman97 - 20.09.2010, 02:46
Re: An in-depth look at binary and binary operators. - by Hiddos - 23.09.2010, 16:09
Re: An in-depth look at binary and binary operators. - by Calgon - 23.09.2010, 19:35
Re: An in-depth look at binary and binary operators. - by Kyosaur - 23.09.2010, 22:34
Re: An in-depth look at binary and binary operators. - by Chaprnks - 24.09.2010, 00:45
Re: An in-depth look at binary and binary operators. - by Slice - 25.09.2010, 18:57
Re: An in-depth look at binary and binary operators. - by MrDeath537 - 26.09.2010, 00:04
Re: An in-depth look at binary and binary operators. - by LarzI - 26.09.2010, 09:56
Re: An in-depth look at binary and binary operators. - by Kyosaur - 26.09.2010, 10:16
Re: An in-depth look at binary and binary operators. - by Tannz0rz - 26.09.2010, 10:35
Re: An in-depth look at binary and binary operators. - by LarzI - 26.09.2010, 10:36
Re: An in-depth look at binary and binary operators. - by Kyosaur - 29.09.2010, 13:42
Re: An in-depth look at binary and binary operators. - by Slice - 29.09.2010, 14:24
Re: An in-depth look at binary and binary operators. - by Kyosaur - 29.09.2010, 14:55
Re: An in-depth look at binary and binary operators. - by Slice - 29.09.2010, 15:02
Re: An in-depth look at binary and binary operators. - by Kyosaur - 29.09.2010, 15:14
Re: An in-depth look at binary and binary operators. - by Slice - 29.09.2010, 15:41
Re: An in-depth look at binary and binary operators. - by LarzI - 29.09.2010, 16:35
Re: An in-depth look at binary and binary operators. - by Tannz0rz - 29.09.2010, 17:47
Re: An in-depth look at binary and binary operators. - by LarzI - 29.09.2010, 18:11
Re: An in-depth look at binary and binary operators. - by [HLF]Southclaw - 09.03.2011, 19:11
Re: An in-depth look at binary and binary operators. - by Kyosaur - 09.03.2011, 23:39
Re: An in-depth look at binary and binary operators. - by black_dota - 10.03.2011, 06:18
Re: An in-depth look at binary and binary operators. - by alpha500delta - 10.03.2011, 13:21
Re: An in-depth look at binary and binary operators. - by Kyosaur - 10.03.2011, 13:32
Re: An in-depth look at binary and binary operators. - by [HLF]Southclaw - 10.03.2011, 15:33
Re: An in-depth look at binary and binary operators. - by Mean - 10.03.2011, 16:02
Re: An in-depth look at binary and binary operators. - by SkizzoTrick - 10.03.2011, 17:20
Re: An in-depth look at binary and binary operators. - by Wesley221 - 03.10.2011, 17:08
Re: An in-depth look at binary and binary operators. - by Cank - 07.10.2011, 15:04
Re: An in-depth look at binary and binary operators. - by [MM]IKKE - 11.10.2011, 23:36
Re: An in-depth look at binary and binary operators. - by Kyosaur - 11.10.2011, 23:56
Re: An in-depth look at binary and binary operators. - by Davz*|*Criss - 12.10.2011, 10:58
Re: An in-depth look at binary and binary operators. - by [MM]IKKE - 12.10.2011, 11:33
Re: An in-depth look at binary and binary operators. - by Kyosaur - 13.10.2011, 03:58
Re: An in-depth look at binary and binary operators. - by [HLF]Southclaw - 13.10.2011, 10:44
Re: An in-depth look at binary and binary operators. - by System64 - 31.03.2012, 12:41
Re: An in-depth look at binary and binary operators. - by LarzI - 05.05.2013, 14:20
Re: An in-depth look at binary and binary operators. - by Pandex - 01.08.2013, 20:41

Forum Jump:


Users browsing this thread: 5 Guest(s)