# Source file src/math/bits/example_math_test.go

2  // Use of this source code is governed by a BSD-style
4
5  package bits_test
6
7  import (
8  	"fmt"
9  	"math/bits"
10  )
11
13  	// First number is 33<<32 + 12
14  	n1 := []uint32{33, 12}
15  	// Second number is 21<<32 + 23
16  	n2 := []uint32{21, 23}
17  	// Add them together without producing carry.
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  	// First number is 1<<32 + 2147483648
24  	n1 = []uint32{1, 0x80000000}
25  	// Second number is 1<<32 + 2147483648
26  	n2 = []uint32{1, 0x80000000}
27  	// Add them together producing carry.
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  	// Output:
33  	// [33 12] + [21 23] = [54 35] (carry bit was 0)
34  	// [1 2147483648] + [1 2147483648] = [3 0] (carry bit was 1)
35  }
36
38  	// First number is 33<<64 + 12
39  	n1 := []uint64{33, 12}
40  	// Second number is 21<<64 + 23
41  	n2 := []uint64{21, 23}
42  	// Add them together without producing carry.
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  	// First number is 1<<64 + 9223372036854775808
49  	n1 = []uint64{1, 0x8000000000000000}
50  	// Second number is 1<<64 + 9223372036854775808
51  	n2 = []uint64{1, 0x8000000000000000}
52  	// Add them together producing carry.
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  	// Output:
58  	// [33 12] + [21 23] = [54 35] (carry bit was 0)
59  	// [1 9223372036854775808] + [1 9223372036854775808] = [3 0] (carry bit was 1)
60  }
61
62  func ExampleSub32() {
63  	// First number is 33<<32 + 23
64  	n1 := []uint32{33, 23}
65  	// Second number is 21<<32 + 12
66  	n2 := []uint32{21, 12}
67  	// Sub them together without producing carry.
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  	// First number is 3<<32 + 2147483647
74  	n1 = []uint32{3, 0x7fffffff}
75  	// Second number is 1<<32 + 2147483648
76  	n2 = []uint32{1, 0x80000000}
77  	// Sub them together producing carry.
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  	// Output:
83  	// [33 23] - [21 12] = [12 11] (carry bit was 0)
84  	// [3 2147483647] - [1 2147483648] = [1 4294967295] (carry bit was 1)
85  }
86
87  func ExampleSub64() {
88  	// First number is 33<<64 + 23
89  	n1 := []uint64{33, 23}
90  	// Second number is 21<<64 + 12
91  	n2 := []uint64{21, 12}
92  	// Sub them together without producing carry.
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  	// First number is 3<<64 + 9223372036854775807
99  	n1 = []uint64{3, 0x7fffffffffffffff}
100  	// Second number is 1<<64 + 9223372036854775808
101  	n2 = []uint64{1, 0x8000000000000000}
102  	// Sub them together producing carry.
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  	// Output:
108  	// [33 23] - [21 12] = [12 11] (carry bit was 0)
109  	// [3 9223372036854775807] - [1 9223372036854775808] = [1 18446744073709551615] (carry bit was 1)
110  }
111
112  func ExampleMul32() {
113  	// First number is 0<<32 + 12
114  	n1 := []uint32{0, 12}
115  	// Second number is 0<<32 + 12
116  	n2 := []uint32{0, 12}
117  	// Multiply them together without producing overflow.
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  	// First number is 0<<32 + 2147483648
123  	n1 = []uint32{0, 0x80000000}
124  	// Second number is 0<<32 + 2
125  	n2 = []uint32{0, 2}
126  	// Multiply them together producing overflow.
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  	// Output:
131  	// 12 * 12 = [0 144]
132  	// 2147483648 * 2 = [1 0]
133  }
134
135  func ExampleMul64() {
136  	// First number is 0<<64 + 12
137  	n1 := []uint64{0, 12}
138  	// Second number is 0<<64 + 12
139  	n2 := []uint64{0, 12}
140  	// Multiply them together without producing overflow.
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  	// First number is 0<<64 + 9223372036854775808
146  	n1 = []uint64{0, 0x8000000000000000}
147  	// Second number is 0<<64 + 2
148  	n2 = []uint64{0, 2}
149  	// Multiply them together producing overflow.
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  	// Output:
154  	// 12 * 12 = [0 144]
155  	// 9223372036854775808 * 2 = [1 0]
156  }
157
158  func ExampleDiv32() {
159  	// First number is 0<<32 + 6
160  	n1 := []uint32{0, 6}
161  	// Second number is 0<<32 + 3
162  	n2 := []uint32{0, 3}
163  	// Divide them together.
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  	// First number is 2<<32 + 2147483648
169  	n1 = []uint32{2, 0x80000000}
170  	// Second number is 0<<32 + 2147483648
171  	n2 = []uint32{0, 0x80000000}
172  	// Divide them together.
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  	// Output:
177  	// [0 6] / 3 = [2 0]
178  	// [2 2147483648] / 2147483648 = [5 0]
179  }
180
181  func ExampleDiv64() {
182  	// First number is 0<<64 + 6
183  	n1 := []uint64{0, 6}
184  	// Second number is 0<<64 + 3
185  	n2 := []uint64{0, 3}
186  	// Divide them together.
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  	// First number is 2<<64 + 9223372036854775808
192  	n1 = []uint64{2, 0x8000000000000000}
193  	// Second number is 0<<64 + 9223372036854775808
194  	n2 = []uint64{0, 0x8000000000000000}
195  	// Divide them together.
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  	// Output:
200  	// [0 6] / 3 = [2 0]
201  	// [2 9223372036854775808] / 9223372036854775808 = [5 0]
202  }
203

View as plain text