-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-binary.go
56 lines (42 loc) · 1.14 KB
/
add-binary.go
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
package main
import "strings"
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func addBinary(a string, b string) string {
result := make([]rune, max(len(a), len(b)) + 1)
carry := byte('0') // 0x30
for i,j,k := len(a)-1,len(b)-1,len(result)-1; i >=0 || j >=0 ; i,j,k = i-1,j-1,k-1 {
x := byte('0')
y := byte('0')
if i >= 0 {
x = a[i]
}
if j >= 0 {
y = b[j]
}
bytes := x + y + carry
// 0x30 + 0x30 + 0x30 = 0x90 => carry = 0, val = 0
// 0x30 + 0x31 + 0x30 = 0x91 => carry = 0, val = 1
// 0x31 + 0x31 + 0x30 = 0x92 => carry = 1, val = 0
// 0x31 + 0x31 + 0x31 = 0x93 => carry = 1, val = 1
carry = byte('0')
if bytes > 0x91 {
carry = byte('1')
}
val := '0'
if bytes == 0x91 || bytes == 0x93 {
val = '1'
}
result[k] = val
}
if carry == 0x31 {
result[0] = '1'
} else {
result = result[1:]
}
return string(result)
}