| View previous topic :: View next topic |
| Author |
Message |
Mr B
Joined: 20 Mar 2003 Posts: 382
|
Posted: Sat Mar 24, 2007 4:43 pm Post subject: "And" vs. "&&" |
|
|
I've been having a great deal of trouble debugging a script. It has an "if" block, with two states that both need to be true (or greater than zero). It used to work. Sometime during the WIP updates, it stopped working. Funny thing is, I didn't change any of the logical coding.
I tested the two values that the if statement depends on, and they both function. After a bunch of fruitless testing and modifying, I tried a remote chance and changed my "if(this, and, that)" to "if(this, &&, that)", based on something I'd read in the whatsnew.txt file. It (seems to have) worked!
Question is, why was the functionality of "and" changed...or am I completely missing what's going on? Are the other logic statements changed as well? |
|
| Back to top |
|
 |
Bob the Hamster OHRRPGCE Developer

Joined: 22 Feb 2003 Posts: 2526 Location: Hamster Republic (Southern California Enclave)
|
Posted: Sun Mar 25, 2007 7:27 am Post subject: Re: "And" vs. "&&" |
|
|
| Mr B wrote: | I've been having a great deal of trouble debugging a script. It has an "if" block, with two states that both need to be true (or greater than zero). It used to work. Sometime during the WIP updates, it stopped working. Funny thing is, I didn't change any of the logical coding.
I tested the two values that the if statement depends on, and they both function. After a bunch of fruitless testing and modifying, I tried a remote chance and changed my "if(this, and, that)" to "if(this, &&, that)", based on something I'd read in the whatsnew.txt file. It (seems to have) worked!
Question is, why was the functionality of "and" changed...or am I completely missing what's going on? Are the other logic statements changed as well? |
&& is logical.
"and" is bitwise.
In most cases, you can use either one, but in the future it is probably best to use && in conditionals, and use "and" only when manipulating bits. |
|
| Back to top |
|
 |
TMC On the Verge of Insanity
Joined: 05 Apr 2003 Posts: 3240 Location: Matakana
|
Posted: Mon Mar 26, 2007 12:43 am Post subject: |
|
|
The behaviour of "and" never changed. The behaviour of "&&", however, (hopefully) will before release. (It'll be made shortcut evaluating.)
At some point comparison operators like >> and == were changed to return 1 instead of -1. This is the problem you would have run into: -1 is safer in expressions using bitwise and, 1 is for people who attempt "(hero is walking(me) == true)" _________________ "It is so great it is insanely great."
Last edited by TMC on Sun Apr 01, 2007 7:40 pm; edited 1 time in total |
|
| Back to top |
|
 |
Mr B
Joined: 20 Mar 2003 Posts: 382
|
Posted: Sat Mar 31, 2007 9:27 am Post subject: |
|
|
Okay, interesting...
But the behavior has changed -- the statement used to function with "and" but it now does not. Or so it seems. And I wasn't using any comparison operators, right?
I feel very confused. |
|
| Back to top |
|
 |
TMC On the Verge of Insanity
Joined: 05 Apr 2003 Posts: 3240 Location: Matakana
|
Posted: Sun Apr 01, 2007 7:43 pm Post subject: |
|
|
"1, and, 2" is false, '-1, and 2" is true and "1 && 2" is true.
What was the expression anyway? There must be something in it returning a boolean value. _________________ "It is so great it is insanely great." |
|
| Back to top |
|
 |
Mr B
Joined: 20 Mar 2003 Posts: 382
|
Posted: Thu Apr 05, 2007 8:18 am Post subject: |
|
|
Don't remember off the top of my head. I think it was something along the line of:
| Code: | | if(variable, and, script) |
where the script returns a value from zero to about sixty.
So why the heck did we have access only to a bitwise "and", and why was it recommended for "if" statements? That was pretty much their only use...
Incidentaly, what would I use a bitwise "and" for? Does it transpose the "ones" in a pair of numbers? (11010000,and,01100110 => 11110110) |
|
| Back to top |
|
 |
Moogle1 Scourge of the Seas Halloween 2006 Creativity Winner


Joined: 15 Jul 2004 Posts: 3377 Location: Seattle, WA
|
Posted: Thu Apr 05, 2007 8:28 am Post subject: |
|
|
That's OR. AND returns the bits that are true in both:
11010000,and,01100110 => 01000000 _________________
|
|
| Back to top |
|
 |
Bob the Hamster OHRRPGCE Developer

Joined: 22 Feb 2003 Posts: 2526 Location: Hamster Republic (Southern California Enclave)
|
Posted: Thu Apr 05, 2007 9:03 am Post subject: |
|
|
| Mr B wrote: | | So why the heck did we have access only to a bitwise "and", and why was it recommended for "if" statements? That was pretty much their only use... |
Because QuickBasic only had bitwise AND and OR operators. I was so used to dealing with them, that it never occured to me (at the time when I was first creating Plotscripting) that logical AND and OR operators were useful.
IIRC, C and C++ both over-use bitwise operators as logic operators also. |
|
| Back to top |
|
 |
Moogle1 Scourge of the Seas Halloween 2006 Creativity Winner


Joined: 15 Jul 2004 Posts: 3377 Location: Seattle, WA
|
Posted: Thu Apr 05, 2007 9:43 am Post subject: |
|
|
Close, but the logical operators are the common ones in C-derivative languages. (&&/|| is logical, &/| is bitwise.) _________________
|
|
| Back to top |
|
 |
Bob the Hamster OHRRPGCE Developer

Joined: 22 Feb 2003 Posts: 2526 Location: Hamster Republic (Southern California Enclave)
|
Posted: Thu Apr 05, 2007 11:12 am Post subject: |
|
|
| Moogle1 wrote: | | Close, but the logical operators are the common ones in C-derivative languages. (&&/|| is logical, &/| is bitwise.) |
I stand corrected. Maybe it was just my crazy C teacher telling us all to use & when we should have been using && |
|
| Back to top |
|
 |
Mr B
Joined: 20 Mar 2003 Posts: 382
|
Posted: Fri Apr 06, 2007 11:19 am Post subject: |
|
|
| Moogle1 wrote: | That's OR. AND returns the bits that are true in both:
11010000,and,01100110 => 01000000 |
Ah! The shame! Don't tell anyone I did that...
This is pretty interesting, and probably explains those few haunting oddnesses that I could never find.
So, what would constitute a "true" in the context of a bitwise AND? One or more "on" bits being shared between the two values?
So...the reason that "-1, and, 2" is true is because the "-1" has all of its bits turned on, and the two has its second-to-least-significant-bit turned on? |
|
| Back to top |
|
 |
|