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.

Source Code

REPORT demo_data_bit.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.

    TYPES: BEGIN OF t_spfli,
             carrid    TYPE spfli-carrid,
             cityfrom  TYPE spfli-cityfrom,
          END OF t_spfli.

    DATA: frankfurt TYPE x LENGTH 4,
          frisco    TYPE x LENGTH 4,
          intersect TYPE x LENGTH 4,
          union     TYPE x LENGTH 4,
          bit       TYPE i,
          spflitab  TYPE TABLE OF t_spfli,
          wa        TYPE t_spfli,
          carrid    TYPE t_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 TABLE spflitab.

    DATA(out) = cl_demo_output=>new(
      )->begin_section(
      'Airlines with departure cities'
      )->write_data( spflitab
      )->end_section( ).

    LOOP AT spflitab INTO wa.

      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.

    ENDLOOP.

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

    out->begin_section(
      'Airlines flying from Frankfurt and San Francisco' ).
    DO 32 TIMES.
      GET BIT sy-index OF intersect INTO bit.
      IF bit = 1.
        carrid = carrier[ sy-index ].
        out->write( |{ carrid }| ).
      ENDIF.
    ENDDO.

    out->next_section(
      'Airlines flying from Frankfurt or San Francisco' ).
    DO 32 TIMES.
      GET BIT sy-index OF union INTO bit.
      IF bit = 1.
        carrid = carrier[ sy-index ].
        out->write( |{ carrid }| ).
      ENDIF.
    ENDDO.

    out->display( ).

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

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.