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 how bit operators are used.
Other versions: 7.31 | 7.40 | 7.54
Source Code
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( ).
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. Here, the basic set is
supposed to be the set of all possible airlines from the database table SCARR.
Each bit of the relevant bit sequences represents 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 city, the SELECT
loop across the database table
SPFLI sets the bit at the position that matches the row number of the airline
in the table carrier
, in either the frankfurt
field or the frisco
field. For this purpose, the row number sy-tabix
is declared using a READ
statement, in which no other fields are transported.
Using the bit operations BIT-AND
and BIT-OR
, the intersection and union of frankfurt
and frisco
are created.
In the two DO
loops, the bits from intersect
and
union
are read and evaluated individually. For every set position, the airline
abbreviations are read from the table carr
using a READ
statement.