日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用纯粹的ABAP位操作实现两个整数相加

發布時間:2023/12/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用纯粹的ABAP位操作实现两个整数相加 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Recently I came across this very funny picture and I would like to share with you.

This picture shows totally five different approaches to implement “a + b”, and how your brain reacts when you read these code

From this picture we know that for most guys, they will be crazy when they read the source code of the last solution as the code is really difficult to understand compared with all other four solutions.

The idea of the last solution is to achieve the add operation via bitwise operation &, | and ^. See detail of these three operations via wikipedia.
You might be interested with how ABAP can play around with these bitwise operation from two of my blogs:

  • Bitwise operation ( OR, AND, XOR ) on ABAP Integer
  • An interview question: Compare two integers without +,-,*,/ or > and <

Back to the last solution to implement a + b using bitwise operation, the code actually conveys the idea how the Adder is designed in electronics. See detail design in Wikipedia.

The idea is to use & to determine whether there is a carry about the add result of current bit, and use | to store the add result of current bit.

For example, how 2 + 3 = 5 works via bitwise operation:

And here below is my implementation using ABAP( Only integers which >= 0 are supported ).

REPORT zint.PARAMETERS: a TYPE int4 OBLIGATORY DEFAULT 100,b TYPE int4 OBLIGATORY DEFAULT 100.DATA: threshold TYPE int4.FORM add USING a TYPE int4 b TYPE int4 CHANGING cv_result TYPE int4.DATA: n TYPE int4 VALUE 0,c TYPE int4 VALUE 0.DATA: i TYPE int4 VALUE 1.DATA: boolean_a TYPE abap_bool,boolean_b TYPE abap_bool,_a TYPE int4,_b TYPE int4,aa TYPE int4,bb TYPE int4.DATA(wrapper_one) = zcl_integer=>value_of( 1 ).DATA(wrapper_c) = zcl_integer=>value_of( c ).aa = a.bb = b.WHILE i < threshold.DATA(wrapper_a) = zcl_integer=>value_of( aa ).DATA(wrapper_b) = zcl_integer=>value_of( bb ).boolean_a = boolc( wrapper_a->and( wrapper_one )->get_raw_value( ) EQ 1 ).boolean_b = boolc( wrapper_b->and( wrapper_one )->get_raw_value( ) EQ 1 )._a = COND int4( WHEN boolean_a EQ abap_true THEN 1 ELSE 0 )._b = COND int4( WHEN boolean_b EQ abap_true THEN 1 ELSE 0 ).wrapper_a = zcl_integer=>value_of( _a ).wrapper_b = zcl_integer=>value_of( _b ).wrapper_c = zcl_integer=>value_of( c ).DATA(_n_wrapper) = wrapper_a->xor( wrapper_b )->xor( wrapper_c ).DATA(b_or_c) = wrapper_b->or( wrapper_c ).DATA(b_and_c) = wrapper_b->and( wrapper_c ).DATA(_c_wrapper) = wrapper_a->and( b_or_c )->or( b_and_c ).c = _c_wrapper->get_raw_value( ).DATA(_n_i0_wrapper) = zcl_integer=>value_of( COND int4( WHEN _n_wrapper->get_raw_value( ) > 0 THEN i ELSE 0 ) ).DATA(wrapper_n) = zcl_integer=>value_of( n ).n = wrapper_n->or( _n_i0_wrapper )->get_raw_value( ).wrapper_a = zcl_integer=>value_of( aa ).aa = wrapper_a->shift_right( )->get_raw_value( ).wrapper_b = zcl_integer=>value_of( bb ).bb = wrapper_b->shift_right( )->get_raw_value( ).cv_result = n.DATA(wrapper_i) = zcl_integer=>value_of( i ).wrapper_i->shift_left( ).i = wrapper_i->get_raw_value( ).ENDWHILE.ENDFORM.START-OF-SELECTION.DATA: i TYPE int4.threshold = ipow( base = 2 exp = 30 ).PERFORM add USING a b CHANGING i.WRITE: / i.

This solution might not make too much sense from business perspective but it at least help me refresh what I have learned in my university.

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

總結

以上是生活随笔為你收集整理的使用纯粹的ABAP位操作实现两个整数相加的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。