SA-MP Forums Archive
|= and & - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: |= and & (/showthread.php?tid=555790)



|= and & - iBots - 08.01.2015

Well i have seen a gamemode that uses like this:
example:
to make someone helper you do /sethelper [playerid][level],lets say i chose level 2,in script it does like this:
pawn Код:
PlayerInfo[playerid][pHelper] |= HELPER_ONE
PlayerInfo[playerid][pHelper] |= HELPER_TWO
and to detect:
pawn Код:
if(PlayerInfo[playerid][pHelper] & HELPER_ONE) // if he is helper level 1
{
}
if(PlayerInfo[playerid][pHelper] & HELPER_TWO) // if he is helper level 2
{
}
when i tried to use |= it gave me errors,why,please explain how to use it


Re: |= and & - DavidSparks - 08.01.2015

I have never heard of |=, What are you trying to fix anyways?


Re: |= and & - Schneider - 08.01.2015

You have to use the exclamation mark and equal sign (!=) to check if 2 values are not equal.

"=" set a variable to a certain value
"==" = is equal
"!=" = is not equal
"||" = Or
"&&" = And


Re: |= and & - DavidSparks - 08.01.2015

Quote:
Originally Posted by Schneider
Посмотреть сообщение
You have to use the exclamation mark and equal sign (!=) to check if 2 values are not equal.
That explains the thing, and about the & not working try using two of them instead &&.


Re: |= and & - WLSF - 08.01.2015

Those are bits operators, show me the code that you're trying to use it.

An example for this:
pawn Код:
//normal 32-bit var, that contains an integer value corresponding to 2
//which means 0010 in binary.
new var = 2; // 0010
If you create another var with the value corresponding to 4, and use the OR bit operator like this:
pawn Код:
new var2 = 4; //4 = 0100 binary
var2 |= var; // 0100 | 0010 = 0110
It will join the bits which are enabled.

So, 0100 has the third bit enabled, and 0010 ain't, then it will turn enabled.
The same thing for the second bit.


Re: |= and & - Mauzen - 08.01.2015

In addition to what Willian_Luigi already said:
a |= b means a = a | b
just like a += b means a = a + b

| is a binary OR operator, meaning the OR operator is performed for each bit.
& is binary AND. So AND will be performed for each bit. The resulting bit at a certain position will only be true if both bits at the same position for both parameters are true.

There are plenty of tutorials and explanations for this, so feel free to use the search if you want to learn more about binary operators.


Re: |= and & - iBots - 08.01.2015

Can you give me example of it?i wanna use it for mapper levels,levels will be 1234ab,so give me example of making mappers command using |= for adding levels and & for detecting


Re: |= and & - iBots - 08.01.2015

and why did it give me error when i used |=


Re: |= and & - WLSF - 08.01.2015

Example, using two cases.

I've a variable, to identify somethings about my personality on the game.
Код:
new person[MAX_PLAYERS];
And I've mapped it on 2 bits, the 1st bit indicates that I'm an admin player, the 2nd bit indicates that I'm a vip player.

So, I can create macros for indentify the difference between vp and ap:
pawn Код:
#define ADMIN_P 0b01
#define VIP_P 0b10
And I can set it, just using:
pawn Код:
person[playerid] |= ADMIN_P;
Now I set myself as an administrator.

to verify, you can use AND bit operator:
pawn Код:
if (person[playerid] & ADMIN_P) //indicates that I'm an Administrator
if (person[playerid] & VIP_P) //indicates that I'm a vip player, but it will return false (01 & 10 = 00)



Re: |= and & - Vince - 08.01.2015

Practical use for this is to store multiple (up to 32) boolean variables (true/false, on/off, yes/no) into one single integer variable. In your case this would probably translate to permissions, so for example:

pawn Код:
enum (<<= 1) // don't forget to add this
{
    CAN_ADD_OBJECT = 1, // set this to 1 otherwise everything will be 0 and the whole thing breaks
    CAN_EDIT_OBJECT,
    CAN_REMOVE_OBJECT,
    // etc
}

new editorPermissions[MAX_PLAYERS];

// somewhere
editorPermissions[playerid] |= CAN_ADD_OBJECT; // player can now add objects

editorPermissions[playerid] &= ~CAN_ADD_OBJECT; // player can no longer add objects
You can write macros or functions for this so you don't have to remember all the | and & things.