Post by eq on Mar 22, 2014 12:52:48 GMT 1
I wrote a tutorial for wolfcam a while ago, and since then one thing has been bugging me. That's boolean...
A long while ago I found jones posted canabis's overview of the fx system. I've never used q3mme, but I know that the WC script system is more or less identical, so it was very useful.
In there, it is mentioned:
I have spent some time trying to work out what the last two lines mean. Here's my interpretation:
In wolfcam fx scripting &, &&, | and || are boolean operators.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
& means 'bitwise-and'. Compares bits in two values and gives '1' wherever both bits are '1'
e.g. 5 & 3
| 4s 2s 1s
--|-----------
5 | 1 0 1
3 |AND 0 1 1
--|-----------
1 | 0 0 1
n.b. can be used to test parity (i.e. even or odd number):
6 & 1 = 0 //therefore even, as the only way a binary number can be odd is if it has a '1' in the final bit
7 & 1 = 1 //therefore odd
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&& means 'logical and'. It tests if both statements are true
starts with the first operand - if this is false, the right operand does not need to be tested
gives '1' for true or '0' for false accordingly.
I believe that wolfcam and q3mme do not support boolean algebra. If they did, perhaps you could use statements such as:
if ((time < 6000) && (t0 ! 0) {
<block>
}
However, we are limited to simple tests such as:
e.g. 1 && 0 = 0 //'1' is tested first, and as it is true (i.e. non-equal to 0), 0 is tested, which is false. Therefore the statement if false
e.g. 6 && 3 = 1 //both operands are non-equal to 0, (i.e. 6 ! 0 = 1 and 3 ! 0 = 1) so the statement is true
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| means 'bitwise-or'. Compares bits in two values and gives a '1' wherever either (or both) bits are 1
e.g. 5 & 3
| 4s 2s 1s
--|----------
5 | 1 0 1
3 |OR 0 1 1
--|----------
7 | 1 1 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|| means 'logical or'. Tests both operands (left then right) and if either is true, the statement is given as true (1)
e.g. 8 || 0 = 1 //because 8 is true, the second operand doesn't need to be tested
e.g. 0 || 0 = 0 //neither operand is true
e.g. 6 || 1 = 1 //both operands are true
Has anyone really tested these test operators? (I know, the irony)
And why does canabis say:
& -> ( 2 & 2 ) returns 1 -> 2 && 2
| -> ( 2 | 0 ) return 1 -> 2 || 0
if the & and | tests are so different to the && and || tests?
A long while ago I found jones posted canabis's overview of the fx system. I've never used q3mme, but I know that the WC script system is more or less identical, so it was very useful.
In there, it is mentioned:
Math test operators:
Special group of operators that return 0 or 1 depending on a test
< -> ( 0 < 2 ) returns 1 -> 2 smaller than 0
> -> ( 2 > 0 ) returns 1 -> 2 bigger than 0
! -> ( 2 ! 0 ) returns 1 -> 2 unequeal to 0
= -> ( 2 = 2 ) returns 1 -> 2 equal to 2
& -> ( 2 & 2 ) returns 1 -> 2 && 2
| -> ( 2 | 0 ) return 1 -> 2 || 0
Special group of operators that return 0 or 1 depending on a test
< -> ( 0 < 2 ) returns 1 -> 2 smaller than 0
> -> ( 2 > 0 ) returns 1 -> 2 bigger than 0
! -> ( 2 ! 0 ) returns 1 -> 2 unequeal to 0
= -> ( 2 = 2 ) returns 1 -> 2 equal to 2
& -> ( 2 & 2 ) returns 1 -> 2 && 2
| -> ( 2 | 0 ) return 1 -> 2 || 0
I have spent some time trying to work out what the last two lines mean. Here's my interpretation:
In wolfcam fx scripting &, &&, | and || are boolean operators.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
& means 'bitwise-and'. Compares bits in two values and gives '1' wherever both bits are '1'
e.g. 5 & 3
| 4s 2s 1s
--|-----------
5 | 1 0 1
3 |AND 0 1 1
--|-----------
1 | 0 0 1
n.b. can be used to test parity (i.e. even or odd number):
6 & 1 = 0 //therefore even, as the only way a binary number can be odd is if it has a '1' in the final bit
7 & 1 = 1 //therefore odd
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&& means 'logical and'. It tests if both statements are true
starts with the first operand - if this is false, the right operand does not need to be tested
gives '1' for true or '0' for false accordingly.
I believe that wolfcam and q3mme do not support boolean algebra. If they did, perhaps you could use statements such as:
if ((time < 6000) && (t0 ! 0) {
<block>
}
However, we are limited to simple tests such as:
e.g. 1 && 0 = 0 //'1' is tested first, and as it is true (i.e. non-equal to 0), 0 is tested, which is false. Therefore the statement if false
e.g. 6 && 3 = 1 //both operands are non-equal to 0, (i.e. 6 ! 0 = 1 and 3 ! 0 = 1) so the statement is true
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| means 'bitwise-or'. Compares bits in two values and gives a '1' wherever either (or both) bits are 1
e.g. 5 & 3
| 4s 2s 1s
--|----------
5 | 1 0 1
3 |OR 0 1 1
--|----------
7 | 1 1 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|| means 'logical or'. Tests both operands (left then right) and if either is true, the statement is given as true (1)
e.g. 8 || 0 = 1 //because 8 is true, the second operand doesn't need to be tested
e.g. 0 || 0 = 0 //neither operand is true
e.g. 6 || 1 = 1 //both operands are true
Has anyone really tested these test operators? (I know, the irony)
And why does canabis say:
& -> ( 2 & 2 ) returns 1 -> 2 && 2
| -> ( 2 | 0 ) return 1 -> 2 || 0
if the & and | tests are so different to the && and || tests?