forked from tunabay/go-bitarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_bitwise_example_test.go
117 lines (90 loc) · 2.2 KB
/
buffer_bitwise_example_test.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
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
// Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package bitarray_test
import (
"fmt"
"github.com/tunabay/go-bitarray"
)
func ExampleBuffer_ToggleBitAt() {
ba := bitarray.MustParse("110010")
buf := bitarray.NewBufferFromBitArray(ba)
fmt.Println(buf)
buf.ToggleBitAt(1)
buf.ToggleBitAt(3)
buf.ToggleBitAt(5)
fmt.Println(buf)
// Output:
// 110010
// 100111
}
func ExampleBuffer_ToggleBitsAt() {
ba := bitarray.MustParse("11110000")
buf := bitarray.NewBufferFromBitArray(ba)
fmt.Println(buf)
buf.ToggleBitsAt(2, 4)
fmt.Println(buf)
// Output:
// 11110000
// 11001100
}
func ExampleBuffer_AndAt() {
ba := bitarray.MustParse("11110000")
buf := bitarray.NewBufferFromBitArray(ba)
fmt.Println(buf)
buf.AndAt(2, bitarray.MustParse("0110"))
fmt.Println(buf)
// Output:
// 11110000
// 11010000
}
func ExampleBuffer_OrAt() {
ba := bitarray.MustParse("11110000")
buf := bitarray.NewBufferFromBitArray(ba)
fmt.Println(buf)
buf.OrAt(2, bitarray.MustParse("0110"))
fmt.Println(buf)
// Output:
// 11110000
// 11111000
}
func ExampleBuffer_XorAt() {
ba := bitarray.MustParse("11110000")
buf := bitarray.NewBufferFromBitArray(ba)
fmt.Println(buf)
buf.XorAt(2, bitarray.MustParse("0110"))
fmt.Println(buf)
// Output:
// 11110000
// 11101000
}
func ExampleBuffer_LeadingZeros() {
ba := bitarray.MustParse("11110000")
buf := bitarray.NewBufferFromBitArray(ba)
fmt.Printf("%b: %d\n", buf, buf.LeadingZeros())
buf.ToggleBitsAt(0, 2)
fmt.Printf("%b: %d\n", buf, buf.LeadingZeros())
// Output:
// 11110000: 0
// 00110000: 2
}
func ExampleBuffer_TrailingZeros() {
ba := bitarray.MustParse("11110000")
buf := bitarray.NewBufferFromBitArray(ba)
fmt.Printf("%b: %d\n", buf, buf.TrailingZeros())
buf.ToggleBitsAt(6, 2)
fmt.Printf("%b: %d\n", buf, buf.TrailingZeros())
// Output:
// 11110000: 4
// 11110011: 0
}
func ExampleBuffer_OnesCount() {
ba := bitarray.MustParse("00111100")
buf := bitarray.NewBufferFromBitArray(ba)
fmt.Printf("%b: %d\n", buf, buf.OnesCount())
buf.ToggleBitsAt(0, 6)
fmt.Printf("%b: %d\n", buf, buf.OnesCount())
// Output:
// 00111100: 4
// 11000000: 2
}