-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddBinary.js
87 lines (77 loc) · 1.95 KB
/
addBinary.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* School addition algorithm
*
* Input: two binary sequences representing non-negative integers a, b
* a = (ak − 1, …, a0)2,
* b = (bl − 1, …, b0)2
* of length k and l respectively, ai, bi ∈ {0, 1}
* where
* a = ∑(k − 1)(i = 0) ai2i,
* a = ∑(l − 1)(i = 0) bi2i
*
* Output: binary sequence c which represents the sum c = a + b.
*/
function addBinary(a, b) {
const c = [];
let carry = 0;
let tmp = 0;
const A = a;
A.reverse();
const B = b;
B.reverse();
for (let i = 0; i < Math.min(A.length, B.length); i++) {
if (A[i] == 0 && B[i] == 0) {
if (carry == 0) {
tmp = 0;
} else {
tmp = 1;
}
carry = 0;
} else if (A[i] == 1 && B[i] == 1) {
if (carry == 0) {
tmp = 0;
} else {
tmp = 1;
}
carry = 1;
} else {
if (carry == 0) {
tmp = 1;
carry = 0;
} else {
tmp = 0;
carry = 1;
}
}
c.push(tmp);
}
if (A.length > B.length) {
for (let i = B.length; i < A.length; i++) {
if (A[i] == 0 && carry == 1) {
c.push(1);
carry = 0;
} else if (A[i] == 1 && carry == 1) {
c.push(0);
carry = 1;
} else {
c.push(A[i]);
}
}
} else if (A.length < B.length) {
for (let i = A.length; i < B.length; i++) {
if (B[i] == 0 && carry == 1) {
c.push(1);
carry = 0;
} else if (B[i] == 1 && carry == 1) {
c.push(0);
carry = 1;
} else {
c.push(B[i]);
}
}
}
if (carry == 1) c.push(carry);
c.reverse();
return c;
}
module.exports = addBinary;