COMPUTE: Bit Operations

COMPUTE

Effect

The bit expressiong bitexp is calculated and the result placed in field x.
You can use the four bit operators BIT-NOT, BIT-AND, BIT-XOR and BIT-OR. The operands are linked according to the table below.
The priority is normal: BIT-NOT has priority over BIT-AND, followed by BIT-XOR, and then BIT-OR. You can use any number of parentheses.
All operands in bitexp , and the result field x, must have type X.
The ABAP keyword COMPUTE is optional.

Relationship table

x y | BIT-NOT x   x BIT-AND y   x BIT-XOR y   x BIT-OR y 
-------------------------------------------------------- 
0 0 |	1			 0			 0			 0 
0 1 |	1			 0			 1			 1 
1 0 |	0			 0			 1			 1 
1 1 |	0			 1			 0			 1

Note

If the field lengths of the operands are different, they are all changed to the length of the result field x using MOVE-X-TO -X. This means that shorter operands are filled with HEX 0 characters, and longer ones are truncated.

DATA: X1(1) TYPE X VALUE '30', 		"  0011 0000  BIT-XOR 
	X2(1) TYPE X VALUE '50'.			"  0101 0000  = 
X1 = X1 BIT-XOR X2.					 "  0110 0000 
WRITE X1.

Output: 60

DATA: X1(4) TYPE X, 
	X2(1) TYPE X VALUE '15', 
	X3(2) TYPE X VALUE 'F3C0', 
	X4(3) TYPE X VALUE '806021'. 
X1 = X2 BIT-AND BIT-NOT ( X3 BIT-OR X4 ). 
		"'F3C0' BIT-OR  '806021'   = 'F3E02100' 
		"	 BIT-NOT 'F3E02100' = '0C1FDEFF' 
		"'15'   BIT-AND '0C1FDEFF' = '04000000' 
WRITE X1.

Output: 04000000

You can implement sets very efficiently using bit sequences. If the basic set contains n elements, you can use X field with length ceil(n/8) (for information about "ceil", see COMPUTE )). If the i-th element of the basic set is contained in a set, the i-th bit in the associated X field is set to 1. Otherwise, it is set to 0. ( SET BIT). Using GET BIT , you can check whether an element belongs to a set.
You can implement the usual set operations as follows:

Bit operation, set operation
BIT-AND Intersection
BIT-OR Union
BIT-XOR Symmetrical difference

The system load caused by evaluating bit expressions depends on the length of the operands.

Related

SET BIT, GET BIT