Operators

From ZDoom Wiki
Revision as of 15:10, 23 August 2007 by HotWax (talk | contribs) ('&&' (binary): Clarification and common usage.)
Jump to navigation Jump to search

Overview

This is all the operators and keywords supported by Acs as well as a basic description of each. You will have to find out for yourself all the different ways you can use. If you have some background in C/C++ then you will be able to pick up on this fast as much of it is the same.

Operators

These are all the operators supported by Acs.

'=' (binary)

Assignment Operator
This operator takes the value on the right and then stores it into the variable on the left. Unless an expression is being passed directly to a function or script then it must have some sort of an assignment operator associated to it. Basic usage:

x = 2;

'+' (binary)

Addition Operator
Adds two numbers together Basic usage:

z = x + y;

If you want to add a number back into a variable then see the '+=' operator.

'-' (unary)

Sign Change Operator (I don't think it has an actual name so I gave it one)
When used in front of a number it changes the sign of that variable. Basic usage:

y = -x;

'-' (binary)

Subtraction Operator
This subtracts two integers from each other. Basic usage:

z = x - y;

If you want to subract a number back into a variable then see the '-=' operator.

'*' (binary)

Multiplication Operator
This multiplies two numbers together. Basic usage:

z = x * y;

If you want to multiply a number back into a variable then see the '*=' operator.

'/' (binary)

Division Operator
This divides two numbers together. Be careful about precision, since acs does not support floating point you can only get the integer portion of the result and the decimal part is cut off. Also becareful not to divide by zero as this will cause an error. Basic usage:

z = x / y;

If you want to divide a number back into a variable then see the '/=' operator.

'%' (binary)

Modulous Operator
This rather strange operator gives the remainder of the division of two numbers. To help you understand in normal math 10/4 is 2 remainder 2 so in acs 10 / 4 would give you 2 and 10 % 4 would give you 2. Basic usage:

z = x % y;

If you want to mod a number back into a variable then see the '%=' operator.

'&' (binary)

Bitwise AND Operator
This operator takes the bits of the left and right side and logically ANDs them together. The AND truth table:

    | p & q
p q |
0 0 |   0
0 1 |   0
1 0 |   0
1 1 |   1

As you can see it returns a true if both are true. So if you have 7 and 6 and you AND them together you would have 6 because 0 1 1 1 & 0 1 1 0 = 0 1 1 0 Basic usage:

z = x & y;

If you want to logically AND the whole statement rather than each bit individually then see the '&&' operator. If you want to logically AND a number back into a variable then see the '&=' operator.

'|' (binary)

Bitwise OR Operator
This operator logically ORs two numbers together. The OR truth table:

    | p | q
p q |
0 0 |   0
0 1 |   1
1 0 |   1
1 1 |   1

As you can see it returns a true as long as one of them is true. So if you logically OR 7 and 6 you would get 7 because 0 1 1 1 | 0 1 1 1 0 = 0 1 1 1 Basic usage:

z = x | y;

If you want to logically OR a whole statement rather than each bit then see the '||' operator. If you want to logically OR a number back into a variable then see the '|=' operator.

'^' (binary)

Bitwise XOR Operator (sometimes refered to as EOR as well)
This is the exclusive-or operator and logically XORs two numbers together. The XOR truth table:

    | p ^ q
p q |
0 0 |   0
0 1 |   1
1 0 |   1
1 1 |   0

As you can see it returns a true if both are different. So if you logically XOR 7 and 6 you would get 1 because 0 1 1 1 ^ 0 1 1 0 = 0 0 0 1 Basic usage:

z = x ^ y;

If you want to logically XOR a whole statement rather than each bit then your going to have to write a function that does it for you because Acs does not have an operator that does. If you want to logically XOR a number back into a variable then you should see the '^=' variable.

'~' (unary)

Bitwise NOT Operator.
This logically NOTs every bit in the number. The NOT truth table:

  | ~p
p |
0 |  1
1 |  0

As you can see it returns a true if false and a false if true. So if you logically NOT 7 then you get 8 (actually since integers in ACS are 32 bits and not 4 you actually get a very big negative number but you should be able to get the point) becase ~0 1 1 1 = 1 0 0 0 Basic usage:

y = ~x;

If you want to logically NOT a whole statement rather than each bit then see the '!' operator. If you want to logically NOT a variable and then store it back into itself then you need to do it the long way:

x = ~x;

'<<' (binary)

Bitwise Left Shift Operator
This operator takes all the bits on the left and shifts them by the number on the right. So if you have 7 and left shift it 1 then you have 14 because 0 1 1 1 << 1 = 1 1 1 0.

Note: this is exactly the same thing as multibling the number on the left by the cooresponding power of 2 on the left, so 4 << 3 = 32 because 2^3 is 8 and 8 by 4 is 32 but instead of multipling 4 * 8 its faster to do 4 << 3.

Note 2: If you shift a bit off you lose it. Since we are dealing with 32 bit integers a 1 << 32 shift will cause the one bit to be shifted off the variable and you would be left with 0.

Basic usage:

z = x << y;

If you want to shift the bits of a variable left and store it back into itself then see the '<<=' operator.

'>>' (binary)

Bitwise Right Shift Operator
This is the same thing as the left shift operator but the exact opposite. It takes the number on the left and moves its bits right by the number on the right. So 6 >> 1 = 3 because 0 1 1 0 >> 1 = 0 0 1 1.

Note: this is exactly the same as dividing a number the cooresponding power of 2 but faster. So, 24 >> 3 = 3 because 2^3 is 8 and 24 / 8 = 3.

Note 2: if you shift the bits off the variable then you lose the bit. If you take 1 and shift it 1: 1 >> 1 then you have zero because the bit got shifted off.

Basic usage:

z x >> y;

If you want to shift the bits of a variable right and store it back into itself then see the '>>=' operator.

'==' (binary)

Relational Equals To Operator.
This compares two statements and then returns a true if they equal and a false if they don't. Basic usage:

z x == y;

'!=' (binary)

Relational Doesn't Equal To Operator
This compares two statements and then returns a true if the don't equal and false if they do. Basic usage:

z = x != y;

'>' (binary)

Relational Greater Than Operator
This compares two statements and then returns true if the value on the left is greater than the value on the right or false otherwise; if the two are equal, it will return false. Basic usage:

z = x > y;

'>=' (binary)

Relational Greater Than or Equal To Operator
This compares two statements and then returns true if the value on the left is greater than or equal to the value on the right or false otherwise. Basic usage:

z = x >= y;

'<' (binary)

Relational Less Than Operator
This compares two statements and then returns true if the value on the left is smaller than the value on the right or false otherwise; if the two are equal it will return false. Basic usage:

z = x < y;

'<=' (binary)

Relational Less Than or Equal To Operater
This compares two statements and then returns true if the value on the left is smaller than or equal to the value on the right, or false otherwise. Basic usage:

z = x <= y;

'&&' (binary)

Logical AND Operator
This operator compares two statements and returns true if both statements are true, or false if either or both are false. See the following table:

    | p & q
p q |
0 0 |   0
0 1 |   0
1 0 |   0
1 1 |   1

Note that since any non-zero number is true, then this returns true: 4 && 3 It's best used when linking together relational operators. Basic usage:

if (x && y) { statement }

It can also be used for assignement:

z = x && y

Linking together relational operators:

z = x == y && a != b

'||' (binary)

Logical OR Operator
This operator compares two statements and then returns a true or false based on the OR truth table:

    | p & q
p q |
0 0 |   0
0 1 |   1
1 0 |   1
1 1 |   1

However, since any non-zero number is true this will return a true statement 4 || 0. Its best used when linking together relational operators. Basic usage:

z = x || y;

Linking together relational operators:

z = x == y || a != b

'!' (unary)

Logical NOT Operator
This operator NOTs a statement. If its true (any non-zero number) then it returns a false (or zero) and if its false it returns a true (usually a 1). Its best used with relational operators amd other logical operators. Basic usage:

y = !x

Used in logical operators:

z = !(x && y) && (x || Y)

'+=' (binary)

Addition Assignment Operator
This operator takes the value on the right, adds it to the value of the variable on the left, and then stores it back into the variable. It is the equivalent of n = n + y. Basic usage:

x += y;

'-=' (binary)

Subtraction Assignment Operator
This operator takes the value on the right, subtracts it from the value of the variable on the left, and then stores it back into the variable. It is the equivalent of n = n - y. Basic usage:

x -= y;

'*=' (binary)

Multiplication Assignment Operator
This operator takes the value on the right, multiplies it to the value of the variable on the left, and then stores it back into the variable. It is the equivalent of n = n * y. Basic usage:

x *= y;

'/=' (binary)

Division Assignment Operator
This operator takes the value on the right, divides the value of the variable by it, and then stores it back into the variable. It is the equivalent of n = n /y.

Note: all the limitaions of the division operator carry over so see the '/' operator if you are not sure.

Basic usage:

x /= y;

'%=' (binary)

Modulous Assignment Operator
This operator takes the value on the right, divides the value of the varialbe by it, returns the remainder and then stores it back into the variable. It is the equivalent of n = n % y.

Note: if you are unsure on how this operatore works then see the modulous operator:, '%', for more info.

Basic usage:

x %= y;

'&=' (binary)

Bitwise AND Assignment Operator
This operator takes the value on the right, logically ANDs all the bits of it to the value of the variable on the left, and then stores it back into the variable. It is the equivalent of n = n & y.

Note: See the Bitwise AND, '&', operator for more info on how this operator works.

Basic usage:

x &= y;

'|=' (binary)

Bitwise OR Assignment Operator
This operator takes the value on the right, logically ORs all the bits of it to the value of the variable on the left, and then stores it back into the variable. It is the equivalent of n = n | y.

Note: See the Bitwise OR, '|', operator for more info on how this operator works.

Basic usage:

x |= y;

'^=' (binary)

Bitwise XOR Assignment Operator
This operator takes the value on the right, logically XORs all the bits of it to the value of the variable on the left, and then stores it back into the variable. It is the equivalent of n = n ^ y.

Note: See the Bitwise XOR, '^', operator for more info on how this operator works.

Basic usage:

x ^= y;

'<<=' (binary)

Bitwise Left Shift Assignment Operator
This operator takes the value on the right, shifts the bits of the value of the variable on the left by the that number, and then stores it back into the variable. It is the equivalent of n = n << y.

Note: See the Bitwise Left Shift, '<<', operator for more info on how this operator works.

Basic usage:

x <<= y;

'>>=' (binary)

Bitwise Right Shift Assignment Operator
This operator takes the value on the right, shifts the bits of the value of the variable on the right by that number, and then stores it back into the variable. It is the equivalent of n = n >> y.

Note: See the Bitwise Right Shift, '>>', operator for more info on how this operator works.

Basic usage:

x >>= y;

Note: more to come but thats as far as I have gotten so feel free to add to it as you see fit (keep in mind that comments, keywords and preproccessor directives still need to be done).