Previous Topic Next topic Print topic


Bitwise Operators

Four binary bitwise operators and one unary bitwise operator can be used in arithmetic expressions. They are represented by reserved words that must be preceded by a space and followed by a space.

Binary Bitwise Operators Meaning
B-AND The effect of performing a logical AND between the two data items
B-OR The effect of performing a logical OR between the two data items
B-XOR The effect of performing a logical XOR (exclusive OR) between the two data items
B-EXOR The effect of performing a logical XOR (exclusive OR) between the two data items
Unary Bitwise Operator Meaning
B-NOT The effect of performing a logical NOT on the data item

B-XOR and B-EXOR are equivalent.

These operators can only be used in any arithmetic expressions with COMP-5, COMP-X or numeric literal operands. B-NOT takes one operand. The rest take two operands. The result is as if the equivalent logical operator library routine were used on the operands and the result returned in a temporary data item the size of the largest item used. If the items differ in size then the smaller item is moved to a temporary data item of the same size as the larger item. If the operands are of mixed type (COMP-5, COMP-X) then the smaller operand is moved to a temporary item of the same type as the larger. For best performance use consistent data types.

Here is an example program:

data division.
working-storage section.
01 n1 pic 9(9).
 01 n2 pic xx comp-5.
 01 n3 pic xxxx comp-5.
 procedure division.
     move 2 to n2
     move 4 to n3
     compute n1 = n2 b-or n1        *> n1 = 6
     if n2 b-and n1 = n2
         display 'true'
     end-if
     compute n1 = b-not n2          *> n1 = 65533
     compute n1 = n2 b-xor n3 + 1   *> n1 = 7
     compute n1 = n2 b-and 2        *> n1 = 2
Previous Topic Next topic Print topic