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: 0110Binary 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: 0100Binary format of 3: 0011
The biggest-sub-bits-series: 0100
0100 & 0100 = 0100 which <> 00011 & 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 <
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2714995/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 超大整數的加減乘除計算方法
- JS 加減乘除 尤其是減法精度問題JS
- C++ - 比較兩個浮點數大小C++
- js中兩個日期大小比較,獲取當前日期,日期加減一天JS
- 使用純粹的ABAP位操作實現兩個整數相加
- 三個數字的加減乘除模運算
- BigDecimal加減乘除運算,保留2位小數點,初始化,與0的比較Decimal
- 版本號比較大小問題
- 數學趣題:比較大小(二)
- shell加減乘除運算
- [每日一題] 第六題:不用加減乘除做加法每日一題
- JavaScript浮點數加減乘除精確計算JavaScript
- 浮點數的加減乘除運算細節
- 79 不用加減乘除做加法
- Verilog實現加減乘除運算
- MongoDB 中的【加減乘除】運算MongoDB
- JS加減乘除位移方法封裝JS封裝
- 簡單的加減乘除(遞迴)遞迴
- 位運算實現加減乘除
- 面試問題 - 只用位操作在ABAP裡實現a+b面試
- Python 解惑:整數比較Python
- 不用做任何比較判斷運算子找出兩個整數中的較大的值
- FPGA中加減乘除運算的注意問題FPGA
- 大數模擬 加減乘除 判斷大數是否為素數 板子
- C++筆記:輸入輸出、變數、變數加減乘除C++筆記變數
- JavaScript中任意兩個數加減的解決方案JavaScript
- 高精度四件套(加減乘除)
- 每日一練(34):不用加減乘除做加法
- 面試中一個暴露能力等級的問題面試
- js精確比較浮點數大小JS
- 7-2 算術入門之加減乘除
- 二進位制運算加減乘除+快速冪
- 【劍指offer】65. 不用加減乘除做加法
- 面試題-JavaScript交換兩個變數的方法面試題JavaScript變數
- 比較兩個table是否相同
- JavaScript比較兩個時間JavaScript
- 面試題:一個整數,它加上100後是一個完全...面試題
- java比較日期大小Java