Bitwise Operators
There are four bitwise operators in IDL: AND, NOT, OR, and XOR. For integer operands (byte, signed- and unsigned-integer, longword, and 64-bit longword data types), bitwise operators operate on each bit of the operand or operands independently.
Tip: You can use the BIT_POPULATION, ISHFT, IDL_Variable::FromBits, and IDL_Variable::ToBits functions for additional bitwise operations.
Note: If one or both of the operands are objects, the operator may be overloaded.
Operator |
Description |
Example |
AND
|
Bitwise AND
For integer, longword, and byte operands, a bitwise AND operation is performed. If the operands are scalars, it returns a scalar value. If either operand is an array, it returns an array containing one value for each element of the shortest array operand.
Before the operation, the second operand is converted to the same type as the first operand.
For operations on other types, the result is equal to the second operand if the first operand is not equal to zero or an empty string; otherwise, the result is zero or an empty string.
Note: The bitwise AND operator is not valid for heap variable operands
|
The statement
5 AND 6 = 4
is represented in binary as follows:
0101 AND 0110 = 0100
PRINT, (5 GT 2) AND (4 GT 2)
IDL Prints: 1
PRINT, (5 GT 2) AND (4 GT 5)
IDL Prints: 0
PRINT, 5 AND 7
IDL Prints: 5
PRINT, 5 AND 2
IDL Prints: 0
PRINT, 4 AND 2
IDL Prints: 0
|
NOT
|
Bitwise NOT
Returns the bitwise inverse of its scalar or array operand (returns scalar if operand is a scalar, or returns an array containing one value for each element of the operand array).
For integer, longword, and byte operands, NOT returns the complement of each bit of the operand. For floating-point operands, the result is 1.0 if the operand is zero; otherwise, the result is zero.
For pointer or object references, NOT returns 0 for null references and 1 for non-null references. It is the same as the logical not operator (~) for these types.
Use caution when using the return value from the bitwise NOT operator as an operand for the logical operators && and ||. See Using the NOT Operator for additional discussion.
Note: Not valid for string or complex operands.
|
The statement
NOT 4 = -5
is represented in binary as follows:
NOT 0100 = 1011
PRINT, NOT 1
IDL Prints:
-2
Note: Modern computers use the “2s complement” representation for negative signed integers. This means that to arrive at the decimal representation of a negative binary number (a string of binary digits with a one as the most significant bit), you must take the complement of each bit, add one, convert to decimal, and prepend a negative sign. For example, NOT 0 equals -1, NOT 1 equals -2, etc.
IF (NOT (5 GT 6)) THEN $
PRINT, 'True'
IDL Prints:
True
|
OR
|
Bitwise OR
Performs the logical “inclusive or” operation on two scalar or array operands (returning a scalar value for scalar operands, or returning an array containing one value for each element of the shortest array operand.
Before the operation, the second operand is converted to the same type as the first operand.
For integer or byte operands, a bitwise inclusive OR is performed. For floating- point operands, returns the first operand if it is non- zero, or the 2nd operand otherwise.
|
For integer operands, OR performs a bitwise inclusive “or” operation and returns the result. The statement:
3 OR 5 = 7
is represented in binary as follows:
0011 OR 0101 = 0111
IF ((5 GT 3) OR $
(4 GT 5)) THEN $
PRINT, 'True'
IDL Prints:
True
|
XOR
|
Bitwise exclusive XOR
XOR is only valid for byte, integer, and longword operands.
Performs the logical “exclusive or” operation on two scalar or array operands (returning a scalar value for scalar operands, or returning an array containing one value for each element of the shortest array operand.
A bit in the result is set to 1 if the corresponding bits in the operands are different; if they are equal, it is set to zero.
|
For integer operands, XOR sets a bit in the result to 1 if the corresponding bits in the operands are different or to 0 if they are equal. The statement:
3 XOR 5 = 6
is represented in binary as follows:
0011 XOR 0101 = 0110
IF ((5 GT 3) XOR (4 GT 5)) THEN $
PRINT, 'Different' $
ELSE PRINT, 'Same'
IDL Prints:
Different
|
Using the NOT Operator
Due to the bitwise nature of the NOT operator, logical negation operations should always use ~ in preference to NOT, reserving NOT exclusively for bitwise computations. Consider a statement such as:
IF ((NOT EOF(lun)) && device_ready) THEN statement
which wants to execute statement if the file specified by the variable lun has data remaining, and the variable device_ready is non-zero. When EOF returns the value 1, the expression NOT EOF(lun) yields ‑2, due to the bitwise nature of the NOT operator. The && operator interprets the value ‑2 as true, and will therefore attempt to execute statement incorrectly in many cases. The proper way to write the above statement is:
IF ((~ EOF(lun)) && device_ready) THEN statement
Additional Bitwise Operator Examples
Some examples of bitwise expressions are as follows:
TV, NOT IMG
ARR AND 'FF'X