Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Character String and Byte String Processing →  Expressions and Functions for Byte String Processing →  bit_exp - Bit Expressions 

Set Operations with Bit Sequences

This example demonstrates the use of bit operators.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: frankfurt TYPE x LENGTH 4,
          frisco    TYPE x LENGTH 4,
          intersect TYPE x LENGTH 4,
          union     TYPE x LENGTH 4,
          bit       TYPE i,
          wa        TYPE spfli,
          carrid    TYPE spfli-carrid,
          carrier   LIKE SORTED TABLE OF carrid
                               WITH UNIQUE KEY table_line.

    SELECT carrid FROM scarr INTO TABLE carrier.

    SELECT carrid cityfrom FROM spfli
                           INTO CORRESPONDING FIELDS OF wa.

      WRITE: / wa-carrid, wa-cityfrom.


      CASE wa-cityfrom.
          SET BIT sy-tabix OF frankfurt.
          SET BIT sy-tabix OF frisco.


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


    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.


    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.


The method main works with four hexadecimal fields of length 4, namely frankfurt, frisco, intersect, and union. These fields can represent sets with a maximum of 32 elements. The basic set should be the set of all possible airlines from the database table SCARR. Each bit of the relevant bit sequence should represent an airline. To index the airlines, an internal table carrier is created and filled with the abbreviations for the airlines from SCARR. An airline is identified using the internal index of the table carrier.

Depending on the departure location, the SELECT loop that loops database table SPFLI sets the bit in either the frankfurt field or the frisco field. It sets it at the position that corresponds to the row number of the ariline in table carrier. For this purpose, the row number sy-tabix is determined using a READ statement, during which no other fields are transported.

Using the bit operations BIT-AND and BIT-OR, the intersection and union of frankfurt and frisco are formed.

In two DO loops, the bits from intersect and union are individually read and evaluated. For every set position, the airline abbreviations are read from the table carr using a READ statement.