-
Notifications
You must be signed in to change notification settings - Fork 11
/
math_bits.go
67 lines (58 loc) · 1.93 KB
/
math_bits.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
57
58
59
60
61
62
63
64
65
66
67
package template
import (
"fmt"
"strconv"
"strings"
)
func leftShift(a, b interface{}) (r interface{}, err error) {
defer func() { err = trapError(err, recover()) }()
return process(a, func(a interface{}) interface{} { return toInt64(a) << toUnsignedInteger(b) })
}
func rightShift(a, b interface{}) (r interface{}, err error) {
defer func() { err = trapError(err, recover()) }()
return process(a, func(a interface{}) interface{} { return toInt64(a) >> toUnsignedInteger(b) })
}
func bitwiseOr(a, b interface{}, rest ...interface{}) (r interface{}, err error) {
defer func() { err = trapError(err, recover()) }()
result := toInt64(a) | toInt64(b)
for i := range rest {
result = result | toInt64(rest[i])
}
return result, nil
}
func bitwiseAnd(a, b interface{}, rest ...interface{}) (r interface{}, err error) {
defer func() { err = trapError(err, recover()) }()
result := toInt64(a) & toInt64(b)
for i := range rest {
result = result & toInt64(rest[i])
}
return result, nil
}
func bitwiseXor(a, b interface{}, rest ...interface{}) (r interface{}, err error) {
defer func() { err = trapError(err, recover()) }()
result := toInt64(a) ^ toInt64(b)
for i := range rest {
result = result ^ toInt64(rest[i])
}
return result, nil
}
func bitwiseClear(a, b interface{}, rest ...interface{}) (r interface{}, err error) {
defer func() { err = trapError(err, recover()) }()
result := toInt64(a) &^ toInt64(b)
for i := range rest {
result = result &^ toInt64(rest[i])
}
return result, nil
}
func hex(a interface{}) (r interface{}, err error) {
defer func() { err = trapError(err, recover()) }()
return process(a, func(a interface{}) interface{} {
return fmt.Sprintf("0x%X", toInt(a))
})
}
func decimal(a interface{}) (r interface{}, err error) {
defer func() { err = trapError(err, recover()) }()
return process(a, func(a interface{}) interface{} {
return must(strconv.ParseInt(strings.TrimPrefix(fmt.Sprint(a), "0x"), 16, 64))
})
}