SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 08/08/2011, 03:47 PM   #1
RyDeR`
High-roller
 
RyDeR`'s Avatar
 
Join Date: Feb 2009
Location: Belgium
Posts: 2,929
Reputation: 700
Default rBits [supports 16, 8, 4, 2 and 1-bit arrays]

Introduction

Last days I did some research regarding bit manipulation and extractions and ended up with this small, very useful include. This include will save you lots of memory if you use it efficient.

Integer Limits

1-Bit
2-Bit
4-Bit
8-Bit
16-Bit
Limit1 ─ 0x13 ─ 0x315 ─ 0xF255 ─ 0xFF65535 ─ 0xFFFF

A higher/lower value will just repeat until the limit and start over again till done.

Usage

Declaration

The syntax is quite simple. You just use BitX and replace X with the prefered bit type as tag, followed by the name and then the size between < and >.

So for example let's declare a 4-bit array b4_Nibble with a size of 32:
pawn Code:
new
    Bit4: b4_Nibble <32>
;
Set and get a bit

You can use the BitX_Set/Get function for this. For example:
pawn Code:
Bit4_Set(b4_Nibble, 0, 4);
This will set b4_Nibble at index 0 to a value of 4.

Retrieving the value is as simple as:
pawn Code:
Bit4_Get(b4_Nibble, 0);
This will return the value in b4_Nibble at index 0 which will be 4 in this case.

Examples

Here are some simple examples of what I mean by "saving lots of memory". I often see people doing things like this:
pawn Code:
#include <a_samp>
   
new
    bool: g_PlayerSpawned[MAX_PLAYERS]
;

public OnPlayerSpawn(playerid) {
    g_PlayerSpawned[playerid] = true;
}

public OnPlayerDeath(playerid) {
    g_PlayerSpawned[playerid] = false;
}

public OnPlayerDisconnect(playerid, reason) {
    g_PlayerSpawned[playerid] = false;
}

stock IsPlayerSpawned(playerid) {
    return g_PlayerSpawned[playerid];
}
The .amx size after compile is 726 bytes. Nothing much, but as you can see we're using 32-bit variables for only 0 and 1, so that's a waste of a lot memory. The most relevant bit type in this case would be 1-bit:
pawn Code:
#include <a_samp>
#include <rBits>

new
    Bit1: g_PlayerSpawned <MAX_PLAYERS>
;

public OnPlayerSpawn(playerid) {
    Bit1_Set(g_PlayerSpawned, playerid, true);
}

public OnPlayerDeath(playerid) {
    Bit1_Set(g_PlayerSpawned, playerid, false);
}

public OnPlayerDisconnect(playerid, reason) {
    Bit1_Set(g_PlayerSpawned, playerid, false);
}

stock IsPlayerSpawned(playerid) {
    return Bit1_Get(g_PlayerSpawned, playerid);
}
The .amx size of this after compile is only 473 bytes. Probably not a big deal right now, but imagine with larger arrays or just a couple of more arrays like this.

This is just analog all the rest of the bit-types, just make sure you pick the right bit-type for your value.

Download

rBits.inc

FAQ
  • How can I use this with enum?
    • You can just sort per bit type. For example for 1-bit arrays:
      pawn Code:
      enum e_Bit1_Data {
          e_bSpawned,
          e_bIsDeath,
          e_bInDM,
          // ...
      };

      new
          g_Bit1_Data[e_Bit1_Data] <MAX_PLAYERS>
      ;

      Bit1_Set(g_Bit1_Data[e_bSpawned], playerid, true);
      Bit1_Get(g_Bit1_Data[e_bSpawned], playerid);
  • How to increase/decrease the value?
    • Since x++ is equal to x = x + 1 we can simply use that technique:
      pawn Code:
      Bit4_Set(b4_Nibble, 0, Bit4_Get(b4_Nibble, 0) + 1);
  • Other questions?
    • Ask in the comments.
__________________

Sup?

Last edited by RyDeR`; 24/02/2012 at 11:09 PM.
RyDeR` is offline   Reply With Quote
Old 08/08/2011, 03:50 PM   #2
Ash.
High-roller
 
Ash.'s Avatar
 
Join Date: Jan 2010
Location: England
Posts: 2,084
Reputation: 163
Default Re: rBits [supports 16, 8, 4, 2 and 1-bit arrays]

Nice Ryder! I'd thought about this in the past, but aren't you leaving yourself open to possible overflows? (I haven't looked at the source code yet, so I'm probably wrong)
__________________
TruckingWorld
Work, Play, Make Friends
Written from scratch, and originally released in 2011, play today!
Visit the forum | Play the game
Ash. is offline   Reply With Quote
Old 08/08/2011, 03:54 PM   #3
VivianKris
Big Clucker
 
Join Date: Jun 2010
Posts: 105
Reputation: 0
Default Re: rBits [supports 16, 8, 4, 2 and 1-bit arrays]

Nice I will try it!
VivianKris is offline   Reply With Quote
Old 08/08/2011, 03:56 PM   #4
Zh3r0
High-roller
 
Join Date: Mar 2008
Posts: 4,513
Reputation: 398
Default Re: rBits [supports 16, 8, 4, 2 and 1-bit arrays]

Now I understand what bits are, thanks a lot for this.

So normal variables are 32bits? Which means they can store a high amount of data but we only need 0 and 1.
Whenever we use a variable to just check something if it's true or false we simply use the 1bit array right?

And when we count a variable we just use the 16bit?
__________________
Zh3r0 is offline   Reply With Quote
Old 08/08/2011, 04:07 PM   #5
RyDeR`
High-roller
 
RyDeR`'s Avatar
 
Join Date: Feb 2009
Location: Belgium
Posts: 2,929
Reputation: 700
Default Re: rBits [supports 16, 8, 4, 2 and 1-bit arrays]

Quote:
Originally Posted by funky1234 View Post
Nice Ryder! I'd thought about this in the past, but aren't you leaving yourself open to possible overflows? (I haven't looked at the source code yet, so I'm probably wrong)
There's a small piece of code involved which checks if you exceed the array bounds so I guess this is quite safe.

Quote:
Originally Posted by Zh3r0 View Post
So normal variables are 32bits?
Yes.


Quote:
Originally Posted by Zh3r0 View Post
Which means they can store a high amount of data but we only need 0 and 1. Whenever we use a variable to just check something if it's true or false we simply use the 1bit array right?
Yes.


Quote:
Originally Posted by Zh3r0 View Post
And when we count a variable we just use the 16bit?
No, you just use the value you need and nothing more. If you use 16-bit arrays, you can save a maximum amount of 65535 like I said in the topic (see limits).
__________________

Sup?
RyDeR` is offline   Reply With Quote
Old 08/08/2011, 04:09 PM   #6
Zh3r0
High-roller
 
Join Date: Mar 2008
Posts: 4,513
Reputation: 398
Default Re: rBits [supports 16, 8, 4, 2 and 1-bit arrays]

Quote:
Originally Posted by RyDeR` View Post
There's a small piece of code involved which checks if you exceed the array bounds so I guess this is quite safe.
No, you just use the value you need and nothing more. If you use 16-bit arrays, you can save a maximum amount of 65535 like I said in the topic (see limits).
Ah yes, true, I meant 4-bit array, then the 4bit array will do it right, and it also doesn't exceed alpha opacity xD
(Which is the right thing I need)

I'm switching right NOW!, Thanks again. Good job.
__________________
Zh3r0 is offline   Reply With Quote
Old 08/08/2011, 04:23 PM   #7
Kaperstone
Banned
 
Join Date: May 2011
Location: Russia
Posts: 3,013
Reputation: 824
Default Re: rBits [supports 16, 8, 4, 2 and 1-bit arrays]

nice

Last edited by Kaperstone; 10/08/2014 at 10:22 AM.
Kaperstone is offline   Reply With Quote
Old 08/08/2011, 04:27 PM   #8
OUL
Big Clucker
 
Join Date: Jun 2011
Posts: 89
Reputation: 2
Default Re: rBits [supports 16, 8, 4, 2 and 1-bit arrays]

This is awesome i'm gonna use this
OUL is offline   Reply With Quote
Old 08/08/2011, 04:31 PM   #9
RyDeR`
High-roller
 
RyDeR`'s Avatar
 
Join Date: Feb 2009
Location: Belgium
Posts: 2,929
Reputation: 700
Default Re: rBits [supports 16, 8, 4, 2 and 1-bit arrays]

Quote:
Originally Posted by xkirill View Post
nice
can u make Minimum random number to?
What do you exactly mean by that? The examples are just examples on how to use it, don't get confused by that please.
__________________

Sup?
RyDeR` is offline   Reply With Quote
Old 08/08/2011, 04:35 PM   #10
OUL
Big Clucker
 
Join Date: Jun 2011
Posts: 89
Reputation: 2
Default Re: rBits [supports 16, 8, 4, 2 and 1-bit arrays]

I mean he think on something like this

RandomEx(min, max)

but that's posible and easy to make use 'Min, Max' fnc
OFF TOPIC
OUL is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] vDealer, probally the most simple vehicledealer system (Supports Y_INI & MYSQL!) Make your own vehicleshops in 1 line! gamer931215 Includes 122 19/05/2014 03:54 PM
TrueServe doesn't supports Hosted Tab? Ouch_Charlie General 3 14/05/2011 03:30 PM
[FilterScript] [FS] ezConverter - Now supports CreateVehicle! Rzzr Filterscripts 13 16/03/2010 10:25 PM
[DAntiCheat] Bye Bye cheaters ... (0.12, SUPPORTS ONLY 0.1b) DracoBlue Filterscripts 261 20/10/2009 11:20 AM


All times are GMT. The time now is 09:57 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.