怎样比较两个整数(或字符串)的大小。_ABAP面试问题 - 不使用加减乘除等操作比较两个整数大小...

80492e759bba42e93011c4945a8cc3e7.png

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

Solution in Java

public 

Output:

16b41561a6148d4a27fb93b185de9986.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

Source code:

METHOD 

Test code:

WRITE

Test output:

855e1937ecae9e32580cf1f314039fde.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值