Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #68 Create a bitset

Create an object x to store n bits (n being potentially large).

x := make([]uint64, (n+63)/64)

This compact bitset requires some extra logic to implement get, set, clear (see demo).
x := make([]bool, n)

This makes a simple fixed-size bitset.
It uses more bits in memory than the useful size n.
import "math/big"
var x *big.Int = new(big.Int)

big.Int type makes a decent bitset.
It grows automatically when needed.
#include <vector>
std::vector<bool> x(n, 0);

This will create a vector of n bits initialized to 0.

New implementation...
< >
programming-idioms.org