-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path【重】不用加减乘除做加法.js
38 lines (36 loc) · 1013 Bytes
/
【重】不用加减乘除做加法.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
*/
//正确解法
function Add(num1, num2)
{
while(num2){
let res = (num1 ^ num2) & 0xffffffff;
let next = ((num1 & num2)<<1) & 0xffffffff;
num1 = res;
num2 = next;
}
return num1;
}
//解法复杂,无法进行负数相加
function Add(num1, num2)
{
let str1 = num1.toString(2).split('').reverse().join(''),
str2 = num2.toString(2).split('').reverse().join('');
let next = 0, i=0;
let sum = '';
while(i<=str1.length || i<=str2.length){
if(!str1[i]) str1[i] = 0;
if(!str2[i]) str2[i] = 0;
let temp = str1[i] ^ str2[i] ^ next;
sum = sum.concat(temp);
let a = str1[i],
b=str2[i],
c=next;
next = ((a|b)&c) | ((a|c)&b) | ((b|c)&a);
i++;
}
sum = sum.concat(next);
let res = sum.split('').reverse().join('');
return parseInt(res, 2);
}