Explaination of code function.

Recommended Posts

if( !~       .................)

I look at the 'X Y' notation on the lsl wiki but it says nothing. How does this work?

Share on other sites

It depends on what comes after the !~ operators. Would you share?

"~" Is "bitwise not," also called "two's complement."
It inverts each individual bit in binary, then adds one to the result.

• ~(-3) =  2
~(-2) =  1
~(-1) =  0
~0    = -1
~1    = -2
~2    = -3
~3    = -4

In scripting, this is most commonly seen with functions that return an index, or -1 if "nothing is found" if the function returns it to indicate an error.

"!" is "logical not."
Any nonzero (like -589, -1, 1, 2385) integer becomes 0, and 0 becomes 1.

• !~(-3) = 0
!~(-2) = 0
!~(-1) = 1
!~0    = 0
!~1    = 0
!~2    = 0
!~3    = 0

Here, -1 finally becomes 1 (because ~(-1) = 0, and !0 = 1) or "true." This is why you see this syntax used with functions that might return -1 in some cases. The if-check probably roughly translates to "if nothing is found."

Edited by Wulfie Reanimator
Correction
• 3
• 1

Share on other sites

The "~" operator is a bitwise NOT.  In the case of a list operation, it's commonly used to mean that the operation has a value greater than or equal to zero.  So, for example

if ( ~llListFindList ( my_list, [this_value] ) { // Do something }

means "if this_value is anywhere in my_list  -- that is, if it is in position zero or any position greater than zero -- then Do Something."   It means the same as

if ( llListFindList( my_list, [this_value] ) != -1 ) { //Do something }

If we want the  reverse, we negate that operation with "!~".  So

if ( !~llListFindList( my_list, [ this_value ] ) { //Do Something }

means "if this_value cannot be found anywhere in my_list (not even in position zero) , then Do something."  It means the same as

if ( llListFindList( my_list, [ this_value ] ) == -1 ) {//Do something }

Edited by Rolig Loon
typos. as always.
• 3
• 1

Share on other sites
9 minutes ago, Rolig Loon said:

The "~" operator is a bitwise NOT.  In the case of a list operation, it's commonly used to mean that the operation has a value greater than or equal to zero.  So, for example

Oh i see. I was confused with the 'bitwise not'. I put this up because the sl wiki is blank aswell.

Edited by steph Arnott

Share on other sites
9 minutes ago, Wulfie Reanimator said:

Thank you.

Share on other sites
1 hour ago, steph Arnott said:

the sl wiki is blank aswell.

It's not very detailed, but the SL wiki does have an LSL "operators" page which at least lists the ~ bitwise negation operator.

Otherwise, although I might quibble with nomenclature, the responses rightly explain that ~ operating on an integer value* is almost always testing for -1.

That bitwise negation is actually called a "one's complement", but "two's complement" is involved here also because that's the term for how negative integers are represented with the high-order sign bit set, and how -1 ends up being all bits set.

Logical operations treat all numbers as TRUE except the uniquely FALSE 0 (all bits cleared), so testing for a unique -1 (all bits set) is to test whether the bitwise negation (~) of the value is FALSE.

______________
*as opposed to an integer representation of a bitfield

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.