使用純粹的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的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2715434/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ABAP面試問題 - 不使用加減乘除等操作比較兩個整數大小面試
- 漫畫:如何實現大整數相加?
- ZOJ Martian Addition (20進位制的兩個大數相加)
- 0~9十個數,每個數只能使用一次,組成兩個三位數相加和為四位數的演算法演算法
- 【原】兩個時間相加的運算子過載實現
- 阿里開發者招聘節 | 面試題14:如何實現兩金額資料相加(最多小數點兩位)阿里面試題
- 計算位數最高達300位的兩個非負整數的乘積,C語言程式設計實現C語言程式設計
- 【附加題1】通過程式碼實現兩個超大的數字相加的結果?
- 2. 兩數相加
- 簡單純粹
- 輸入一個整數,返回這個整數的位數
- 使用純粹的JS構建 Web ComponentJSWeb
- 題目:2.兩數相加 解題思路及Java實現Java
- LeetCode——兩數相加LeetCode
- Leetcode兩數相加LeetCode
- 演算法-兩數相加演算法
- 面試問題 - 只用位操作在ABAP裡實現a+b面試
- Java 兩個整數相除保留兩位小數,將小數轉化為百分數Java
- 求兩個正整數的最大公約數與最小公倍數--C#實現C#
- JavaScript實現兩個數的交換JavaScript
- javascript如何實現保留兩位小數JavaScript
- js取整並保留兩位小數的方法JS
- 兩個棧實現佇列操作佇列
- C----判斷兩個整數乘積幾進位制
- 不借助第三方變數實現兩個整數變數值的互換變數
- 程式設計題-兩數相加程式設計
- LeetCode 2——兩數相加LeetCode
- 【leetcode】【2、兩數相加】LeetCode
- 【LeetCode】2 兩數相加LeetCode
- LeetCode-兩數相加LeetCode
- 讀js純粹筆記JS筆記
- Java 太笨?純粹誹謗Java
- 讓自己純粹一點
- C++過載加號運算子實現兩個結構體的相加C++結構體
- ABAP字串操作 擷取字元長度 取位數字串字元
- 《鬼鬥》Steam: “純粹”的格鬥遊戲遊戲
- [LeetCode] Add Two Numbers 兩個數字相加LeetCode
- 如何用位運算實現整數的加減法