Entering content frameSet Operations Using Bit Sequences Locate the document in its SAP Library structure

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 n th 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 operator when comparing bit sequences to determine whether a particular set is a subset of another.

Example

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:

This graphic is explained in the accompanying text

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 READ statement 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 DO loops. For each position in the fields with the value 1, a READ statement retrieves the airline code from the table CARRIER.

 

 

 

Leaving content frame