Skip to content

Large Sets of Bits in Golang with BitSet

Bitsets are powerful data structures that allow you to efficiently manage large sets of bits. They can be used to represent sets of integers, store binary data, or perform bitwise operations. In this article, we’ll explore how to use the bitset package in Golang to efficiently manage large sets of bits.

A BitSet is a data structure that represents a set of non-negative integers, utilizing a compact array of bits to store their presence or absence. Each bit in the BitSet array represents an element, with 1 indicating that the element is present in the set and 0 indicating that it is absent.

BitSets have several advantages over other data structures like arrays or maps, including:

  1. Memory efficiency: BitSets store data in a compact bit array, resulting in significant memory savings compared to other data structures.
  2. Performance: Bitwise operations on BitSets are faster than comparable operations on arrays or maps, allowing for efficient set operations like union, intersection, and difference.
  3. Ease of use: The BitSet package provides a simple and easy-to-use API for working with sets of bits.

The BitSet package is not included in the Golang standard library, so you’ll need to install it using the go get command:

Terminal window
go get -u github.com/willf/bitset

To use BitSet in your Golang project, first import the package:

import "github.com/willf/bitset"

Use the bitset.New function to create a new BitSet with the specified number of bits:

// Create a BitSet with 100 bits
myBitSet := bitset.New(100)

To set a bit, use the Set method, providing the index of the bit to set:

// Set the bit at index 42
myBitSet.Set(42)

To clear a bit, use the Clear method, providing the index of the bit to clear:

// Clear the bit at index 42
myBitSet.Clear(42)

To check if a bit is set, use the Test method, providing the index of the bit to test:

// Check if the bit at index 42 is set
isSet := myBitSet.Test(42)

To count the number of set bits in a BitSet, use the Count method:

// Get the number of set bits in the BitSet
count := myBitSet.Count()

BitSets support several bitwise operations, including:

  • And: Perform a bitwise AND operation between two BitSets
  • Or: Perform a bitwise OR operation between two BitSets
  • Xor: Perform a bitwise XOR operation between two BitSets
  • AndNot: Perform a bitwise AND NOT operation between two BitSets

Here’s an example of performing bitwise AND and OR operations on two BitSets:

// Create two BitSets
bitset1 := bitset.New(100)
bitset2 := bitset.New(100)
// Set some bits in each BitSet
bitset1.Set(1).Set(3).Set(5)
bitset2.Set(1).Set(2).Set(3)
// Perform bitwise AND and OR operations
andResult := bitset1.And(bitset2)
orResult := bitset1.Or(bitset2)

To iterate over the set bits in a BitSet, use the NextSet method to find the next set bit and the EOL method to check if you’ve reached the end of the BitSet:

for i, ok := myBitSet.NextSet(0); !ok; i, ok = myBitSet.NextSet(i + 1) {
fmt.Printf("Bit %d is set\n", i)
}

BitSets offer a powerful and efficient way to manage large sets of bits in Golang. By using the BitSet package, you can easily take advantage of bitwise operations and memory-efficient storage to work with sets of bits in your applications.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js