ABAP整型类型的几种位操作 Usage of ZCL_INTEGER Get instance of ZCL_INTEGER Get integer’s binary representation Bitwise operation ZBIT domain Data element for BIT type Further reading

For training purpose I need to explain to my ABAP team colleagues about how bitwise operation on Integer in Java like below is done.

ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading

And since the bitwise operation in ABAP can only support data type X and XSTRING,

ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading

so now I create a prototype which can support bitwise operation OR, AND, XOR on int4 in ABAP for teaching purpose.

Still remember the wrapper class Integer in Java which is explained in my blog Integer in ABAP, Java and JavaScript?

I just create a wrapper class ZCL_INTEGER in ABAP by following the design idea used in Java.

Here below the usage of this wrapper class is explained.

Get instance of ZCL_INTEGER

In Java you use the following code to get an instance of wrapper class Integer:

Integer a = Integer.valueOf(1);
In ABAP:

data(int1) = zcl_integer=>value_of( 3 ).
data(inta) = zcl_integer=>value_of( 3 ).
data(int2) = zcl_integer=>value_of( 2 ).

ASSERT int1 = inta.

The same primitive int value will always lead to exactly the same instance of ZCL_INTEGER. I achieve this by using an internal table MT_CACHE in ZCL_INTEGER as a cache.

Get integer’s binary representation

A string for the integer’s binary format is returned by simply calling method get_binary_format.

data(int1) = zcl_integer=>value_of( 3 ).
data(int2) = zcl_integer=>value_of( 2 ).
WRITE:/ int1->get_binary_format( ).
WRITE:/ int2->get_binary_format( ).

Result:

ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading

Bitwise operation

Use instance method OR, AND and XOR accordingly.
Test code:

data(int1) = zcl_integer=>value_of( 3 ).
data(int2) = zcl_integer=>value_of( 2 ).

data(int3) = zcl_integer=>value_of( 12 ).
data(int4) = zcl_integer=>value_of( 4 ).

data(int5) = zcl_integer=>value_of( 24 ).
data(int6) = zcl_integer=>value_of( 4 ).

WRITE:/ int1->or( int2 )->get_raw_value( ).
WRITE:/ int3->and( int4 )->get_raw_value( ).
WRITE:/ int5->xor( int6 )->get_raw_value( ).

Same output as Java:

ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading

Now let’s try to do more interesting task: swap the two integer without using intermediate variable

ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading

DATA(a) = zcl_integer=>value_of( 20 ).
DATA(b) = zcl_integer=>value_of( 30 ).

a = a->xor( b ).
b = a->xor( b ).
a = a->xor( b ).

WRITE:/ a->get_raw_value( ).
WRITE:/ b->get_raw_value( ).

Test output, swap is done

ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading
ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading

Currently this toy only supports unsigned integer.

Please manually create these three simple DDIC objects before you activate the code:

ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading

ZBIT domain

ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading
ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading

Data element for BIT type

ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading

And table type:

ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading

Further reading

I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":
ABAP整型类型的几种位操作
Usage of ZCL_INTEGER
Get instance of ZCL_INTEGER
Get integer’s binary representation
Bitwise operation
ZBIT domain
Data element for BIT type
Further reading