一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

ABAP面試問題 - 不使用加減乘除等操作比較兩個整數(shù)大小

 汪子熙 2020-08-23

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:

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章

    午夜精品一区二区三区国产| 加勒比日本欧美在线观看| 亚洲av首页免费在线观看| 国产精品午夜福利在线观看| 五月天丁香亚洲综合网| 欧美国产极品一区二区| 午夜精品在线视频一区| 91欧美日韩国产在线观看| 亚洲一区二区精品国产av| 激情五月综五月综合网| 国产精品伦一区二区三区四季| 亚洲欧洲一区二区综合精品| 色丁香之五月婷婷开心| 国产精品日韩欧美一区二区| 中文字幕亚洲视频一区二区| 亚洲清纯一区二区三区| 开心久久综合激情五月天| 国产精品免费福利在线| 色丁香一区二区黑人巨大| 很黄很污在线免费观看| 国产成人在线一区二区三区| 欧美日韩国产另类一区二区| 欧美日韩国产福利在线观看| 欧美人妻盗摄日韩偷拍| 黄色污污在线免费观看| 麻豆精品在线一区二区三区| 丰满人妻少妇精品一区二区三区| 在线免费国产一区二区三区| 成人午夜激情在线免费观看| 亚洲香艳网久久五月婷婷| 国内精品一区二区欧美| 精品国产亚洲区久久露脸| 亚洲av专区在线观看| 国产成人精品在线播放| 千仞雪下面好爽好紧好湿全文| 熟妇人妻av中文字幕老熟妇| 黄色美女日本的美女日人| 高潮少妇高潮久久精品99| 国产又粗又猛又大爽又黄| 国产精品一区二区视频大全| 成人日韩在线播放视频|