Don't click here unless you want to be banned.

LSL Wiki : Unary

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl338.us.archive.org

Unary

Unary operators take one argument. In LSL these arguments are normally integers.


Operator Type Meaning Return Value Effect
variable++ Arithmetic post-increment variable variable is set to variable + 1
++variable Arithmetic pre-increment variable + 1 variable is set to variable + 1
variable-- Arithmetic post-decrement variable variable is set to variable - 1
--variable Arithmetic pre-decrement variable - 1 variable is set to variable - 1
!variable Logical Logical NOT 1 if variable is 0, otherwise 0 None
-variable Arithmetic Unary negation negative value of variable None
~variable Bitwise Bitwise NOT flips each bit of variable None

Examples:

integer x = 5;
llSay(0, (string)(x++));
//Says "5" and x is set to 6
integer x = 5;
llSay(0, (string)(++x));
//Says "6" and x is set to 6

Operators | Unary | Binary | Bitwise | Boolean | Equality | Assignment
Comments [Hide comments/form]
It is said that j++ is slower than j = j + 1. I ran a test to see whether that was true:

Increment test: It took 129.376556 seconds to exec j = j + 1 10,000 times in a for loop.
Increment test: It took 153.302780 seconds to exec j++ 10,000 times in a for loop.

The math for that test works out to j = j + 1 being .002 seconds faster than j++. Personally I'm staying with ++.
-- HunsValen (2004-05-11 04:47:54)
That's 0.002 seconds I can't afford to waste!

This is interesting Huns, why not move this info up in to the page, or on to the speed-tweaks page?
-- WednesdayGrimm (2004-05-14 11:24:11)
Also note that you can combine assignment and binary operators, like other languages.
"j = j + 1;" can also be written as "j += 1;"
Technically, the "short-hand" version is faster - but tests have shown the difference to only be about 0.00007 seconds. :-)
-- TreadWhiplash (2005-01-16 15:21:56)
j++ is slower than j = j + 1, but by my tests, ++j are about the same speed. j += 1 is slightly slower, but faster than j++.
-- AlondriaLeFay (2005-10-16 09:33:20)
Classically the reason that j++ is slower than ++j is that, in an unoptimised compiler, a useless copy of j's old state is saved then destroyed. From an assembler standpoint ++j should be the fastest operation. I have yet to verify this though.
-- BasicerSL (2006-03-29 13:46:06)
I believe there are some tests on this somewhere around the wiki.
-- DolusNaumova (2006-04-01 08:22:50)
Attach a comment to this page: