View source code
Display the source code in std/bitmanip.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Struct std.bitmanip.BitArray

A dynamic array of bits. Each bit in a BitArray can be manipulated individually or by the standard bitwise operators &, |, ^, ~, >>, << and also by other effective member functions; most of them work relative to the BitArray's dimension (see dim), instead of its length.

struct BitArray ;

Constructors

NameDescription
this () Creates a BitArray from a bool array, such that bool values read from left to right correspond to subsequent bits in the BitArray.
this (v, numbits) Creates a BitArray from the raw contents of the source array. The source array is not copied but simply acts as the underlying array of bits, which stores data as size_t units.

Properties

NameTypeDescription
dim[get] ulong
dup[get] BitArrayDuplicates the BitArray and its contents.
length[get] ulong
length[set] ulongSets the amount of bits in the BitArray. Warning: increasing length may overwrite bits in the final word of the current underlying data regardless of whether it is shared between BitArray objects. i.e. D dynamic array extension semantics are not followed.
reverse[get] BitArrayReverses the bits of the BitArray.
sort[get] BitArraySorts the BitArray's elements.

Methods

NameDescription
bitsSet () Return a lazy range of the indices of set bits.
count () Counts all the set bits in the BitArray
flip () Flips all the bits in the BitArray
flip (i) Flips a single bit, specified by pos
opApply (dg) Support for foreach loops for BitArray.
opBinary (e2) Support for binary bitwise operators for BitArray.
opBinary (b) Support for binary operator ~ for BitArray.
opBinaryRight (b) Support for binary operator ~ for BitArray.
opCast () Convert to void[].
opCast () Convert to size_t[].
opCmp (a2) Supports comparison operators for BitArray.
opEquals (a2) Support for operators == and != for BitArray.
opIndex (i) Gets the i'th bit in the BitArray.
opIndexAssign (b, i) Sets the i'th bit in the BitArray.
opOpAssign (e2) Support for operator op= for BitArray.
opOpAssign (b) Support for operator ~= for BitArray. Warning: This will overwrite a bit in the final word of the current underlying data regardless of whether it is shared between BitArray objects. i.e. D dynamic array concatenation semantics are not followed
opOpAssign (nbits) Operator >>= support.
opOpAssign (nbits) Operator <<= support.
opSliceAssign (val) Sets all the values in the BitArray to the value specified by val.
opSliceAssign (val, start, end) Sets the bits of a slice of BitArray starting at index start and ends at index ($D end - 1) with the values specified by val.
opUnary () Support for unary operator ~ for BitArray.
toHash () Support for hashing for BitArray.
toString (sink, fmt) Return a string representation of this BitArray.

Example

Slicing & bitsSet

import std.algorithm.comparison : equal;
import std.range : iota;

bool[] buf = new bool[64 * 3];
buf[0 .. 64] = true;
BitArray b = BitArray(buf);
assert(b.bitsSet.equal(iota(0, 64)));
b <<= 64;
assert(b.bitsSet.equal(iota(64, 128)));

Example

Concatenation and appending

import std.algorithm.comparison : equal;

auto b = BitArray([1, 0]);
b ~= true;
writeln(b[2]); // 1
b ~= BitArray([0, 1]);
auto c = BitArray([1, 0, 1, 0, 1]);
writeln(b); // c
assert(b.bitsSet.equal([0, 2, 4]));

Example

Bit flipping

import std.algorithm.comparison : equal;

auto b = BitArray([1, 1, 0, 1]);
b &= BitArray([0, 1, 1, 0]);
assert(b.bitsSet.equal([1]));
b.flip;
assert(b.bitsSet.equal([0, 2, 3]));

Example

String format of bitarrays

import std.format : format;
auto b = BitArray([1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);
writeln(format("%b", b)); // "1_00001111_00001111"

Example

import std.format : format;

BitArray b;

b = BitArray([]);
writeln(format("%s", b)); // "[]"
assert(format("%b", b) is null);

b = BitArray([1]);
writeln(format("%s", b)); // "[1]"
writeln(format("%b", b)); // "1"

b = BitArray([0, 0, 0, 0]);
writeln(format("%b", b)); // "0000"

b = BitArray([0, 0, 0, 0, 1, 1, 1, 1]);
writeln(format("%s", b)); // "[0, 0, 0, 0, 1, 1, 1, 1]"
writeln(format("%b", b)); // "00001111"

b = BitArray([0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);
writeln(format("%s", b)); // "[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]"
writeln(format("%b", b)); // "00001111_00001111"

b = BitArray([1, 0, 0, 0, 0, 1, 1, 1, 1]);
writeln(format("%b", b)); // "1_00001111"

b = BitArray([1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);
writeln(format("%b", b)); // "1_00001111_00001111"

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis, Alex Rønne Petersen, Damian Ziemba, Amaury SECHET

License

Boost License 1.0.