A document say it,
I want 0/1 return values.
document
Arithmetic, Relational and Boolean Operators
We have already used one type of well-known operator in the preceding examples. Operators are the term given to a mathematical symbol used in all calculations. The most common operators are arithmetic operators and are quickly identified. All operators require two operands of data that are placed either side of the operator.
ARITHMETIC OPERATORS
An arithmetic operator can represent an Addition, Subtraction, Multiplication or Division. These operators are represented symbolically as (+) (-) (*) (/) respectively.
The Plus(+) sign specifies that the data on the right of the plus sign must be added to the data on the left. Examples of which you have already seen are:
3 + 4 equals 7
A + B equals the value of B added to the value of A
The minus(-) sign specifies that the data to the right of the minus sign must be subtracted from the data to the left of the minus sign:
3 - 4 equals -1
A - B equals the value of B subtracted from the value of A
An asterix(*) specifies that the data on the right side of the asterix is multiplied by the data on the left side if the asterix:
3 * 4 equals 12
A * B equals the value of B multiplied by the value of A
The slash(/) specifies that the data on the left side of the slash is to be divided by the data on the right side of the slash:
10 / 2 equals 5
A / B equals the value of A divided by the value of B
The MOD specifies that the data on the left side of the MOD is to be divided by the data on the right side of the MOD, and the remainder of the division is the result:
11 MOD 2 equals 1
A MOD B equals the remainder of the division between A and B
RELATIONAL OPERATORS
These operators are less common, unless you have programming experience. These operators represent conditions that are applied to data. The conditions handled are Equal To, Greater Than, Less Than, Greater or Equal To, Less or Equal To and Not Equal To. The purposes of these conditions are to determine the result of a comparison between two data values. A condition result can only be of two possible values. If the condition is false, the resulting value is zero. If the condition is true, the resulting value is one. Take the following examples:
10 = 9 results in 0 because 10 is not the same as 9
10 = 10 results in 1 because 10 is the same as 10
10 > 9 results in 1 because 10 is greater than 9
100 >= 100 results in 1 because 100 is greater or equal to 100
The same relational operators can be applied to real numbers, integer and real variables and in some case strings and string variables. You can compare whether two strings are the same or not the same, and even test whether one string is greater or less than another.
BOOLEAN OPERATORS
Simple Boolean operators provide the last type of operator. Dark Basic Professional allows you to use AND, OR, XOR and NOT operators on your data.
The AND operator works with any integer value, but for demonstration purposes the general rule applies when using this operator:
0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1
What you see is the decision tree of the AND operator. It shows that only if both data operands of the AND operator are 1 will the result be a 1 also. All other cases a zero is returned. To see how this logic works in reality, take the following example:
A=5
B=25
(A > 10) AND (B > 20) so what is the resulting value?
We can determine the result of the parts enclosed in brackets first. We can see the relational operators provide us with the following results:
(A > 10) results in 0 because 5 is not greater than 10
(B > 20) results in 1 because 25 is greater than 20
Our updated calculation looks something like this:
(0) AND (1) results in 0 as our table shows 0 AND 1 = 0
The logic of the table is that only when both sides of the AND operand are 1 will the result of the calculation be 1 also. What would happen if you change the value of A to 15?
The OR operator works in a similar fashion, but using the following table. If either the left side or right has a value of 1, the result will be 1:
0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1
The NOT operator works using the following table. This operator is a unary operator and only requires a single right-side value:
NOT 0 = 1
NOT 1 = 0
BITWISE OPERATORS
Bitwise operators, unlike boolean operators work on all the bits of the specified variable or value. There are six bitwise operators as follows:
BITWISE LEFT SHIFT signified by the symbol << will shift all bits one space to the left. %0111 << 1 becomes %1110.
BITWISE RIGHT SHIFT signified by the symbol >> will shift all bits one space to the right. %0111 >> 1 becomes %0011.
BITWISE AND signified by the symbol && will AND all bits of one value with another. %1111 && %0011 becomes %0011.
BITWISE OR signified by the symbol || will OR all bits of one value with another. %1111 || %0011 becomes %1111.
BITWISE XOR signified by the symbol ~~ will XOR all bits of one value with another. %1111 ~~ %0011 becomes %1100.
BITWISE NOT signified by the symbol .. will NOT all bits of the right value. %1111 .. %1010 becomes %0101.
You will discover how useful these operators become when writing conditions for your programs. Being able to write conditions with multiple parts will become increasingly important as you begin to write more complex programs.