<< Click to Display Table of Contents >> Manual > Script Guide > Script Ver2 (SC2 syntax) > Operators |
Operators
The following operators can be used in a script:
Operator |
|||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Arithmetic Operators |
|
||||||||||||||||||
Comparison Operators |
|
||||||||||||||||||
Logical Operators |
|
||||||||||||||||||
Bitwise Operators |
|
Operator features and usage examples
Implicit string composition and arithmetic operations
If the contents of both operands are strings, the addition is treated as string composition. However, if one operand is a number and the other is a string, the addition is performed as an operation between numbers if the contents of the string are numbers.
var a = "Hello" + " " + "World";//The string "Hello World" is assigned to the variable a var b = "1" + 2; //The number 3 is assigned to the variable b var d = 1 + "2"; //Similarly, it becomes the number 3
Boolean interpretation for arithmetic operations
When you perform arithmetic on Boolean values, T is converted to 1 and F is converted to 0.
var a = T + T + T + F + T; // results in the value 4 (1+1+1+0+1) var b = T * 123 + F * 456; // results in the value 123 (1*123 + 0*456)
Adding and Subtracting Times
For time, you can add or subtract the following combinations of values:
1)[Time] + [Number] or [Time] - [Number]
The number is interpreted as seconds, which can be used to advance or reverse the time value.
var a = #2004/1/1 0:0:0#; var b = a + 10; // b is added 10 seconds to a, resulting in #2004/1/1 0:0:10# var d = a + 10.123; // b is added 10 seconds, or 123 ms, to a, resulting in #2004/1/1 0:0:10.123#
2)[Time] - [Time]
Returns the time difference as a number in seconds.
var a = #2004/1/1 0:0:0#; var b = #2004/1/1 0:1:0#; var d = b - a; // d is 60 (1 minute difference = 60 seconds)
Operations on Arrays
■ Arithmetic operators (+, -, *, /)
a = c(1,2,3); b = a + 1; // b becomes c(2, 3, 4) d = a - 1; // d becomes c(0, 1, 2) e = a * 2; // e becomes c(2, 4, 6) f = a / 2; // f becomes c(0.5, 1, 1.5) g = a + c(2, 3, 4); // g becomes c(3, 5, 7)
■ Comparison operators (<, <=, >, >=, ==)
a = c(1,2,3); b = a < 2; // b becomes c(T, F, F) d = a <= 2; // d becomes c(T, T, F) e = a > 2; // e becomes c(F, F, T) f = a >= 2; // f becomes c(F, T, T) g = a == 2; // f becomes c(F, T, F) h = a > c(3, 2, 1); // g becomes c(F, F, T)
Adding and Subtracting Times
In the case of multidimensional arrays, the operation is performed on all elements at once.
a = c(c(1,2,3), c(4,5,6)); b = a + 2; // b becomes c(c(3,4,5), c(6,7,8))
When performing an operation on arrays with different numbers of elements, such as "c(1,2,3,4,5,6,7,8) + c(1,2,3)", the right-hand side c(1,2,3) is used for the repeated operation. In other words, "c(1,2,3,4,5,6,7,8) + c(1,2,3)" becomes "c(1,2,3,4,5,6,7,8) + c(1,2,3,1,2,3,1,2)". gives the same result, i.e. c(2,4,6,5,7,9,8,10). |
Numeric bit operations
To get the i-th bit in a binary number or to turn only the i-th bit ON/OFF, use the bit operators ('&' and '|').
example)
var b = (a & 0x1) > 0; // If the 1st bit of a's number is 1, then b will be T. var b = (a & 0x2) > 0; // If the 2nd bit of a's number is 1, then b will be T. var b = (a & 0x4) > 0; // If the 3rd bit of a's number is 1, then b will be T. var b = (a & 0x8) > 0; // If the 4th bit of a's number is 1, then b will be T. var a = a | 0x1; // Set the 1st bit of a's number to 1. var a = a | 0x2; // Set the 2nd bit of a's number to 1. var a = a | 0x4; // Set the 3rd bit of a's number to 1. var a = a | 0x8; // Sets the fourth bit of the numeric value of a to 1. var a = a & 0xFFFE; // Sets the first bit of the word value of a to 0. var a = a & 0xFFFD; // Sets the second bit of the word value of a to 0. var a = a & 0xFFFB; // Sets the third bit of the word value of a to 0. var a = a & 0xFFF7; // Sets the fourth bit of the word value of a to 0.