Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Me again! what does ~ mean

Tarak Voss
Meanderer
Join date: 14 Oct 2006
Posts: 330
12-07-2008 23:57
OK this is a couple of lines from a script:

integer s = llSubStringIndex(data, "=";);//understand this
if(~s)
{ etc

I don't understand the if - what does the '~' mean?
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
12-08-2008 00:19
To be extremely non-technical... it means "not". And it's used as a shortcut to see if a value is not -1. If you take a calculator and enter "zero", then the "not" key, you get -1. If you enter "1" and the "not" key, you get -2, 2 = -3 and so on. -1, -2 and -3 are all TRUE values.

Now, if you enter -1 followed by the "not" key, you get zero.... FALSE.

So, when using functions that return a -1 for non-existing index values such as inventory, list or string searches... you can't check for TRUE/FALSE because zero is a valid index. So using "~" is like saying: if (s != -1) ... the code only executes if the result of ~s is TRUE (anything but -1).
Tarak Voss
Meanderer
Join date: 14 Oct 2006
Posts: 330
12-08-2008 00:49
Great thanks - you don't have a similar simple English explanation of negative index do you ? :)
Kerik Rau
Registered User
Join date: 8 Mar 2007
Posts: 54
12-08-2008 01:12
From: Tarak Voss
Great thanks - you don't have a similar simple English explanation of negative index do you ? :)


Index:

0 1 2 3 4 5

Negative index:

-6 -5 -4 -3 -2 -1

Usually negative indexes are used to access members from the "right" side rather then from the left. For instance using negative indexes you can get the last 3 digits of a word (-3, -1) without having to know the length of that word (length - 2, length).
Tarak Voss
Meanderer
Join date: 14 Oct 2006
Posts: 330
12-08-2008 01:24
Thanks for that so if a sentence was "the red fox got dysentery" (8,-1) would equal "ysentery" - is that correct?
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
12-08-2008 03:04
From: DoteDote Edison
To be extremely non-technical... it means "not". And it's used as a shortcut to see if a value is not -1. If you take a calculator and enter "zero", then the "not" key, you get -1. If you enter "1" and the "not" key, you get -2, 2 = -3 and so on. -1, -2 and -3 are all TRUE values.

Now, if you enter -1 followed by the "not" key, you get zero.... FALSE.

That was an awkward way to say a simple thing:)

~X is a bit wise negation apposed to !X which is a logical negation.

Say we have the four bit number in binary X=1010, then ~X==0101, X==TRUE and !X==FALSE
_____________________
From Studio Dora
Tarak Voss
Meanderer
Join date: 14 Oct 2006
Posts: 330
12-08-2008 03:36
Thanks, but I understand the "awkward way" better.

My main thing is trying to get head around negative indexing - thanks
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
12-08-2008 06:09
From: Tarak Voss
Thanks for that so if a sentence was "the red fox got dysentery" (8,-1) would equal "ysentery" - is that correct?


No, that would return " fox got dysentery" because in your example 8 is positive so it counts from the start of the string, where the first character of the string is 0.

you would need -8,-1 to get the result you are after.

And at the risk of being pedantic !1 is NOT a logical negation it is a "boolean inversion", that is, Zero becomes One any other value(positive or negative) becomes Zero.

if anything is a logical negation, it is the ~, more correctly termed the 1's Compliment, as many early binary notations used 1's compliment to indicate a negative number, that is if the highest order bit is one, the number is negative. Most systems now use 2's compliment so -1 is the same as ~1+1.

*you did say you were happier with the more technical explanation, I hope I didn't go too far :)*
Yumi Murakami
DoIt!AttachTheEarOfACat!
Join date: 27 Sep 2005
Posts: 6,860
12-08-2008 09:05
Basically, you already know that SL indexes strings forwards, with the first letter being zero, the second 1, and so on. So

0 h
1 e
2 l
3 l
4 o

However, LSL also indexes them backwards, with the LAST letter being -1, the next-to-last being -2, and so on. So each letter actually has two indexes:

0 or -5 h
1 or -4 e
2 or -3 l
3 or -2 l
4 or -1 o

The negative indexes are only for your convenience, so that you can use just "-2" as an index instead of "llStringLength(x)-2". Functions that return indexes, like llSubStringIndex always return the positive index and use -1 as a "not found" signal.
Tarak Voss
Meanderer
Join date: 14 Oct 2006
Posts: 330
12-08-2008 13:53
You are all wonderful - this bear of little brain is soo impressed - it makes sense - there is light!!
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
12-08-2008 14:19
Personally, I would make this test 'if(x < 0)' or 'if(x == -1)' rather than 'if(~x)', simply because the return value is not a bitmap and so it's an inconsistent idiom.

I've done it the other way... micro-optimizing source... and been burned trying to read a stranger's code. The stranger being "me, 5 years ago". :)
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
12-08-2008 16:17
From: Argent Stonecutter
I've done it the other way... micro-optimizing source... and been burned trying to read a stranger's code. The stranger being "me, 5 years ago". :)


You have an amazing memory Argent -- takes me about 3 months to totally forget what i was doing in a non-trivial script so i tend to avoid *any* optimization that is not proven necessary by testing.

/esc
_____________________
http://slurl.com/secondlife/Together
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
12-08-2008 16:24
From: Escort DeFarge
You have an amazing memory Argent -- takes me about 3 months to totally forget what i was doing in a non-trivial script so i tend to avoid *any* optimization that is not proven necessary by testing.
I didn't say I'd never managed to become a stranger quicker than that. :)

But there's nothing like a five year old program written by the damn fool you used to be to wise you up and take pity on the damn fool you'll be five years from now.
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore