Source file
src/math/bits/example_math_test.go
1
2
3
4
5 package bits_test
6
7 import (
8 "fmt"
9 "math/bits"
10 )
11
12 func ExampleAdd32() {
13
14 n1 := []uint32{33, 12}
15
16 n2 := []uint32{21, 23}
17
18 d1, carry := bits.Add32(n1[1], n2[1], 0)
19 d0, _ := bits.Add32(n1[0], n2[0], carry)
20 nsum := []uint32{d0, d1}
21 fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
22
23
24 n1 = []uint32{1, 0x80000000}
25
26 n2 = []uint32{1, 0x80000000}
27
28 d1, carry = bits.Add32(n1[1], n2[1], 0)
29 d0, _ = bits.Add32(n1[0], n2[0], carry)
30 nsum = []uint32{d0, d1}
31 fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
32
33
34
35 }
36
37 func ExampleAdd64() {
38
39 n1 := []uint64{33, 12}
40
41 n2 := []uint64{21, 23}
42
43 d1, carry := bits.Add64(n1[1], n2[1], 0)
44 d0, _ := bits.Add64(n1[0], n2[0], carry)
45 nsum := []uint64{d0, d1}
46 fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
47
48
49 n1 = []uint64{1, 0x8000000000000000}
50
51 n2 = []uint64{1, 0x8000000000000000}
52
53 d1, carry = bits.Add64(n1[1], n2[1], 0)
54 d0, _ = bits.Add64(n1[0], n2[0], carry)
55 nsum = []uint64{d0, d1}
56 fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
57
58
59
60 }
61
62 func ExampleSub32() {
63
64 n1 := []uint32{33, 23}
65
66 n2 := []uint32{21, 12}
67
68 d1, carry := bits.Sub32(n1[1], n2[1], 0)
69 d0, _ := bits.Sub32(n1[0], n2[0], carry)
70 nsum := []uint32{d0, d1}
71 fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
72
73
74 n1 = []uint32{3, 0x7fffffff}
75
76 n2 = []uint32{1, 0x80000000}
77
78 d1, carry = bits.Sub32(n1[1], n2[1], 0)
79 d0, _ = bits.Sub32(n1[0], n2[0], carry)
80 nsum = []uint32{d0, d1}
81 fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
82
83
84
85 }
86
87 func ExampleSub64() {
88
89 n1 := []uint64{33, 23}
90
91 n2 := []uint64{21, 12}
92
93 d1, carry := bits.Sub64(n1[1], n2[1], 0)
94 d0, _ := bits.Sub64(n1[0], n2[0], carry)
95 nsum := []uint64{d0, d1}
96 fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
97
98
99 n1 = []uint64{3, 0x7fffffffffffffff}
100
101 n2 = []uint64{1, 0x8000000000000000}
102
103 d1, carry = bits.Sub64(n1[1], n2[1], 0)
104 d0, _ = bits.Sub64(n1[0], n2[0], carry)
105 nsum = []uint64{d0, d1}
106 fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
107
108
109
110 }
111
112 func ExampleMul32() {
113
114 n1 := []uint32{0, 12}
115
116 n2 := []uint32{0, 12}
117
118 hi, lo := bits.Mul32(n1[1], n2[1])
119 nsum := []uint32{hi, lo}
120 fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
121
122
123 n1 = []uint32{0, 0x80000000}
124
125 n2 = []uint32{0, 2}
126
127 hi, lo = bits.Mul32(n1[1], n2[1])
128 nsum = []uint32{hi, lo}
129 fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
130
131
132
133 }
134
135 func ExampleMul64() {
136
137 n1 := []uint64{0, 12}
138
139 n2 := []uint64{0, 12}
140
141 hi, lo := bits.Mul64(n1[1], n2[1])
142 nsum := []uint64{hi, lo}
143 fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
144
145
146 n1 = []uint64{0, 0x8000000000000000}
147
148 n2 = []uint64{0, 2}
149
150 hi, lo = bits.Mul64(n1[1], n2[1])
151 nsum = []uint64{hi, lo}
152 fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
153
154
155
156 }
157
158 func ExampleDiv32() {
159
160 n1 := []uint32{0, 6}
161
162 n2 := []uint32{0, 3}
163
164 quo, rem := bits.Div32(n1[0], n1[1], n2[1])
165 nsum := []uint32{quo, rem}
166 fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)
167
168
169 n1 = []uint32{2, 0x80000000}
170
171 n2 = []uint32{0, 0x80000000}
172
173 quo, rem = bits.Div32(n1[0], n1[1], n2[1])
174 nsum = []uint32{quo, rem}
175 fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)
176
177
178
179 }
180
181 func ExampleDiv64() {
182
183 n1 := []uint64{0, 6}
184
185 n2 := []uint64{0, 3}
186
187 quo, rem := bits.Div64(n1[0], n1[1], n2[1])
188 nsum := []uint64{quo, rem}
189 fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)
190
191
192 n1 = []uint64{2, 0x8000000000000000}
193
194 n2 = []uint64{0, 0x8000000000000000}
195
196 quo, rem = bits.Div64(n1[0], n1[1], n2[1])
197 nsum = []uint64{quo, rem}
198 fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)
199
200
201
202 }
203
View as plain text