summaryrefslogtreecommitdiff
path: root/devdocs/go/math%2Fbits%2Findex.html
blob: e046143789f3d68b66d263a5ed7428f97954ceb9 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<h1> Package bits  </h1>     <ul id="short-nav">
<li><code>import "math/bits"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package bits implements bit counting and manipulation functions for the predeclared unsigned integer types. </p>
<p>Functions in this package may be implemented directly by the compiler, for better performance. For those functions the code in this package will not be used. Which functions are implemented by the compiler depends on the architecture and the Go release. </p>     <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#pkg-constants">Constants</a></li>
<li><a href="#Add">func Add(x, y, carry uint) (sum, carryOut uint)</a></li>
<li><a href="#Add32">func Add32(x, y, carry uint32) (sum, carryOut uint32)</a></li>
<li><a href="#Add64">func Add64(x, y, carry uint64) (sum, carryOut uint64)</a></li>
<li><a href="#Div">func Div(hi, lo, y uint) (quo, rem uint)</a></li>
<li><a href="#Div32">func Div32(hi, lo, y uint32) (quo, rem uint32)</a></li>
<li><a href="#Div64">func Div64(hi, lo, y uint64) (quo, rem uint64)</a></li>
<li><a href="#LeadingZeros">func LeadingZeros(x uint) int</a></li>
<li><a href="#LeadingZeros16">func LeadingZeros16(x uint16) int</a></li>
<li><a href="#LeadingZeros32">func LeadingZeros32(x uint32) int</a></li>
<li><a href="#LeadingZeros64">func LeadingZeros64(x uint64) int</a></li>
<li><a href="#LeadingZeros8">func LeadingZeros8(x uint8) int</a></li>
<li><a href="#Len">func Len(x uint) int</a></li>
<li><a href="#Len16">func Len16(x uint16) (n int)</a></li>
<li><a href="#Len32">func Len32(x uint32) (n int)</a></li>
<li><a href="#Len64">func Len64(x uint64) (n int)</a></li>
<li><a href="#Len8">func Len8(x uint8) int</a></li>
<li><a href="#Mul">func Mul(x, y uint) (hi, lo uint)</a></li>
<li><a href="#Mul32">func Mul32(x, y uint32) (hi, lo uint32)</a></li>
<li><a href="#Mul64">func Mul64(x, y uint64) (hi, lo uint64)</a></li>
<li><a href="#OnesCount">func OnesCount(x uint) int</a></li>
<li><a href="#OnesCount16">func OnesCount16(x uint16) int</a></li>
<li><a href="#OnesCount32">func OnesCount32(x uint32) int</a></li>
<li><a href="#OnesCount64">func OnesCount64(x uint64) int</a></li>
<li><a href="#OnesCount8">func OnesCount8(x uint8) int</a></li>
<li><a href="#Rem">func Rem(hi, lo, y uint) uint</a></li>
<li><a href="#Rem32">func Rem32(hi, lo, y uint32) uint32</a></li>
<li><a href="#Rem64">func Rem64(hi, lo, y uint64) uint64</a></li>
<li><a href="#Reverse">func Reverse(x uint) uint</a></li>
<li><a href="#Reverse16">func Reverse16(x uint16) uint16</a></li>
<li><a href="#Reverse32">func Reverse32(x uint32) uint32</a></li>
<li><a href="#Reverse64">func Reverse64(x uint64) uint64</a></li>
<li><a href="#Reverse8">func Reverse8(x uint8) uint8</a></li>
<li><a href="#ReverseBytes">func ReverseBytes(x uint) uint</a></li>
<li><a href="#ReverseBytes16">func ReverseBytes16(x uint16) uint16</a></li>
<li><a href="#ReverseBytes32">func ReverseBytes32(x uint32) uint32</a></li>
<li><a href="#ReverseBytes64">func ReverseBytes64(x uint64) uint64</a></li>
<li><a href="#RotateLeft">func RotateLeft(x uint, k int) uint</a></li>
<li><a href="#RotateLeft16">func RotateLeft16(x uint16, k int) uint16</a></li>
<li><a href="#RotateLeft32">func RotateLeft32(x uint32, k int) uint32</a></li>
<li><a href="#RotateLeft64">func RotateLeft64(x uint64, k int) uint64</a></li>
<li><a href="#RotateLeft8">func RotateLeft8(x uint8, k int) uint8</a></li>
<li><a href="#Sub">func Sub(x, y, borrow uint) (diff, borrowOut uint)</a></li>
<li><a href="#Sub32">func Sub32(x, y, borrow uint32) (diff, borrowOut uint32)</a></li>
<li><a href="#Sub64">func Sub64(x, y, borrow uint64) (diff, borrowOut uint64)</a></li>
<li><a href="#TrailingZeros">func TrailingZeros(x uint) int</a></li>
<li><a href="#TrailingZeros16">func TrailingZeros16(x uint16) int</a></li>
<li><a href="#TrailingZeros32">func TrailingZeros32(x uint32) int</a></li>
<li><a href="#TrailingZeros64">func TrailingZeros64(x uint64) int</a></li>
<li><a href="#TrailingZeros8">func TrailingZeros8(x uint8) int</a></li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_Add32">Add32</a></dd> <dd><a class="exampleLink" href="#example_Add64">Add64</a></dd> <dd><a class="exampleLink" href="#example_Div32">Div32</a></dd> <dd><a class="exampleLink" href="#example_Div64">Div64</a></dd> <dd><a class="exampleLink" href="#example_LeadingZeros16">LeadingZeros16</a></dd> <dd><a class="exampleLink" href="#example_LeadingZeros32">LeadingZeros32</a></dd> <dd><a class="exampleLink" href="#example_LeadingZeros64">LeadingZeros64</a></dd> <dd><a class="exampleLink" href="#example_LeadingZeros8">LeadingZeros8</a></dd> <dd><a class="exampleLink" href="#example_Len16">Len16</a></dd> <dd><a class="exampleLink" href="#example_Len32">Len32</a></dd> <dd><a class="exampleLink" href="#example_Len64">Len64</a></dd> <dd><a class="exampleLink" href="#example_Len8">Len8</a></dd> <dd><a class="exampleLink" href="#example_Mul32">Mul32</a></dd> <dd><a class="exampleLink" href="#example_Mul64">Mul64</a></dd> <dd><a class="exampleLink" href="#example_OnesCount">OnesCount</a></dd> <dd><a class="exampleLink" href="#example_OnesCount16">OnesCount16</a></dd> <dd><a class="exampleLink" href="#example_OnesCount32">OnesCount32</a></dd> <dd><a class="exampleLink" href="#example_OnesCount64">OnesCount64</a></dd> <dd><a class="exampleLink" href="#example_OnesCount8">OnesCount8</a></dd> <dd><a class="exampleLink" href="#example_Reverse16">Reverse16</a></dd> <dd><a class="exampleLink" href="#example_Reverse32">Reverse32</a></dd> <dd><a class="exampleLink" href="#example_Reverse64">Reverse64</a></dd> <dd><a class="exampleLink" href="#example_Reverse8">Reverse8</a></dd> <dd><a class="exampleLink" href="#example_ReverseBytes16">ReverseBytes16</a></dd> <dd><a class="exampleLink" href="#example_ReverseBytes32">ReverseBytes32</a></dd> <dd><a class="exampleLink" href="#example_ReverseBytes64">ReverseBytes64</a></dd> <dd><a class="exampleLink" href="#example_RotateLeft16">RotateLeft16</a></dd> <dd><a class="exampleLink" href="#example_RotateLeft32">RotateLeft32</a></dd> <dd><a class="exampleLink" href="#example_RotateLeft64">RotateLeft64</a></dd> <dd><a class="exampleLink" href="#example_RotateLeft8">RotateLeft8</a></dd> <dd><a class="exampleLink" href="#example_Sub32">Sub32</a></dd> <dd><a class="exampleLink" href="#example_Sub64">Sub64</a></dd> <dd><a class="exampleLink" href="#example_TrailingZeros16">TrailingZeros16</a></dd> <dd><a class="exampleLink" href="#example_TrailingZeros32">TrailingZeros32</a></dd> <dd><a class="exampleLink" href="#example_TrailingZeros64">TrailingZeros64</a></dd> <dd><a class="exampleLink" href="#example_TrailingZeros8">TrailingZeros8</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>bits.go</span> <span>bits_errors.go</span> <span>bits_tables.go</span>  </p>   <h2 id="pkg-constants">Constants</h2> <p>UintSize is the size of a uint in bits. </p>
<pre data-language="go">const UintSize = uintSize</pre> <h2 id="Add">func <span>Add</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Add(x, y, carry uint) (sum, carryOut uint)</pre> <p>Add returns the sum with carry of x, y and carry: sum = x + y + carry. The carry input must be 0 or 1; otherwise the behavior is undefined. The carryOut output is guaranteed to be 0 or 1. </p>
<p>This function's execution time does not depend on the inputs. </p>
<h2 id="Add32">func <span>Add32</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Add32(x, y, carry uint32) (sum, carryOut uint32)</pre> <p>Add32 returns the sum with carry of x, y and carry: sum = x + y + carry. The carry input must be 0 or 1; otherwise the behavior is undefined. The carryOut output is guaranteed to be 0 or 1. </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_Add32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// First number is 33&lt;&lt;32 + 12
n1 := []uint32{33, 12}
// Second number is 21&lt;&lt;32 + 23
n2 := []uint32{21, 23}
// Add them together without producing carry.
d1, carry := bits.Add32(n1[1], n2[1], 0)
d0, _ := bits.Add32(n1[0], n2[0], carry)
nsum := []uint32{d0, d1}
fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)

// First number is 1&lt;&lt;32 + 2147483648
n1 = []uint32{1, 0x80000000}
// Second number is 1&lt;&lt;32 + 2147483648
n2 = []uint32{1, 0x80000000}
// Add them together producing carry.
d1, carry = bits.Add32(n1[1], n2[1], 0)
d0, _ = bits.Add32(n1[0], n2[0], carry)
nsum = []uint32{d0, d1}
fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
</pre> <p>Output:</p> <pre class="output" data-language="go">[33 12] + [21 23] = [54 35] (carry bit was 0)
[1 2147483648] + [1 2147483648] = [3 0] (carry bit was 1)
</pre>   <h2 id="Add64">func <span>Add64</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Add64(x, y, carry uint64) (sum, carryOut uint64)</pre> <p>Add64 returns the sum with carry of x, y and carry: sum = x + y + carry. The carry input must be 0 or 1; otherwise the behavior is undefined. The carryOut output is guaranteed to be 0 or 1. </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_Add64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// First number is 33&lt;&lt;64 + 12
n1 := []uint64{33, 12}
// Second number is 21&lt;&lt;64 + 23
n2 := []uint64{21, 23}
// Add them together without producing carry.
d1, carry := bits.Add64(n1[1], n2[1], 0)
d0, _ := bits.Add64(n1[0], n2[0], carry)
nsum := []uint64{d0, d1}
fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)

// First number is 1&lt;&lt;64 + 9223372036854775808
n1 = []uint64{1, 0x8000000000000000}
// Second number is 1&lt;&lt;64 + 9223372036854775808
n2 = []uint64{1, 0x8000000000000000}
// Add them together producing carry.
d1, carry = bits.Add64(n1[1], n2[1], 0)
d0, _ = bits.Add64(n1[0], n2[0], carry)
nsum = []uint64{d0, d1}
fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
</pre> <p>Output:</p> <pre class="output" data-language="go">[33 12] + [21 23] = [54 35] (carry bit was 0)
[1 9223372036854775808] + [1 9223372036854775808] = [3 0] (carry bit was 1)
</pre>   <h2 id="Div">func <span>Div</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Div(hi, lo, y uint) (quo, rem uint)</pre> <p>Div returns the quotient and remainder of (hi, lo) divided by y: quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper half in parameter hi and the lower half in parameter lo. Div panics for y == 0 (division by zero) or y &lt;= hi (quotient overflow). </p>
<h2 id="Div32">func <span>Div32</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Div32(hi, lo, y uint32) (quo, rem uint32)</pre> <p>Div32 returns the quotient and remainder of (hi, lo) divided by y: quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper half in parameter hi and the lower half in parameter lo. Div32 panics for y == 0 (division by zero) or y &lt;= hi (quotient overflow). </p>   <h4 id="example_Div32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// First number is 0&lt;&lt;32 + 6
n1 := []uint32{0, 6}
// Second number is 0&lt;&lt;32 + 3
n2 := []uint32{0, 3}
// Divide them together.
quo, rem := bits.Div32(n1[0], n1[1], n2[1])
nsum := []uint32{quo, rem}
fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)

// First number is 2&lt;&lt;32 + 2147483648
n1 = []uint32{2, 0x80000000}
// Second number is 0&lt;&lt;32 + 2147483648
n2 = []uint32{0, 0x80000000}
// Divide them together.
quo, rem = bits.Div32(n1[0], n1[1], n2[1])
nsum = []uint32{quo, rem}
fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)
</pre> <p>Output:</p> <pre class="output" data-language="go">[0 6] / 3 = [2 0]
[2 2147483648] / 2147483648 = [5 0]
</pre>   <h2 id="Div64">func <span>Div64</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Div64(hi, lo, y uint64) (quo, rem uint64)</pre> <p>Div64 returns the quotient and remainder of (hi, lo) divided by y: quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper half in parameter hi and the lower half in parameter lo. Div64 panics for y == 0 (division by zero) or y &lt;= hi (quotient overflow). </p>   <h4 id="example_Div64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// First number is 0&lt;&lt;64 + 6
n1 := []uint64{0, 6}
// Second number is 0&lt;&lt;64 + 3
n2 := []uint64{0, 3}
// Divide them together.
quo, rem := bits.Div64(n1[0], n1[1], n2[1])
nsum := []uint64{quo, rem}
fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)

// First number is 2&lt;&lt;64 + 9223372036854775808
n1 = []uint64{2, 0x8000000000000000}
// Second number is 0&lt;&lt;64 + 9223372036854775808
n2 = []uint64{0, 0x8000000000000000}
// Divide them together.
quo, rem = bits.Div64(n1[0], n1[1], n2[1])
nsum = []uint64{quo, rem}
fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)
</pre> <p>Output:</p> <pre class="output" data-language="go">[0 6] / 3 = [2 0]
[2 9223372036854775808] / 9223372036854775808 = [5 0]
</pre>   <h2 id="LeadingZeros">func <span>LeadingZeros</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func LeadingZeros(x uint) int</pre> <p>LeadingZeros returns the number of leading zero bits in x; the result is <a href="#UintSize">UintSize</a> for x == 0. </p>
<h2 id="LeadingZeros16">func <span>LeadingZeros16</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func LeadingZeros16(x uint16) int</pre> <p>LeadingZeros16 returns the number of leading zero bits in x; the result is 16 for x == 0. </p>   <h4 id="example_LeadingZeros16"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("LeadingZeros16(%016b) = %d\n", 1, bits.LeadingZeros16(1))
</pre> <p>Output:</p> <pre class="output" data-language="go">LeadingZeros16(0000000000000001) = 15
</pre>   <h2 id="LeadingZeros32">func <span>LeadingZeros32</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func LeadingZeros32(x uint32) int</pre> <p>LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0. </p>   <h4 id="example_LeadingZeros32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("LeadingZeros32(%032b) = %d\n", 1, bits.LeadingZeros32(1))
</pre> <p>Output:</p> <pre class="output" data-language="go">LeadingZeros32(00000000000000000000000000000001) = 31
</pre>   <h2 id="LeadingZeros64">func <span>LeadingZeros64</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func LeadingZeros64(x uint64) int</pre> <p>LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0. </p>   <h4 id="example_LeadingZeros64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("LeadingZeros64(%064b) = %d\n", 1, bits.LeadingZeros64(1))
</pre> <p>Output:</p> <pre class="output" data-language="go">LeadingZeros64(0000000000000000000000000000000000000000000000000000000000000001) = 63
</pre>   <h2 id="LeadingZeros8">func <span>LeadingZeros8</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func LeadingZeros8(x uint8) int</pre> <p>LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for x == 0. </p>   <h4 id="example_LeadingZeros8"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("LeadingZeros8(%08b) = %d\n", 1, bits.LeadingZeros8(1))
</pre> <p>Output:</p> <pre class="output" data-language="go">LeadingZeros8(00000001) = 7
</pre>   <h2 id="Len">func <span>Len</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func Len(x uint) int</pre> <p>Len returns the minimum number of bits required to represent x; the result is 0 for x == 0. </p>
<h2 id="Len16">func <span>Len16</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func Len16(x uint16) (n int)</pre> <p>Len16 returns the minimum number of bits required to represent x; the result is 0 for x == 0. </p>   <h4 id="example_Len16"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("Len16(%016b) = %d\n", 8, bits.Len16(8))
</pre> <p>Output:</p> <pre class="output" data-language="go">Len16(0000000000001000) = 4
</pre>   <h2 id="Len32">func <span>Len32</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func Len32(x uint32) (n int)</pre> <p>Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0. </p>   <h4 id="example_Len32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("Len32(%032b) = %d\n", 8, bits.Len32(8))
</pre> <p>Output:</p> <pre class="output" data-language="go">Len32(00000000000000000000000000001000) = 4
</pre>   <h2 id="Len64">func <span>Len64</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func Len64(x uint64) (n int)</pre> <p>Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0. </p>   <h4 id="example_Len64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("Len64(%064b) = %d\n", 8, bits.Len64(8))
</pre> <p>Output:</p> <pre class="output" data-language="go">Len64(0000000000000000000000000000000000000000000000000000000000001000) = 4
</pre>   <h2 id="Len8">func <span>Len8</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func Len8(x uint8) int</pre> <p>Len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0. </p>   <h4 id="example_Len8"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("Len8(%08b) = %d\n", 8, bits.Len8(8))
</pre> <p>Output:</p> <pre class="output" data-language="go">Len8(00001000) = 4
</pre>   <h2 id="Mul">func <span>Mul</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Mul(x, y uint) (hi, lo uint)</pre> <p>Mul returns the full-width product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo. </p>
<p>This function's execution time does not depend on the inputs. </p>
<h2 id="Mul32">func <span>Mul32</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Mul32(x, y uint32) (hi, lo uint32)</pre> <p>Mul32 returns the 64-bit product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo. </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_Mul32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// First number is 0&lt;&lt;32 + 12
n1 := []uint32{0, 12}
// Second number is 0&lt;&lt;32 + 12
n2 := []uint32{0, 12}
// Multiply them together without producing overflow.
hi, lo := bits.Mul32(n1[1], n2[1])
nsum := []uint32{hi, lo}
fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)

// First number is 0&lt;&lt;32 + 2147483648
n1 = []uint32{0, 0x80000000}
// Second number is 0&lt;&lt;32 + 2
n2 = []uint32{0, 2}
// Multiply them together producing overflow.
hi, lo = bits.Mul32(n1[1], n2[1])
nsum = []uint32{hi, lo}
fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
</pre> <p>Output:</p> <pre class="output" data-language="go">12 * 12 = [0 144]
2147483648 * 2 = [1 0]
</pre>   <h2 id="Mul64">func <span>Mul64</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Mul64(x, y uint64) (hi, lo uint64)</pre> <p>Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo. </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_Mul64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// First number is 0&lt;&lt;64 + 12
n1 := []uint64{0, 12}
// Second number is 0&lt;&lt;64 + 12
n2 := []uint64{0, 12}
// Multiply them together without producing overflow.
hi, lo := bits.Mul64(n1[1], n2[1])
nsum := []uint64{hi, lo}
fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)

// First number is 0&lt;&lt;64 + 9223372036854775808
n1 = []uint64{0, 0x8000000000000000}
// Second number is 0&lt;&lt;64 + 2
n2 = []uint64{0, 2}
// Multiply them together producing overflow.
hi, lo = bits.Mul64(n1[1], n2[1])
nsum = []uint64{hi, lo}
fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
</pre> <p>Output:</p> <pre class="output" data-language="go">12 * 12 = [0 144]
9223372036854775808 * 2 = [1 0]
</pre>   <h2 id="OnesCount">func <span>OnesCount</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func OnesCount(x uint) int</pre> <p>OnesCount returns the number of one bits ("population count") in x. </p>   <h4 id="example_OnesCount"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("OnesCount(%b) = %d\n", 14, bits.OnesCount(14))
</pre> <p>Output:</p> <pre class="output" data-language="go">OnesCount(1110) = 3
</pre>   <h2 id="OnesCount16">func <span>OnesCount16</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func OnesCount16(x uint16) int</pre> <p>OnesCount16 returns the number of one bits ("population count") in x. </p>   <h4 id="example_OnesCount16"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("OnesCount16(%016b) = %d\n", 14, bits.OnesCount16(14))
</pre> <p>Output:</p> <pre class="output" data-language="go">OnesCount16(0000000000001110) = 3
</pre>   <h2 id="OnesCount32">func <span>OnesCount32</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func OnesCount32(x uint32) int</pre> <p>OnesCount32 returns the number of one bits ("population count") in x. </p>   <h4 id="example_OnesCount32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("OnesCount32(%032b) = %d\n", 14, bits.OnesCount32(14))
</pre> <p>Output:</p> <pre class="output" data-language="go">OnesCount32(00000000000000000000000000001110) = 3
</pre>   <h2 id="OnesCount64">func <span>OnesCount64</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func OnesCount64(x uint64) int</pre> <p>OnesCount64 returns the number of one bits ("population count") in x. </p>   <h4 id="example_OnesCount64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("OnesCount64(%064b) = %d\n", 14, bits.OnesCount64(14))
</pre> <p>Output:</p> <pre class="output" data-language="go">OnesCount64(0000000000000000000000000000000000000000000000000000000000001110) = 3
</pre>   <h2 id="OnesCount8">func <span>OnesCount8</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func OnesCount8(x uint8) int</pre> <p>OnesCount8 returns the number of one bits ("population count") in x. </p>   <h4 id="example_OnesCount8"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("OnesCount8(%08b) = %d\n", 14, bits.OnesCount8(14))
</pre> <p>Output:</p> <pre class="output" data-language="go">OnesCount8(00001110) = 3
</pre>   <h2 id="Rem">func <span>Rem</span>  <span title="Added in Go 1.14">1.14</span> </h2> <pre data-language="go">func Rem(hi, lo, y uint) uint</pre> <p>Rem returns the remainder of (hi, lo) divided by y. Rem panics for y == 0 (division by zero) but, unlike Div, it doesn't panic on a quotient overflow. </p>
<h2 id="Rem32">func <span>Rem32</span>  <span title="Added in Go 1.14">1.14</span> </h2> <pre data-language="go">func Rem32(hi, lo, y uint32) uint32</pre> <p>Rem32 returns the remainder of (hi, lo) divided by y. Rem32 panics for y == 0 (division by zero) but, unlike <a href="#Div32">Div32</a>, it doesn't panic on a quotient overflow. </p>
<h2 id="Rem64">func <span>Rem64</span>  <span title="Added in Go 1.14">1.14</span> </h2> <pre data-language="go">func Rem64(hi, lo, y uint64) uint64</pre> <p>Rem64 returns the remainder of (hi, lo) divided by y. Rem64 panics for y == 0 (division by zero) but, unlike <a href="#Div64">Div64</a>, it doesn't panic on a quotient overflow. </p>
<h2 id="Reverse">func <span>Reverse</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func Reverse(x uint) uint</pre> <p>Reverse returns the value of x with its bits in reversed order. </p>
<h2 id="Reverse16">func <span>Reverse16</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func Reverse16(x uint16) uint16</pre> <p>Reverse16 returns the value of x with its bits in reversed order. </p>   <h4 id="example_Reverse16"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%016b\n", 19)
fmt.Printf("%016b\n", bits.Reverse16(19))
</pre> <p>Output:</p> <pre class="output" data-language="go">0000000000010011
1100100000000000
</pre>   <h2 id="Reverse32">func <span>Reverse32</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func Reverse32(x uint32) uint32</pre> <p>Reverse32 returns the value of x with its bits in reversed order. </p>   <h4 id="example_Reverse32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%032b\n", 19)
fmt.Printf("%032b\n", bits.Reverse32(19))
</pre> <p>Output:</p> <pre class="output" data-language="go">00000000000000000000000000010011
11001000000000000000000000000000
</pre>   <h2 id="Reverse64">func <span>Reverse64</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func Reverse64(x uint64) uint64</pre> <p>Reverse64 returns the value of x with its bits in reversed order. </p>   <h4 id="example_Reverse64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%064b\n", 19)
fmt.Printf("%064b\n", bits.Reverse64(19))
</pre> <p>Output:</p> <pre class="output" data-language="go">0000000000000000000000000000000000000000000000000000000000010011
1100100000000000000000000000000000000000000000000000000000000000
</pre>   <h2 id="Reverse8">func <span>Reverse8</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func Reverse8(x uint8) uint8</pre> <p>Reverse8 returns the value of x with its bits in reversed order. </p>   <h4 id="example_Reverse8"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%08b\n", 19)
fmt.Printf("%08b\n", bits.Reverse8(19))
</pre> <p>Output:</p> <pre class="output" data-language="go">00010011
11001000
</pre>   <h2 id="ReverseBytes">func <span>ReverseBytes</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func ReverseBytes(x uint) uint</pre> <p>ReverseBytes returns the value of x with its bytes in reversed order. </p>
<p>This function's execution time does not depend on the inputs. </p>
<h2 id="ReverseBytes16">func <span>ReverseBytes16</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func ReverseBytes16(x uint16) uint16</pre> <p>ReverseBytes16 returns the value of x with its bytes in reversed order. </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_ReverseBytes16"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%016b\n", 15)
fmt.Printf("%016b\n", bits.ReverseBytes16(15))
</pre> <p>Output:</p> <pre class="output" data-language="go">0000000000001111
0000111100000000
</pre>   <h2 id="ReverseBytes32">func <span>ReverseBytes32</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func ReverseBytes32(x uint32) uint32</pre> <p>ReverseBytes32 returns the value of x with its bytes in reversed order. </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_ReverseBytes32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%032b\n", 15)
fmt.Printf("%032b\n", bits.ReverseBytes32(15))
</pre> <p>Output:</p> <pre class="output" data-language="go">00000000000000000000000000001111
00001111000000000000000000000000
</pre>   <h2 id="ReverseBytes64">func <span>ReverseBytes64</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func ReverseBytes64(x uint64) uint64</pre> <p>ReverseBytes64 returns the value of x with its bytes in reversed order. </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_ReverseBytes64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%064b\n", 15)
fmt.Printf("%064b\n", bits.ReverseBytes64(15))
</pre> <p>Output:</p> <pre class="output" data-language="go">0000000000000000000000000000000000000000000000000000000000001111
0000111100000000000000000000000000000000000000000000000000000000
</pre>   <h2 id="RotateLeft">func <span>RotateLeft</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func RotateLeft(x uint, k int) uint</pre> <p>RotateLeft returns the value of x rotated left by (k mod <a href="#UintSize">UintSize</a>) bits. To rotate x right by k bits, call RotateLeft(x, -k). </p>
<p>This function's execution time does not depend on the inputs. </p>
<h2 id="RotateLeft16">func <span>RotateLeft16</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func RotateLeft16(x uint16, k int) uint16</pre> <p>RotateLeft16 returns the value of x rotated left by (k mod 16) bits. To rotate x right by k bits, call RotateLeft16(x, -k). </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_RotateLeft16"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%016b\n", 15)
fmt.Printf("%016b\n", bits.RotateLeft16(15, 2))
fmt.Printf("%016b\n", bits.RotateLeft16(15, -2))
</pre> <p>Output:</p> <pre class="output" data-language="go">0000000000001111
0000000000111100
1100000000000011
</pre>   <h2 id="RotateLeft32">func <span>RotateLeft32</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func RotateLeft32(x uint32, k int) uint32</pre> <p>RotateLeft32 returns the value of x rotated left by (k mod 32) bits. To rotate x right by k bits, call RotateLeft32(x, -k). </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_RotateLeft32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%032b\n", 15)
fmt.Printf("%032b\n", bits.RotateLeft32(15, 2))
fmt.Printf("%032b\n", bits.RotateLeft32(15, -2))
</pre> <p>Output:</p> <pre class="output" data-language="go">00000000000000000000000000001111
00000000000000000000000000111100
11000000000000000000000000000011
</pre>   <h2 id="RotateLeft64">func <span>RotateLeft64</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func RotateLeft64(x uint64, k int) uint64</pre> <p>RotateLeft64 returns the value of x rotated left by (k mod 64) bits. To rotate x right by k bits, call RotateLeft64(x, -k). </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_RotateLeft64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%064b\n", 15)
fmt.Printf("%064b\n", bits.RotateLeft64(15, 2))
fmt.Printf("%064b\n", bits.RotateLeft64(15, -2))
</pre> <p>Output:</p> <pre class="output" data-language="go">0000000000000000000000000000000000000000000000000000000000001111
0000000000000000000000000000000000000000000000000000000000111100
1100000000000000000000000000000000000000000000000000000000000011
</pre>   <h2 id="RotateLeft8">func <span>RotateLeft8</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func RotateLeft8(x uint8, k int) uint8</pre> <p>RotateLeft8 returns the value of x rotated left by (k mod 8) bits. To rotate x right by k bits, call RotateLeft8(x, -k). </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_RotateLeft8"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("%08b\n", 15)
fmt.Printf("%08b\n", bits.RotateLeft8(15, 2))
fmt.Printf("%08b\n", bits.RotateLeft8(15, -2))
</pre> <p>Output:</p> <pre class="output" data-language="go">00001111
00111100
11000011
</pre>   <h2 id="Sub">func <span>Sub</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Sub(x, y, borrow uint) (diff, borrowOut uint)</pre> <p>Sub returns the difference of x, y and borrow: diff = x - y - borrow. The borrow input must be 0 or 1; otherwise the behavior is undefined. The borrowOut output is guaranteed to be 0 or 1. </p>
<p>This function's execution time does not depend on the inputs. </p>
<h2 id="Sub32">func <span>Sub32</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Sub32(x, y, borrow uint32) (diff, borrowOut uint32)</pre> <p>Sub32 returns the difference of x, y and borrow, diff = x - y - borrow. The borrow input must be 0 or 1; otherwise the behavior is undefined. The borrowOut output is guaranteed to be 0 or 1. </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_Sub32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// First number is 33&lt;&lt;32 + 23
n1 := []uint32{33, 23}
// Second number is 21&lt;&lt;32 + 12
n2 := []uint32{21, 12}
// Sub them together without producing carry.
d1, carry := bits.Sub32(n1[1], n2[1], 0)
d0, _ := bits.Sub32(n1[0], n2[0], carry)
nsum := []uint32{d0, d1}
fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)

// First number is 3&lt;&lt;32 + 2147483647
n1 = []uint32{3, 0x7fffffff}
// Second number is 1&lt;&lt;32 + 2147483648
n2 = []uint32{1, 0x80000000}
// Sub them together producing carry.
d1, carry = bits.Sub32(n1[1], n2[1], 0)
d0, _ = bits.Sub32(n1[0], n2[0], carry)
nsum = []uint32{d0, d1}
fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
</pre> <p>Output:</p> <pre class="output" data-language="go">[33 23] - [21 12] = [12 11] (carry bit was 0)
[3 2147483647] - [1 2147483648] = [1 4294967295] (carry bit was 1)
</pre>   <h2 id="Sub64">func <span>Sub64</span>  <span title="Added in Go 1.12">1.12</span> </h2> <pre data-language="go">func Sub64(x, y, borrow uint64) (diff, borrowOut uint64)</pre> <p>Sub64 returns the difference of x, y and borrow: diff = x - y - borrow. The borrow input must be 0 or 1; otherwise the behavior is undefined. The borrowOut output is guaranteed to be 0 or 1. </p>
<p>This function's execution time does not depend on the inputs. </p>   <h4 id="example_Sub64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// First number is 33&lt;&lt;64 + 23
n1 := []uint64{33, 23}
// Second number is 21&lt;&lt;64 + 12
n2 := []uint64{21, 12}
// Sub them together without producing carry.
d1, carry := bits.Sub64(n1[1], n2[1], 0)
d0, _ := bits.Sub64(n1[0], n2[0], carry)
nsum := []uint64{d0, d1}
fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)

// First number is 3&lt;&lt;64 + 9223372036854775807
n1 = []uint64{3, 0x7fffffffffffffff}
// Second number is 1&lt;&lt;64 + 9223372036854775808
n2 = []uint64{1, 0x8000000000000000}
// Sub them together producing carry.
d1, carry = bits.Sub64(n1[1], n2[1], 0)
d0, _ = bits.Sub64(n1[0], n2[0], carry)
nsum = []uint64{d0, d1}
fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
</pre> <p>Output:</p> <pre class="output" data-language="go">[33 23] - [21 12] = [12 11] (carry bit was 0)
[3 9223372036854775807] - [1 9223372036854775808] = [1 18446744073709551615] (carry bit was 1)
</pre>   <h2 id="TrailingZeros">func <span>TrailingZeros</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func TrailingZeros(x uint) int</pre> <p>TrailingZeros returns the number of trailing zero bits in x; the result is <a href="#UintSize">UintSize</a> for x == 0. </p>
<h2 id="TrailingZeros16">func <span>TrailingZeros16</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func TrailingZeros16(x uint16) int</pre> <p>TrailingZeros16 returns the number of trailing zero bits in x; the result is 16 for x == 0. </p>   <h4 id="example_TrailingZeros16"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("TrailingZeros16(%016b) = %d\n", 14, bits.TrailingZeros16(14))
</pre> <p>Output:</p> <pre class="output" data-language="go">TrailingZeros16(0000000000001110) = 1
</pre>   <h2 id="TrailingZeros32">func <span>TrailingZeros32</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func TrailingZeros32(x uint32) int</pre> <p>TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0. </p>   <h4 id="example_TrailingZeros32"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("TrailingZeros32(%032b) = %d\n", 14, bits.TrailingZeros32(14))
</pre> <p>Output:</p> <pre class="output" data-language="go">TrailingZeros32(00000000000000000000000000001110) = 1
</pre>   <h2 id="TrailingZeros64">func <span>TrailingZeros64</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func TrailingZeros64(x uint64) int</pre> <p>TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0. </p>   <h4 id="example_TrailingZeros64"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("TrailingZeros64(%064b) = %d\n", 14, bits.TrailingZeros64(14))
</pre> <p>Output:</p> <pre class="output" data-language="go">TrailingZeros64(0000000000000000000000000000000000000000000000000000000000001110) = 1
</pre>   <h2 id="TrailingZeros8">func <span>TrailingZeros8</span>  <span title="Added in Go 1.9">1.9</span> </h2> <pre data-language="go">func TrailingZeros8(x uint8) int</pre> <p>TrailingZeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0. </p>   <h4 id="example_TrailingZeros8"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Printf("TrailingZeros8(%08b) = %d\n", 14, bits.TrailingZeros8(14))
</pre> <p>Output:</p> <pre class="output" data-language="go">TrailingZeros8(00001110) = 1
</pre><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/math/bits/" class="_attribution-link">http://golang.org/pkg/math/bits/</a>
  </p>
</div>