![80492e759bba42e93011c4945a8cc3e7.png](https://i-blog.csdnimg.cn/blog_migrate/d2f95bd11d4e7016024c6115c3af4fb5.jpeg)
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
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
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
So we can know 0110 > 0101. Another example – compare 4 and 3
Binary
The biggest-sub-bits-series: 0100
0100
So 0100 > 0011.
Solution in JavaScript
function
Output:
![72d1bb39d5241cd75001e98d0c195f2c.png](https://i-blog.csdnimg.cn/blog_migrate/113efa47bde1ab67443bc93dabeba7b4.png)
Solution in Java
public
Output:
![16b41561a6148d4a27fb93b185de9986.png](https://i-blog.csdnimg.cn/blog_migrate/d642911993cf8d359517a9d341094e98.png)
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:
![4855d42a4e7ab88403478defa2b594e3.png](https://i-blog.csdnimg.cn/blog_migrate/6deb1e64d9472593864a217046d58417.jpeg)
Source code:
METHOD
Test code:
WRITE
Test output:
![855e1937ecae9e32580cf1f314039fde.png](https://i-blog.csdnimg.cn/blog_migrate/b8d39294b95c3dbb56b639ee83325cc9.png)
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的原创文章,请关注公众号"汪子熙":
![a17d64ce674127507474cb937201c258.png](https://i-blog.csdnimg.cn/blog_migrate/29c94386e420b7925dad6fb3fb077ea1.jpeg)