Logo

Programming-Idioms

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

Idiom #68 Create a bitset

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

procedure Task;
var
  x: Set Of Byte;
begin
  x:= [2,4,8,16,32,64,256];  
end;

when the set contains less than 256 elements the built-in Set Of construct can be used.
uses classes;
procedure task;
var 
  x: TBits;
  n: integer;
begin 
  n := $FFF;
  x := TBits.Create(n);
end;

when the set contains more than 256 elements the class TBits must be used
#include <vector>
std::vector<bool> x(n, 0);

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

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