Page 1 of 1

[INFO] Bitsums and Operators

Posted: Sun Oct 11, 2026 6:00 pm
by Archive-Poster
About:
This tutorial is meant for beginner scripters from amxx. This will teach you on how to use bitsum operators and show some advantages over boolean storage arrays!

What is a bitsum?
A bitsum is based on the way memory works. If we think, the memory chip containes cells (bits) that can hold 2 infos: 1 and 0. To store a number in one byte (byte = 8 bits) here is how to do it:
"8" (normal number) = "00001000" (memory number [1 byte])

How does a bitsum work?
Well the idea is that bitsums allow to control the position that we want to set it to 1. And we can check wether that position has 1 or 0.

What operators do I have, and what do they mean?
I will give an example for each operator:

Code: Select all

Operator: |
first_byte = "10010001"
second_byte = "01010001"

first_byte | second_byte = "11010001"

So basically this operator works like the logical one || but it does the operation on each bit!

Code: Select all

|     1      0
1     1      1
0     1      0

Code: Select all

Operator: &
first_byte = "10010001"
second_byte = "01010001"

first_byte & second_byte = "0001001" 
So basically this operator works like the logical one && but it does the operation on each bit!

Code: Select all

&     1      0
1     1      0
0     0      0

Code: Select all

Operator: ^ (Exclusive OR)
first_byte = "10010001"
second_byte = "01010001"

first_byte ^ second_byte = "1100000" 
So basically this operator works like the logical one ^^ but it does the operation on each bit!

Code: Select all

^    1      0
1    0      1
0    1      0

Code: Select all

Operator: ~ (Complement applies on only one number!)
first_byte = "10010001"

~first_byte = "01101110" 
So basically this operator negates every bit! So 0 becomes 1 and 1 becomes 0.

Code: Select all

~1 = 0
~0 = 1
How do normal numbers work?
They are stored in the normal cells by base. So they are converted from base 10 from base 2. In this case number 9 for example becomse: "00001001". To comvert that number from the cells back to 9 we need to multiply each position with 2 and a power of it! 2^2 = power(2,2) = 4
00001001: 1*(2^0) + 0*(2^1) + 0*(2^2) + 1*(2^3) + 0*(2^4) + 0*(2^5) + 0*(2^6) + 0*(2^7) = 1 + 8 = 9

How can I easily create a bitsum?
Well you need 2 operators: << and >>, or if you don't want to use those operators you can easily create one by using elements that represent powers of 2.

Example:

Code: Select all

Bitsum "00001111" = 8 | 4 | 2 | 1 
The 2 operators have a special meaning!
"<< x" means that a position of a number is moved by x points to left! Example:

Code: Select all

"00001100" << 1 = "00011000" 
When we convert this to normal number we can see that the normal number was multiplied by 2.
So if we have (1<<3) it would be like 2^3 * 1

The >> operator does the same thing like << but moves x points to right! When using integer variables the numbers disappear.

Code: Select all

"00000111" >> 3 = "00000000" 
So using those 2 operators to create a bitsum we have this:

Code: Select all

Bitsum "00001111" = (1<<3 | 1<<2 | 1<<1 | 1<<0) 
How can I use this in my scripts?
Well it can be used for insance instead of boolean arrays.

So instead of having this:

Code: Select all

new bool:is_alive[33] 
We have:

Code: Select all

new bitsum_is_alive


So less memory usage!

Instead of

Code: Select all

if (is_alive[id])
{
} 
We have:

Code: Select all

if (bistum_is_alive & (1<<id))
{
} 
Instead of this

Code: Select all

is_alive[id] = true 

Code: Select all

is_alive[id] = false 
We will have this:

Code: Select all

bitsum_is_alive |= (1<<id) 

Code: Select all

bitsum_is_alive &=  ~(1<<id)

The good thing is that you can extend the limit of the 32 slots that a simple number allowes with an array so that we can have more slots.

Ex:

Code: Select all

new holder[4]
// holder has 4 * 32 places where we can store the true/false value
// Here are the functions that allow you to manipulate them!

// Here we make the desired slot true
save(x)
{
    holder[x / 32] |= 1 << (x % 32);
}

// Here we check the desired slot 
exists(x)
{
    return holder[x / 32] & 1 << (x % 32);
}
 
// Here we remove the desired slot 
remove(x)
{
    holder[x / 32] &= ~(1 << (x % 32))
} 
As you can see it is harder to use bitsums but they have better results when it comes to low memory usage!
Hope that my english was fine!