Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Storing bitwise OR result in an integer variable

Tralos Westerburg
XTSL Radio Lead DJ
Join date: 14 Jun 2004
Posts: 43
09-01-2004 10:03
Would one of you gurus tell me why the following code produces a syntax error:

From: someone
integer i;

i = ANIM_ON | SMOOTH | LOOP | SCALE | PING_PONG;


I assume that bitwise OR'ing 5 integer constants would provide an integer value. Am I insane or just stupid?

Thanks in advance!
Cross Lament
Loose-brained Vixen
Join date: 20 Mar 2004
Posts: 1,115
09-01-2004 10:40
Couldn't you just:

CODE


integer i ;

i = ANIM_ON + SMOOTH + LOOP + SCALE + PING_PONG ;



Same diff, isn't it? :D
_____________________
- Making everyone's day just a little more surreal -

Teeple Linden: "OK, where did the tentacled thing go while I was playing with my face?"
Wednesday Grimm
Ex Libris
Join date: 9 Jan 2003
Posts: 934
09-01-2004 10:46
From: someone
Originally posted by Cross Lament
Couldn't you just:


Yes for this specific case, but in general no.

Consider if you have nFlags and you don't know what's in there, and you want to turn animation on, then
nFlags | ANIM_ON
can be very different from
nFlags + ANIM_ON

Also, it would be periously difficult to try turning a flag off using arithmetic operators.

As to the original question Tralos, that looks right, and should work. That's not your entire script right, you have a default state with some events in there too right? If not that's the error.

If so, sometimes the LSL editor isn't the best at telling you which line the error is actually on.

Try adding those two lines to the top of the default script. If it doesn't compile, you may have found a bug!
_____________________
Sarcasm meter:
0 |-----------------------*-| 10
Rating: Awww Jeeze!
Tralos Westerburg
XTSL Radio Lead DJ
Join date: 14 Jun 2004
Posts: 43
09-01-2004 11:06
Oh no, that's not the whole script, Wednesday. :)

Now that SL is back up I just retried my script and discovered what is apparently wrong, although I'm not sure why it should make any difference.

This is what I was doing that throws a syntax error:
From: someone
integer i = ANIM_ON | SMOOTH | LOOP | SCALE | PING_PONG;
However, declaring the variable and assigning the value on separate lines, as I represented in my post, works just fine:
From: someone
integer i;

i = ANIM_ON | SMOOTH | LOOP | SCALE | PING_PONG;

My question now is, should it matter whether the declaration and assignment are on the same line or is this still possibly a bug?
Kurt Zidane
Just Human
Join date: 1 Apr 2004
Posts: 636
09-01-2004 11:20
| operator may some times work like + operator, but it doesn't always operate like +. (2 | 3) = 3 while (10 | 3) returns the value 11. And (10 | 10) returns the value of 10.

In theory I think you could use any operator, includes the | operator, as many times as you want on a single line. Because each operator is triggered one at a time. I do not know what priority | has, so I can't tell you if / or | would be trigger first. Nor can I tell you witch way multiple | operators would be processed. But I would assume it would be right to left. ie 0 | 1 | 0 | 1 | 0 = (0 | (1 | (0 | (1 | 0))))

But what would be the point of processing constants? I could tell you 100 percent of the time that ANIM_ON | SMOOTH | LOOP | SCALE | PING_PONG = 91, and it will always = 91.

btw I can use | in the declaration of a variable just fine. Make sure you used ; and look at the lines above it to see if it's right. you could also try commenting out | and see where your code chokes.
Fractal Mandala
Registered User
Join date: 15 Dec 2003
Posts: 60
09-01-2004 11:34
From: someone
Originally posted by Tralos Westerburg
This is what I was doing that throws a syntax error:However, declaring the variable and assigning the value on separate lines, as I represented in my post, works just fine:
My question now is, should it matter whether the declaration and assignment are on the same line or is this still possibly a bug?


If you're declaring "i" at the beginning of the script, outside of any states, then you can't assign anything to it that requires any functions in the variable declaration. That is, you could do

integer i = 0;

but not

integer i = 1 + 2;

I don't know whether you can have "i=1+2;" outside a state. If your original version was within a state, I'm not sure what would cause that problem.
Jake Cellardoor
CHM builder
Join date: 27 Mar 2003
Posts: 528
09-01-2004 11:38
From: someone
Originally posted by Tralos Westerburg
My question now is, should it matter whether the declaration and assignment are on the same line or is this still possibly a bug?


If this is a global variable, you cannot perform any arithmetic or other operations in the declaration. You can initialize a global with an explicit constant (integer i = ANIM_ON; ), but if you want to combine multiple values, you'll have to do that in the state_entry() event handler for the default state.

With local variables, you should be able to use arbitrary expressions in the declarations: arithmetic, function calls, etc.
Cross Lament
Loose-brained Vixen
Join date: 20 Mar 2004
Posts: 1,115
09-01-2004 11:55
From: someone
Originally posted by Wednesday Grimm
Yes for this specific case, but in general no.


Well, yes... I'm aware of the differences between + and |. I'm just a big fan of flawed solutions. :D
_____________________
- Making everyone's day just a little more surreal -

Teeple Linden: "OK, where did the tentacled thing go while I was playing with my face?"
Wednesday Grimm
Ex Libris
Join date: 9 Jan 2003
Posts: 934
09-01-2004 12:52
From: someone
Originally posted by Tralos Westerburg
My question now is, should it matter whether the declaration and assignment are on the same line or is this still possibly a bug?


As others have said, you can't execute code in a declaration.

If you were a bad person, you could find out what the literal values of those constants are, OR them and just use that numeric result (in hex even now!). But don't do that.
_____________________
Sarcasm meter:
0 |-----------------------*-| 10
Rating: Awww Jeeze!
Wednesday Grimm
Ex Libris
Join date: 9 Jan 2003
Posts: 934
09-01-2004 12:52
From: someone
Originally posted by Cross Lament
Well, yes... I'm aware of the differences between + and |. I'm just a big fan of flawed solutions. :D


/me is a flawed solution.
_____________________
Sarcasm meter:
0 |-----------------------*-| 10
Rating: Awww Jeeze!
Higbee Protagonist
Yggdrai Ranger
Join date: 7 Aug 2003
Posts: 266
09-01-2004 13:21
From: someone
but if you want to combine multiple values, you'll have to do that in the state_entry() event handler for the default state.


Just to clarify this is not true. I have succesfully combined values in different event states as well as in self-created functions. The key is Global verses Local, and Jake's main point is on the money! In LSL I have found however, that using bitwise operators outside of parenthesis can be problematic. I recommend the Arithmetic operators.

Actually hehe that makes me think of a question...were you referring to Bitwise Ops Jake? Can they be used in the state-entry() of DEFAULT without problem?
_____________________
Higbee Protagonist

************************
"Even an immobile stone will respond to you
If you approach with love, call out, and talk to it."
- Shinagawa Tetsuzan

http://www.redprometheus.com
Jake Cellardoor
CHM builder
Join date: 27 Mar 2003
Posts: 528
09-01-2004 16:01
Let me rephrase:

"but if you were trying to combine multiple values in a global declaration, you'll probably want to to do it in the state_entry() event handler for the default state instead."

Of course you can use arithmetic and bitwise operators in other event handlers and functions.
Tralos Westerburg
XTSL Radio Lead DJ
Join date: 14 Jun 2004
Posts: 43
09-03-2004 13:17
Thank you for the responses, everyone. Yes, this is a global variable being declared outside of a state, so it was the attempt at doing a mathematical calculation outside of a state that was throwing the error. The solution was simple, assign the value in default.state_entry. ;)
Higbee Protagonist
Yggdrai Ranger
Join date: 7 Aug 2003
Posts: 266
09-03-2004 17:30
it all makes sense now lol sorry I misunderstood hehe
_____________________
Higbee Protagonist

************************
"Even an immobile stone will respond to you
If you approach with love, call out, and talk to it."
- Shinagawa Tetsuzan

http://www.redprometheus.com