Electron microscopy
 
Operators in DM and C++
- Practical Electron Microscopy and Database -
- An Online Book -
Microanalysis | EM Book                                                                                   http://www.globalsino.com/EM/        


=================================================================================

 

In DM, an expression can be a constant, variable, function call, or be composed of one or more operations. An expression, composed of two or more operations, is called a compound expression. C++ and thus DM has many built-in operators. Operators tell the compiler to perform specific mathematical or logical manipulations. The operators in C++ can be categorized by:
         i) Arithmetic. Table 1136a lists the arithmetic operators allowed in C++ and DM. In C++, When / is applied to an integer or a character, any remainder will be truncated, e.g. 10/3 = 3 instead of 3.333... The modulus operation in C++ yields the remainder of an integer division. That is, the % cannot be used on type float or double. For instance, 1%2 yields the remainder 1 because 1/2 in integer division is 0.

Table 1136a. Arithmetic operators allowed in C++ and DM.

Meaning

Operator in DM
Operator in C++
Symbol Application

Addition

+

Real, complex, RGB, string +

Subtraction

-

Real, complex, RGB -

Multiplication

*

Real, complex, RGB *

Division

/

Real, complex, RGB /

Exponentiation

**

Real, complex  

Negation

-

Real, complex, RGB  
Modulus % Example %
Decrement -- -- is the same as x = x-1 --
Increment ++ ++ is the same as x = x+1;
Example
++

            The increment and decrement in DM and C++ are two operators not found in some other computer languages. In programs, both the increment and decrement operators can either precede (prefix, ++x) or follow (postfix, x++) the operand. In some cases, there is no difference whether the increment is applied as a prefix or a postfix. However, when an increment or decrement operator precedes its operand (e.g. x), the corresponding operation will be performed prior to obtaining the operand’s value for use by the rest of the expression. If the operator
follows its operand, then the operand’s value is obtained before incrementing or decrementing it (see Table 1136b). Note that, in general, increment and decrement operators are faster than "normal" math expressions. For this reason, that's How C++ Got Its Name; that is, C++ represents an incremental improvement to C.

Table 1136b. Difference between increment and decrement operators as prefix and postfix.
    Prefix Postfix
++ DM script

number x
number y

x = 10;
y = ++x;

result (y + "\n\n")

number x
number y

x = 10;
y = x++;

result (y + "\n\n")

Result from the script 11 10
-- DM script

number x
number y

x = 10;
y = --x;

result (y + "\n\n")

number x
number y

x = 10;
y = x--;

result (y + "\n\n")

Result from the script 9 10

         ii) Equality, relational and logical. Relational refers to the relationships that values can have with one another, while logical refers to the ways in which true and false values can be connected together. Since the relational operators produce true or false results, thus they often work with the logical operators. Table 1136c lists the equality, relational and logical operators allowed in C++ and DM.

Table 1136c. Equality, relational and logical operators allowed in C++ and DM.

 

Meaning

Operator in DM
Operator in C++
Symbol Application
Equality Equality == Real, complex, and RGB, string ==
Inequality != !=
Relational

Less Than

< Real number only <

Less Than or Equal

<= <=

Greater Than

> >

Greater Than or Equal

>= >=
Logical

Logical NOT

! !

Logical OR

|| ||

Logical AND

&& &&

The logical operators are used to support the basic logical operations logical AND, OR, and NOT, according to the following truth in Table 1136d.

Table 1136d. Logical operations logical AND, OR, and NOT. (1 for true and 0 for false). Script examples are inserted as well.
A
B
A AND B
A OR B
NOT A
0 0 0 0 1
0 1 0 (script) 1 (script) 1
1 1 1 1 0
1 0 0 1 0

An example of logical OR operations is DM script. The result of the program is shown in Table 1136e.  In this script, each digit performs an OR operation.    

Table 1136e. Results of DM script above (as 16-bit integer values). The rule of OR is in Table 1136d.
MyA 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1
MyB 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
MyA | MyB ([OR]) 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1

An example of logical AND operations is DM script. The result of the program is shown in Table 1136f.  In this script, each digit performs an AND operation.    

Table 1136f. Results of DM script above (as 16-bit integer values). The rule of OR is in Table 1136d.
MyA 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1
MyB 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
MyA & MyB ([AND]) 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1

An example of logical NOT operations is DM script. In this script, each digit performs an NOT operation as well.    

C++ does not contain a built-in exclusive-OR (XOR) logical operator, but it can be easily constructed as shown in Table 1136g. In this case, the XOR operation produces a true result when one, and only one, operand is true. For instance, the function of the following XOR operation is constructed by using the && and || operators in DM application with output "1":
         number a
         number b
         a = 0         
         b = 1
         result ("\n\n")
         result ((a||b) &&!(a && b))
The program above is allowed because C++ automatically converts 1 values into true and 0 into false, and versa vice.

Table 1136g. Eexclusive-OR (XOR) logical operator. Refer to the example of defining and calling XOR operation.
A
B
XOR
DM script
0 0 0 Script
0 1 1 Script
1 0 1 Script
1 1 0 Script

         iii) Bitwise operators. Such operators act on individual bits of a number in binary representation. The expressions are cast as 32-bit integer values.

Table 1136h. Bit-wise comparison operators in C++ and DM.

Meaning

Operator in DM and C++

Bit-wise OR

|

Bit-wise AND

&

Bit-wise XOR

^

Bit-wise NOT

~

As discussed above, in DM and C++, logical XOR operation can be performed by "(a||b) &&!(a && b)"; however, in DM, XOR is simplified by "^" as shown in Table 1136h (e.g. DM script).

Table 1136i. Results of a DM script of a logical XOR operation by following the rule in Table 1136g.

MyA 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1
MyB 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
MyA ^ MyB ([XOR]) 0 1 0 1 0 1 0 1 1 1 0 0 0 0 1 0

         iv) Special operators for particular tasks.

 

 

 

=================================================================================