https://github.com/purehyperbole/bitfield
A basic variable length bit field implementation
https://github.com/purehyperbole/bitfield
Last synced: 8 months ago
JSON representation
A basic variable length bit field implementation
- Host: GitHub
- URL: https://github.com/purehyperbole/bitfield
- Owner: purehyperbole
- License: mit
- Created: 2021-03-23T16:41:22.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-23T17:12:30.000Z (over 5 years ago)
- Last Synced: 2025-07-03T19:07:40.699Z (12 months ago)
- Language: Go
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bitfield
A basic variable length bit field implementation ported from [this stack overflow answer](https://stackoverflow.com/a/177092)
# Usage
```go
package main
import (
"fmt"
"github.com/purehyperbole/bitfield"
)
type Item int
const (
Eggs Item = iota
Milk
Sugar
Pasta
Cheese
Tomatoes
)
func (i Item) String() string {
return [...]string{"Eggs", "Milk", "Sugar", "Pasta", "Cheese", "Tomatoes"}[i]
}
func main() {
// creates a bitfield with 128 different possible flags
shoppingList := bitfield.New(16)
// set some flags
shoppingList.Set(Eggs)
shoppingList.Set(Pasta)
shoppingList.Set(Cheese)
for _, item := range []int{Eggs, Milk, Sugar, Pasta, Cheese, Tomatoes} {
// check if the list has flags
if shoppingList.Has(item) {
fmt.Println("need:", item)
}
}
}
```