Logo

Programming-Idioms

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

Idiom #68 Create a bitset

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

class BitSet:
    def __init__(self, n):
        self.a = [False] * n
    def __getitem__(self, i):
        return self.a[i]
    def __setitem__(self, k, v):
        self.a[k] = v
    def __str__(self):
        s = ('01'[x] for x in self.a)
        return ''.join(s)
x = BitSet(n)
from __future__ import division
import math
x = bytearray(int(math.ceil(n / 8.0)))
#include <vector>
std::vector<bool> x(n, 0);

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

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