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


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

 

Since C++ as well as DM is designed to allow full access to the computer’s hardware, it has the ability to operate directly upon the bits within a byte or word. Therefore, it contains bitwise operators. The bitwise operations, as one type of operators, refer to the testing, setting, or shifting of the actual bits in a byte or word, which corresponds to C++’s character and integer types. The bitwise operators can be replaced by bit-fields in C++. Table 1119a lists the bitwise operators in DM as well as in C++.

Table 1119a. Bitwise operators in DM as well as in C++.

Bitwise operators Bitwise operators Note
& AND  
| OR  
^ exclusive OR (XOR)  
~ one’s complement (NOT)  
>> shift right DM does not have Shift Operators
<< shift left

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

Table 1119b. 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

AND (&) has the most common usage in programming, you can think of the bitwise AND as a way to turn bits off, namely, any bit that is 0 in either operand will cause the corresponding bit in the outcome to be set to 0. The bitwise OR (|), as the reverse of AND, can be used to turn bits on, namely, a bit that is set to 1 in either operand will cause the corresponding bit in the result to be set to 1. As shown below, an exclusive OR (XOR) will set a bit if and only if the bits being compared are different. The NOT (1’s complement unary operator) reverses the state of all the bits of its operand.

C++ does not contain a built-in exclusive-OR (XOR) logical operator, but it can be easily constructed as shown in Table 1119c. 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 1119c. 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

As Table 1119c indicates, the outcome of an XOR is true only if exactly one of the operands is true; otherwise it is false.

The shift operators, >> and <<, move all bits in a value to the right or left. Note that DM does not have Shift Operators. Each left-shift operation causes all bits within the specified value to shift left one position, and it brings in a zero bit on the right. Each right-shift operation shifts all bits to the right one position, and brings in a zero on the left. However, if the value is a signed integer containing a negative value, each right-shift brings in a 1 on the left, which preserves the sign bit. The bits shifted off of one end do not come back around to the other. In general, the shift operators work only with integral types, such as characters, integers, and long integers. Bit shift operations can be very useful when decoding external device input, like D/A converters, and processing status information. The bitwise shift operators can be used to perform very fast multiplication and division of integers as well. A shift left will, in fact, multiply a number by 2, and a shift right will divide it by 2.

 

 

 

 

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