Difference between revisions of "LSL Operators"

From Second Life Wiki
Jump to navigation Jump to search
(One's & Two's Complement are binary methods for negative numbers, Another name for bitwise-not is complement. The name is a complication.)
Line 148: Line 148:
== Bitwise Tips ==
== Bitwise Tips ==


{| {{Prettytable}}
{| {{Prettytable|style=float:left; margin-top:0;}}
|+ Equivalencies
|+ Equivalencies
!AND
!AND
Line 162: Line 162:
|-
|-
|}
|}
Code on the left is functionally equivalent to code on the right. '''a''' and '''b''' need not be variables. In certain circumstances these equivalencies can be used to simplify complex code.
Code on the left is functionally equivalent to code on the right. '''a''' and '''b''' need not be variables. In certain circumstances these equivalencies can be used to simplify complex code. I believe this to be an extension of {{Wikipedia|De Morgan's laws}}.


{{LSLC|}}{{LSLC|Syntax}}{{LSLC|Keywords}}
{{LSLC|}}{{LSLC|Syntax}}{{LSLC|Keywords}}

Revision as of 12:52, 8 May 2009

Operators are used to cause an operation (or mathematical action) to be performed on one (such as !) or two operands. The easy and common example is 1 + 2 where 1 and 2 are operands, and the + is the operator.

This concept can be extended much further with LSL since operands can be variables with the special case of the assignment operators requiring that the left hand side be a variable.

Operator Description Usage Example
() [] . Parenthesis, Brackets, and Dot do this second (do this first)
(type) Typecasting message = "The result is:" + (string) result;
! ~ ++ -- Binary-NOT, Bitwise-NOT, Increment, Decrement counter++;
* / % Multiply/Dot-Product, Divide, Modulus/Cross-Product rollover = (count + 1)%5;
- Subtraction one = 3 - 2;
+ Addition or joining Strings two = 1+1;

text = "Hello" + "World";

+ Concatenation or joining Lists myList = [1, 2, 3] + [4, 5];

newList = oldList + addList;

<< >> Left Shift, Right Shift eight = 4 << 1;
< <= > >= Less Than, Less Than Or Equal To,

Greater Than, Greater Than or Equal To

isFalse = (6 <= 4);
==  != Comparison Equal, Comparison Not Equal isFalse = ("this" == "that");
& Bitwise AND zero = 4 & 2;

four = 4 & 4;

^ Bitwise XOR zero = 4 ^ 4;

six = 4 ^ 2;

| Bitwise OR four = 4 | 4;

six = 4 | 2;

|| Comparison OR isTrue = (FALSE || TRUE);
&& Comparison AND isFalse = (FALSE && TRUE);
= += -= *= /= %= Assignment four = 4;


Note: Unlike most, if not all, other languages that use the C style && and || operators, both operands are always evaluated. For example,

<lsl>if (TRUE || 1/0) llSay( 0, "Aha!" );</lsl>

will cause a Math Error rather than say "Aha".


Note: The order of precedence of boolean operators is unclear. It is possible that there is a bug in the expression parser, making precedence inconsistent, or it may simply be that || and && have equal precedence; testing is inconclusive. Thus, when in doubt, parenthesize.

Note: The order of evaluation appears to be backwards from most languages. If the value of x starts as 1 then the first two conditions below evaluate false and the second two evaluate true:

<lsl>(x && (x = 0) == 0 && x)</lsl> <lsl>(x && (x = 0) == 0 && x == 0)</lsl> <lsl>(x == 0 && (x = 0) == 0)</lsl> <lsl>(x == 0 && (x = 0) == 0 && x)</lsl>

Both sides are evaluated regardless of the the truth of either side.


+ Operator

result = left + right

Left Type Right Type Result Type Description
integer integer integer Adds left and right
integer float float Adds left and right
float integer float Adds left and right
string string string Concatenates right onto the end of left.
list * list Concatenates right onto the end of left.
* list list Affixes left onto the start of right.
vector vector vector Adds left and right
rotation rotation rotation Adds left and right
Not useful for combining rotations, use * or / instead.

Shorthand Operators

Alternatives to the simple '=' operator...

Simple assignment operator Shorthand operator
a = a + 1 a += 1
a = a – 1 a -= 1
a = a * (n+1) a *= (n+1)
a = a / (n+1) a /= (n+1)
a = a % b a %= b

Bitwise Tips

Equivalencies
AND OR
~(a & b) ~a | ~b
~a & ~b ~(a | b)
a & ~b ~(~a | b)
~(a & ~b) ~a | b

Code on the left is functionally equivalent to code on the right. a and b need not be variables. In certain circumstances these equivalencies can be used to simplify complex code. I believe this to be an extension of "Wikipedia logo"De Morgan's laws.