func New
New creates a new BitSet pre-allocated to hold at least size bits.
Package implements an arbitrary-size bit set, also known as bit array.
Bit sets are useful when you need a compact way to track large sets of boolean flags, such as permissions, feature toggles, or membership sets, using significantly less memory than a slice of booleans while also supporting fast bulk operations across entire sets.
Repository can be found at jeronimoalbi/gnome,
as part of jeronimoalbi's Gno smart contracts monorepo.
1package main
2
3import "gno.land/p/g17khqpukees4237dtn3astzapmp462vjhsz6st4/bitset"
4
5const (
6 PermRead = 0
7 PermWrite = 1
8 PermDelete = 2
9 PermAdmin = 3
10)
11
12func main() {
13 var perms bitset.BitSet
14 perms.Set(PermRead)
15 perms.Set(PermWrite)
16
17 println("Can read:", perms.IsSet(PermRead))
18 println("Can write:", perms.IsSet(PermWrite))
19 println("Can delete:", perms.IsSet(PermDelete))
20 println("Is admin:", perms.IsSet(PermAdmin))
21 println("Permissions set:", perms.Len())
22}
23
24// Output:
25// Can read: true
26// Can write: true
27// Can delete: false
28// Is admin: false
29// Permissions set: 2
BitSet implements an arbitrary-size bit array.
And performs an in-place AND with other bitset. Bits beyond the other bitset are cleared.
Clear turns off the bit at a given position.
ClearAll turns off all bits. The size of the bitset remains unchanged.
Compact reclaims memory by removing trailing zero words.
Equal checks whether other bitset have exactly the same bits set.
IsSet checks whether the bit at a given position is set.
Len returns the number of set bits.
Or performs an in-place OR (union) with other bitset. Current bitset grows if the other bitset is bigger.
PaddedString returns a binary representation of the bitset with zero padding. Representation uses MSB-first binary representation.
Set turns on the bit at a given position.
Size returns the total number of bits (including unset) currently stored. It's the total space allocated within the set in bits.
String returns a binary representation of the bitset. Representation uses MSB-first binary representation.
Xor performs an in-place XOR with other. Current bitset grows if the other bitset is bigger.