https://github.com/astef/bitmask
Arbitrary size bitmask (aka bitset) with efficient Slice method
https://github.com/astef/bitmask
binary bitmask bits bitset go mask slice
Last synced: 6 months ago
JSON representation
Arbitrary size bitmask (aka bitset) with efficient Slice method
- Host: GitHub
- URL: https://github.com/astef/bitmask
- Owner: astef
- License: mit
- Created: 2023-03-19T12:20:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-12T19:00:02.000Z (about 1 year ago)
- Last Synced: 2025-04-15T06:11:47.692Z (6 months ago)
- Topics: binary, bitmask, bits, bitset, go, mask, slice
- Language: Go
- Homepage:
- Size: 58.6 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bitmask
[](https://pkg.go.dev/github.com/astef/bitmask) 
Arbitrary size bitmask (aka bitset) with efficient Slice method.
### `.Slice` doesn't create copies of the underlying buffer, just like Go slices
```go
bm := bitmask.New(4) // [4]{0000}
bm.Set(3) // [4]{0001}
bm.Slice(2, 4).ToggleAll() // [4]{0010}
bm.Slice(1, 3).ToggleAll() // [4]{0100}
bm.Slice(0, 2).ToggleAll() // [4]{1000}
bm.ClearAll() // [4]{0000}
```### It's safe to copy overlapping bitmasks, which were created by slicing the original one
```go
base := bitmask.New(10)
base.Set(0)
base.Set(9) // [10]{1000000001}src := base.Slice(0, 8) // [8]{10000000}
dst := base.Slice(2, 10) // [8]{00000001}bitmask.Copy(dst, src)
fmt.Println(base)
``````
[10]{1010000000}
```### Iterator
Go >=1.23 iterator is exposed by `.Bits()` method:
```go
bm := bitmask.New(5)
bm.Set(0)
bm.Set(3)for idx, isSet := range bm.Bits() {
// use the value
fmt.Printf("%v) %v\n", index, value)
}
```There's also an old-style equivalent:
```go
it := bm.Iterator()
for {
ok, value, index := it.Next()
if !ok {
break
}// use the value
fmt.Printf("%v) %v\n", index, value)
}
``````
0) true
1) false
2) false
3) true
4) false
```### `.String()` is O(1), it starts stripping after 512 bits
```go
bm := bitmask.New(513)
bm.Slice(255, 321).SetAll()
fmt.Println(bm)
``````
[513]{0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000001 1000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0}
```### Non-copying constructor to avoid heap-allocating bit buffer
```go
bm := bitmask.NewFromUintRawNocopy(1, 2, 3)
```From `go build -gcflags=-m`
```
inlining call to bitmask.NewFromUintRawNocopy
... argument does not escape
&bitmask.BitMask{...} does not escape
```