Raised This Month: $51 Target: $400
 12% 

[INFO] Bitsums and Operators


Post New Thread Reply   
 
Thread Tools Display Modes
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-21-2009 , 04:47   Re: [INFO] Bitsums and Operators
Reply With Quote #41

For those who don't know what i'm talking about (This is all in the pawn manual):

A cell is a contiguous region of memory composed of 4 bytes (32 bits) (normally).

A character can be represented with only 1 byte (8 bits) (And it is in other languages, being also a type definition)

In Pawn there is only the cell type. So, normally, to hold a character, a cell is used. But, Pawn also permits the storage of chars in 1 byte sized memory regions. That permits us to use a cell as a 4 characters holder. (What is called "to pack a string")

To create a variable to be used as a "packed string holder" the syntax is:

PHP Code:
new packedStringHolder[5 char
That will create a new variable called packedStringHolder that will have cells enough to hold 5 packed chars. That means that it will have 2 cells. The first cell will hold 4 packed chars, the second one will hold 1 packed char.

To create a packed string the syntax is:

new packedStringHolder[] = !"string"

To access a char in a packed string the syntax is:

packetStringHolder{index}

So, packetStringHolder{0} == 's' .

You will not find many uses for this data storage way since there aren't natives for them (i think) but, for the case that we are talking about here, it fits:

PHP Code:
new bool:isConnected[33 char]

public 
client_authorized(id)
{
    
isConnected{id} = true;
}
public 
client_disconnect(id)
{
    
isConnected{id} = false;

That would probably be as fast as using a 33 cells array but, will use 9 cells instead.

The formula to calculate the cells needed is:

(packed chars / 4) + (packed chars % 4) ? 0 : 1

So, for 33 packed chars:

(33 / 4) + (33 % 4) ? 0 : 1

8 + 1 = 9
__________________

Last edited by joaquimandrade; 04-21-2009 at 05:14.
joaquimandrade is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-21-2009 , 05:00   Re: [INFO] Bitsums and Operators
Reply With Quote #42

Thanks for your explanations. Now I understand better what was a packed string.
Arkshine is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-21-2009 , 05:04   Re: [INFO] Bitsums and Operators
Reply With Quote #43

Quote:
Originally Posted by arkshine View Post
Thanks for your explanations. Now I understand better what was a packed string.
__________________
joaquimandrade is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-21-2009 , 05:09   Re: [INFO] Bitsums and Operators
Reply With Quote #44

You should make a separate thread for that.
Arkshine is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-21-2009 , 05:10   Re: [INFO] Bitsums and Operators
Reply With Quote #45

Quote:
Originally Posted by arkshine View Post
You should make a separate thread for that.
Ok. Tomorrow i will.
__________________
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-21-2009 , 08:55   Re: [INFO] Bitsums and Operators
Reply With Quote #46

The Pawn language guide discusses packed\unpacked strings FYI.
__________________
Bugsy is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-21-2009 , 09:13   Re: [INFO] Bitsums and Operators
Reply With Quote #47

I know, that's why I said 'was'. How it's explained I have some difficulties to understand it. joaquimandrade explains well and with some examples it should be a good tutorial about it.
Arkshine is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-21-2009 , 10:07   Re: [INFO] Bitsums and Operators
Reply With Quote #48

I absolutely agree, I was just putting it up there for anyone interested in seeing what the guide has to say.
__________________
Bugsy is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-15-2009 , 14:59   Re: [INFO] Bitsums and Operators
Reply With Quote #49

If someone is interested on knowing how to use bit storage for values bigger than 31:

PHP Code:
save(x)
{
    
holder[32] |= << (32);
}

exists(x)
{
    return 
holder[32] & << (32);
}

remove(x)
{
    
holder[32] &= ~(<< (32))

It's a memory tradeoff.
__________________

Last edited by joaquimandrade; 05-16-2009 at 00:45. Reason: fixed save. &= -> |=
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-15-2009 , 19:14   Re: [INFO] Bitsums and Operators
Reply With Quote #50

Nice find
__________________
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 14:50.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode