[JS]如何不使用+來實現加法功能

張庭瑋
1 min readApr 14, 2019

首先先設定相加的兩數m,n

function(m,n){

此時設定兩個變數並給予初始值

let a=m,b=0;

while(a!=0){

把進位的結果儲存在a(&為交集:當兩方皆為1的時候為1 ex:011&101 =001)

a=(m&n)>>1;

把不用進位的結果儲存在b(^為互斥:當1方為1、1方為0的時候為1 ex:011^101 =110)

b=m^n;

重複運算至a=0

m=a;

n=b;

}

此時b即為結果

return b

}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response