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.
And since the bitwise operation in ABAP can only support data type X and XSTRING,
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.
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:
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:
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:
Now let’s try to do more interesting task: swap the two integer without using intermediate variable
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
Currently this toy only supports unsigned integer.
Please manually create these three simple DDIC objects before you activate the code:
ZBIT domain
Data element for BIT type
And table type:
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:
- 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 <
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 2019 ICPC 银川网络赛 H. Fight Against Monsters
- 状态压缩DP(大佬写的很好,转来看)
- 2019 ICPC 银川网络赛 F-Moving On (卡Cache)
- 树形结构--二叉树的遍历算法应用(十九)
- POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)
- 2019 ICPC 南京网络赛 F Greedy Sequence
- 补题Codeforces 1102E. Monotonic Renumeration
- 2019 ICPC 南京网络赛 H-Holy Grail
- 写代码?程序猿?你不能不懂的八大排序算法的Python实现
- Java开发编程规范:5.集合处理
- codeforce 272E Dima and Horses (假DFS)
- 网速慢?NO可能是路由器的原因?
- codeforce 272B Dima and Sequence
- 微软自家的.Net下的JavaScript引擎--- ClearScript
- HDU 1159.Common Subsequence【动态规划DP】