|= and &
#1

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
Reply
#2

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

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
Reply
#4

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 &&.
Reply
#5

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.
Reply
#6

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.
Reply
#7

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
Reply
#8

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

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)
Reply
#10

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)