## Set Operations Using Bit Sequences

If you have a set of m elements, you can represent any subset as a sequence of bits. If the nth element of the set is present in a subset, the nth bit is set to 1, otherwise, it is set to 0. The universal set therefore contains no zeros.

In ABAP, you can represent a set of m elements using a field with type X and length of at least m/8. To include a member of the universal set in a set, use the SET BIT statement. To check which members of the universal set are included in a particular set, use the GET BIT statement. You can use bit operations for the following set operations:

 Set operation Bit operation Intersection BIT-AND Union BIT-OR Symmetrical difference BIT-XOR

You can also use the O relational operator for bit sequences (see keyword documentation) to determine whether a particular set is a subset of another.

REPORT demo_data_bit.

DATA: frankfurt(4) TYPE x,
frisco(4)    TYPE x,
intersect(4) TYPE x,
union(4)     TYPE x,
bit          TYPE i.

DATA: carrid TYPE spfli-carrid,
carrier LIKE SORTED TABLE OF carrid
WITH UNIQUE KEY table_line.

DATA wa TYPE spfli.

SELECT carrid FROM scarr INTO TABLE carrier.

SELECT carrid cityfrom FROM spfli
INTO CORRESPONDING FIELDS OF wa.

WRITE: / wa-carrid, wa-cityfrom.

READ TABLE carrier FROM wa-carrid TRANSPORTING NO FIELDS.

CASE wa-cityfrom.
WHEN 'FRANKFURT'.
SET BIT sy-tabix OF frankfurt.
WHEN 'SAN FRANCISCO'.
SET BIT sy-tabix OF frisco.
ENDCASE.

ENDSELECT.

intersect = frankfurt BIT-AND frisco.
union     = frankfurt BIT-OR  frisco.

SKIP.

WRITE 'Airlines flying from Frankfurt and San Francisco:'.
DO 32 TIMES.
GET BIT sy-index OF intersect INTO bit.
IF bit = 1.
READ TABLE carrier INDEX sy-index INTO carrid.
WRITE carrid.
ENDIF.
ENDDO.

SKIP.

WRITE 'Airlines flying from Frankfurt or San Francisco:'.
DO 32 TIMES.
GET BIT sy-index OF union INTO bit.
IF bit = 1.
READ TABLE carrier INDEX sy-index INTO carrid.
WRITE carrid.
ENDIF.
ENDDO.

This produces the following output list:

The program uses four hexadecimal fields with length 4 - FRANKFURT, FRISCO, INTERSECT, and UNION. Each of these fields can represent a set of up to 32 elements. The basic set is the set of all airlines from database table SCARR. Each bit of the corresponding bit sequences representes one airline. To provide an index, the external index table CARRIER is created and filled with the airline codes from table SCARR. It is then possible to identify an airline using the internal index of table CARRIER.

In the SELECT loop for database table SPFLI, the corresponding bit for the airline is set either in the FRANKFURT field or the FRISCO field, depending on the departure city. The line number SY-TABIX is determined using a READstatement in which no fields are transported.

The intersection and union of FRANKFURT and FRISCO are constructed using the bit operations BIT-AND and BIT-OR.

The bits in INTERSECT and UNION are read one by one and evaluated in two DOloops. For each position in the fields with the value 1, a READstatement retrieves the airline code from the table CARRIER.