Logo

Programming-Idioms

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

Idiom #68 Create a bitset

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

vec($x, $n, 1) = 0;
#include <bitset>
std::bitset<n> x;
#include <vector>
std::vector<bool> x(n, 0);
using System.Collections;
new BitArray(n);
import std.bitmanip;
BitArray x;
x.length = n;
import 'package:bit_array/bit_array.dart';
final x = FixedBitArray(n);
import "math/big"
var x *big.Int = new(big.Int)
x := make([]bool, n)
x := make([]uint64, (n+63)/64)
import Data.Bits
x :: Integer
x = sum [bit i | i <- [1..n], wannaset i]
let x = new Buffer (Math.ceil (n / 8))
import java.util.BitSet;
BitSet x = new BitSet(n);
(let ((x (make-array n :element-type 'bit))))
$x = new BitSet($n);
procedure Task;
var
  x: Set Of Byte;
begin
  x:= [2,4,8,16,32,64,256];  
end;
uses classes;
procedure task;
var 
  x: TBits;
  n: integer;
begin 
  n := $FFF;
  x := TBits.Create(n);
end;
from __future__ import division
import math
x = bytearray(int(math.ceil(n / 8.0)))
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)
x = 0
let mut x = vec![false; n];

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