[TUT AMXX SCRIPTING ] Bitwise Operators & Their Usage
Apr 21, 2019 0:34:03 GMT
Gaspatcho and DoNii like this
Post by ZinoZack47 on Apr 21, 2019 0:34:03 GMT
It's been a long time... I was busy with university studies and I will be back probably in summer-break.
With not further due... I want to explain what are bitwise operators and how can they be useful concerning AMXX Scripting.
First of all you will need 2 things: The Will and Binary System Knowledge.
You can learn about the binary system in about 5 mins if you visit Youtube.
In most codes you might encounter signs such as: | & ^ ~ << >>
So what are these?
Simply they are called bitwise operators:
What's the point of this and how is it useful concerning AMXX Scripting?
Supposing for example you have a plugin that can give certain skills to a certain player, so for this you would be thinking about using 5 arrays:
If you are fancy you could use enum like this
Using bitwise operators in such codes will surely not drain your RAM too much which will not cause any laggs to your server.
With not further due... I want to explain what are bitwise operators and how can they be useful concerning AMXX Scripting.
First of all you will need 2 things: The Will and Binary System Knowledge.
You can learn about the binary system in about 5 mins if you visit Youtube.
In most codes you might encounter signs such as: | & ^ ~ << >>
So what are these?
Simply they are called bitwise operators:
- & stands for and
- | stands for or
- ^ stands for xor
- ~ stands for Ones Complement
- << stands for Left Shift
- >> stands for Right Shift
1 & 1 is 1
1 & 0 is 0
0 & 1 is 0
0 & 0 is 0
// to get 1 both of them must be 1
1 | 1 is 1
1 | 0 is 1
0 | 1 is 1
0 | 0 is 0
// to get 1 one of them at least should be 1
//The ^ operator is like the | operator but:
1 ^ 1 is 0
1 ^ 0 is 1
0 ^ 1 is 1
0 ^ 0 is 0
// to get 1 both of them must be different from each other
// The ~ operator changes the 0 to 1 and 1 to 0
// You have x = 5 in binary 5 is 101
// So ~x is 010 which is 2 in 3 bits
// the << operator shift works like this:
// For example you have 1 << 1
// 1 in binary 32 bits (since most of Linux OS are 32 bits) is 00000000000000000000000000000001
// we shift it to the left by 1 bit it will become like this 00000000000000000000000000000010
// 1 << 2 will shift it with 2 bits like this
// Before 00000000000000000000000000000001 After 00000000000000000000000000000100
// 5 << 2 how will it be?
// 5 in binary (32 bits) is 00000000000000000000000000000101
// we shift by 2 bits to the left we get 00000000000000000000000000010100 which is 20 in decimal
//NOTE: When we shift to the left it is like multiplying by 2 ** x (ex: 1 << 3 = 1 * 2**3 = 8)
//the >> operator is like the << but it shifts to the right instead:
//For example you have 2 >> 1
// 2 in binary 32 bits is 00000000000000000000000000000010
// we shift it to the right by 1 bit it will become like this 00000000000000000000000000000001 which is 1 in decimal
// 1 << 2 will shift it with 2 bits like this
// Before 00000000000000000000000000000001 After 00000000000000000000000000000100
// 4 >> 2 how will it be?
// 5 in binary (32 bits) is 00000000000000000000000000000100
// we shift by 2 bits to the right we get 0000000000000000000000000000001 which is 1 in decimal
What's the point of this and how is it useful concerning AMXX Scripting?
Supposing for example you have a plugin that can give certain skills to a certain player, so for this you would be thinking about using 5 arrays:
new bool:g_bSkill1[33], bool:g_bSkill2[33], bool:g_bSkill3[33], bool:g_bSkill4[33], bool:g_bSkill5[33]
but using bitwise operators we would only need one array with some Macros to make things simple#define SKILL1 (1<<1) // 00000000000000000000000000000010
#define SKILL2 (1<<2) // 00000000000000000000000000000100
#define SKILL3 (1<<3) // 00000000000000000000000000001000
#define SKILL4 (1<<4) // 00000000000000000000000000010000
#define SKILL5 (1<<5) // 00000000000000000000000000100000
new g_bitSkill[33]
If you are fancy you could use enum like this
//Shifting to the left by 1 bit is like multiplying by 2**1 which is 2 so we would have this instead
enum (*=2)
{
SKILL1 = (1<<1),
SKILL2,
SKILL3,
SKILL4,
SKILL5
}
new g_bitSkill[33]
Consider this example:#include <amxmodx>
#define PLUGIN "[TUT] Bitwise Operators"
#define VERSION "1.0"
#define AUTHOR "ZinoZack47"
#define SKILL1 (1<<1) // 00000000000000000000000000000010
#define SKILL2 (1<<2) // 00000000000000000000000000000100
#define SKILL3 (1<<3) // 00000000000000000000000000001000
#define SKILL4 (1<<4) // 00000000000000000000000000010000
#define SKILL5 (1<<5) // 00000000000000000000000000100000
new g_bitSkill[33]
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_clcmd("lol", "ClCmd_LOL")
register_clcmd("nword", "ClCmd_Toxic")
}
public ClCmd_LOL(id)
{
g_bitSkill[id] |= SKILL1 // g_bitSkill[id] = g_bitSkill[id] | SKILL1
/* What Happens?
We have g_bitSkill[id] is empty which means g_bitSkill[id] is equal to 0 which is 00000000000000000000000000000000 in 32 bits whereas SKILL1 is 00000000000000000000000000000010
so lets do the operation |
00000000000000000000000000000000 0 | 1 = 1 True or False gives True
||||||||||||||||||||||||||||||||
00000000000000000000000000000010
as a result g_bitSkill[id] gets a new value which is 00000000000000000000000000000010
Now we want to give our player another skill (i.e SKILL3)
g_bitSkill[id] |= SKILL3 // g_bitSkill[id] = g_bitSkill[id] | SKILL1
/*
00000000000000000000000000000010 This is not an addition operation
||||||||||||||||||||||||||||||||
00000000000000000000000000001000
as a result g_bitSkill[id] gets a new value which is 00000000000000000000000000001010
*/
}
public ClCmd_Toxic(id)
{
//Supposing now we want to strip the player from SKILL3 We simply do this:
g_bitSkill[id] &= ~SKILL3 // g_bitSkill[id] = g_bitSkill[id] & ~SKILL3
/*
Let's first get the value of ~SKILL3 (SKILL3 Complement)
SKILL3 is (1<<3) which is 0000000000001000
now by turning the 0 to 1 and the 1 to 0 we get the value of ~SKILL3
1111111111110111
now we execute the operaion &
00000000000000000000000000001010 0 & 1 is 0 only 1 & 1 is 1
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
11111111111111111111111111110111
as a result g_bitSkill[id] gets a new value which is 00000000000000000000000000000010
see? now we only have SKILL1 like earlier
*/
}
Using bitwise operators in such codes will surely not drain your RAM too much which will not cause any laggs to your server.