ABAP整型类型的几种位操作 - OR, AND, XOR

简介: ABAP整型类型的几种位操作 - OR, AND, XOR

For training purpose I need to explain to my ABAP team colleagues about how bitwise operation on Integer in Java like below is done.image.pngAnd since the bitwise operation in ABAP can only support data type X and XSTRING,image.pngso 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.


Usage of ZCL_INTEGER

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:


image.pngdata(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.


zcl_integer=>value_of( 3 ).

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

WRITE:/ int1->get_binary_format( ).

WRITE:/ int2->get_binary_format( ).


Result:image.png

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:image.pngNow let’s try to do more interesting task: swap the two integer without using intermediate variable

image.png

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 doneimage.pngimage.pngCurrently this toy only supports unsigned integer.

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

image.png

ZBIT domainimage.pngData element for BIT type

image.pngAnd table type:

image.pngFurther 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:


Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP

Functional programming – Simulate Curry in ABAP

Functional Programming – Try Reduce in JavaScript and in ABAP

Simulate Mockito in ABAP

A simulation of Java Spring dependency injection annotation @Inject in ABAP

Singleton bypass – ABAP and Java

Weak reference in ABAP and Java

Fibonacci Sequence in ES5, ES6 and ABAP

Java byte code and ABAP Load

How to write a correct program rejected by compiler: Exception handling in Java and in ABAP

An small example to learn Garbage collection in Java and in ABAP

String Template in ABAP, ES6, Angular and React

Try to access static private attribute via ABAP RTTI and Java Reflection

Local class in ABAP, Java and JavaScript

Integer in ABAP, Java and JavaScript

Covariance in Java and simulation in ABAP

Various Proxy Design Pattern implementation variants in Java and ABAP

Tag(Marker) Interface in ABAP and Java

Bitwise operation ( OR, AND, XOR ) on ABAP Integer

An interview question: Compare two integers without +,-,*,/ or > and <


相关文章
|
5月前
|
存储 SQL 数据库
工作实战:SAP ABAP 动态创建类型在实际工作中的一个应用场合分享试读版
工作实战:SAP ABAP 动态创建类型在实际工作中的一个应用场合分享试读版
工作实战:SAP ABAP 动态创建类型在实际工作中的一个应用场合分享试读版
|
BI 数据库
通过一段简单的代码,介绍 ABAP 的预定义类型 c
通过一段简单的代码,介绍 ABAP 的预定义类型 c
SAP ABAP OData 服务里需要指定 guid 类型的请求参数时,正确语法是什么?
SAP ABAP OData 服务里需要指定 guid 类型的请求参数时,正确语法是什么?
|
5月前
|
存储 SQL 数据库
128. 工作实战:SAP ABAP 动态创建类型在实际工作中的一个应用场合分享
128. 工作实战:SAP ABAP 动态创建类型在实际工作中的一个应用场合分享
ABAP 数据字典里数据元素(Data Element),结构(Structure)和表类型(Table Type)三者的使用辨析试读版
ABAP 数据字典里数据元素(Data Element),结构(Structure)和表类型(Table Type)三者的使用辨析试读版
|
12月前
关于 SAP ABAP 里的锁类型 Lock Type
关于 SAP ABAP 里的锁类型 Lock Type
|
12月前
|
数据库
SAP ABAP 系统支持的锁操作类型和各自使用场景的详细讲解试读版
SAP ABAP 系统支持的锁操作类型和各自使用场景的详细讲解试读版
|
12月前
|
存储 移动开发
什么是 SAP ABAP 系统中类型为 Relocations 的传输请求
什么是 SAP ABAP 系统中类型为 Relocations 的传输请求
SAP SEGW 事物码里的 ABAP 类型和 EDM 类型映射的一个具体例子
SAP SEGW 事物码里的 ABAP 类型和 EDM 类型映射的一个具体例子
|
存储 自然语言处理 BI
SAP ABAP——数据类型(三)【TYPE-POOL和INCLUDE嵌套定义类型】
本文主要介绍一下SAP ABAP中的INCLUDE嵌套定义和类型组TYPE-POOL,类型组讲解主要包括了TYPE-POOL简介,TYPE-POOL的创建和使用以及常用TYPE-POOL的介绍
926 0
SAP ABAP——数据类型(三)【TYPE-POOL和INCLUDE嵌套定义类型】
下一篇
无影云桌面