ABAP面试问题 - 不使用加减乘除等操作比较两个整数大小
Our team architect has asked us this question which is said to be an interview question from Microsoft long time ago: Please implement one function which accepts two integers as input and generate the following result accordingly:
If a > b, return 1,
if a = b, return 0,
if a < b, return -1
For simplification reason here we can just consider unsigned int ( that is, all importing parameter of integers are greater than or equal to 0 ).
Inside the implementation, you are NOT allowed to use +, -, *, /, > and < for comparison. There must be multiple ways to achieve it, here below is just one among them. Even we are not allowed to use four arithmetic operations and > or <, we can still leverage the bit operation supported on Integer. The basic idea is, say we have 6 and 5 for comparison.
Binary format of 6: 0110
Binary format of 5: 0101
If we can generate the biggest-sub-bits-series which differentiate the two integers, in the example above it is 0010( since the third bit of both integer are equal ), then we can simply know which is bigger by making bit AND operation:
0110 & 0010 = 10 which <> 0.
0101 & 0010 = 0
So we can know 0110 > 0101. Another example – compare 4 and 3
Binary format of 4: 0100
Binary format of 3: 0011
The biggest-sub-bits-series: 0100
0100 & 0100 = 0100 which <> 0
0011 & 0100 = 0
So 0100 > 0011.
Solution in JavaScript
function compare(a,b){
var diff = a ^ b;
if( diff == 0)
return 0;
diff = diff | ( diff >> 1 );
diff |= diff >> 2;
diff |= diff >> 4;
diff |= diff >> 8;
diff |= diff >> 16;
diff ^= diff >> 1;
return ( a & diff )? 1:-1;
}
console.log(compare(1,2));
console.log(compare(3,2));
console.log(compare(300,2));
console.log(compare(3000,2));
console.log(compare(3000,3000));
console.log(compare(3000,3001));
Output:
Solution in Java
public static int compare(int a, int b){
int diff = a ^ b;
if( diff == 0)
return 0;
diff = diff | ( diff >> 1 );
diff |= diff >> 2;
diff |= diff >> 4;
diff |= diff >> 8;
diff |= diff >> 16;
diff ^= diff >> 1;
return ( (a & diff) == 0 ) ? -1 : 1;
}
System.out.println(compare(1,2));
System.out.println(compare(3,2));
System.out.println(compare(300,2));
System.out.println(compare(3000,2));
System.out.println(compare(3000,3000));
System.out.println(compare(3000,3001));
Output:
Solution in ABAP
Since it is not possible to directly perform bit operation on integer in ABAP, in my blog Bitwise operation ( OR, AND, XOR ) on ABAP Integer I simulate these three operations with the help of ABAP internal table. Still it is not enough, the bit shift operation like >> and << are also required to finish this exercise, so I make further enhancement, adding two new methods SHIFT_RIGHT and SHIFT_LEFT in ZCL_INTEGER, which could be found from my github.
Now all prerequisite to finish it using ABAP are fulfilled. Here it is:
Source code:
METHOD compare.
DEFINE shift_right.
lv_diff = a->get_raw_value( ).
a->shift_right( &1 ).
lo_diff = zcl_integer=>value_of( lv_diff ).
a = lo_diff->or( a ).
END-OF-DEFINITION.
DATA(a) = zcl_integer=>value_of( iv_a ).
DATA(b) = zcl_integer=>value_of( iv_b ).
DATA: lv_diff TYPE int4,
lo_diff TYPE REF TO zcl_integer.
a = a->xor( b ).
IF a->get_raw_value( ) IS INITIAL.
rv_result = 0.
RETURN.
ENDIF.
shift_right 1.
shift_right 2.
shift_right 4.
shift_right 8.
shift_right 16.
lv_diff = a->get_raw_value( ).
a->shift_right( 1 ).
lo_diff = zcl_integer=>value_of( lv_diff ).
a = lo_diff->xor( a ).
DATA(lo_origin_a) = zcl_integer=>value_of( iv_a ).
rv_result = zcl_integer=>value_of( lo_origin_a->and( a )->get_raw_value( ) )->get_raw_value( ).
rv_result = COND #( WHEN rv_result IS INITIAL THEN -1 ELSE 1 ).
ENDMETHOD.
Test code:
WRITE:/ zcl_comparator=>compare( iv_a = 1 iv_B = 2 ).
WRITE:/ zcl_comparator=>compare( iv_a = 3 iv_B = 2 ).
WRITE:/ zcl_comparator=>compare( iv_a = 300 iv_B = 2 ).
WRITE:/ zcl_comparator=>compare( iv_a = 3000 iv_B = 2 ).
WRITE:/ zcl_comparator=>compare( iv_a = 3000 iv_B = 3000 ).
WRITE:/ zcl_comparator=>compare( iv_a = 3000 iv_B = 3001 ).
Test output:
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 <
- 一个SQL语句引发的ORA-00600错误排查(一) (r9笔记第64天)
- 一个SQL语句引发的ORA-00600错误排查(二)(r9笔记第65天)
- 关于all_procedures的问题分析 (r9笔记第61天)
- 半自动化搭建Data Guard的想法和实践(一) (r9笔记第74天)
- Go语言位操作实例
- 通过Snapshot Standby来精确评估SQL性能 (r9笔记第73天)
- mongodb11天之屠龙宝刀(三)基本操作:增删改查与mysql对比
- Go语言 实现bitmap
- 最近的几个技术问题总结和答疑(八) (r9笔记第72天)
- Java基础-day05-代码题
- MySQL级联复制的同步问题(一) (r9笔记第71天)
- GO语言 TCP传输实例
- JavaScript 性能优化技巧分享
- 一个拷贝操作导致的潜在监听类问题(r9笔记第70天)
- 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 数组属性和方法
- Android 布局中的android:onClick的使用方法总结
- Android 提交或者上传数据时的dialog弹框动画效果
- Android 调用系统应用的方法总结
- Android实现网易严选标签栏滑动效果
- Android socket实现原理详解 服务端和客户端如何搭建
- Android使用ViewFlipper实现图片切换功能
- Android 图片显示与屏幕适配的问题
- Android实现截图和分享功能的代码
- Android 自定义相机及分析源码
- Android实现微博菜单弹出效果
- Android开发之拖动条和评分组件用法分析
- Android使用AsyncQueryHandler实现获取手机联系人功能
- Android用过TextView实现跑马灯效果的示例
- android如何设置Activity背景色为透明色
- Android 手机防止休眠的两种实现方法