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.

std.bitmanip.BitArray.this - multiple declarations

Function BitArray.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 (
  const(bool[]) ba
) pure nothrow;

Parameters

NameDescription
ba Source array of bool values.

Example

import std.algorithm.comparison : equal;

bool[] input = [true, false, false, true, true];
auto a = BitArray(input);
writeln(a.length); // 5
assert(a.bitsSet.equal([0, 3, 4]));

// This also works because an implicit cast to bool[] occurs for this array.
auto b = BitArray([0, 0, 1]);
writeln(b.length); // 3
assert(b.bitsSet.equal([2]));

Example

import std.algorithm.comparison : equal;
import std.array : array;
import std.range : iota, repeat;

BitArray a = true.repeat(70).array;
writeln(a.length); // 70
assert(a.bitsSet.equal(iota(0, 70)));
}

/**
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.

That means a particular care should be taken when passing an array
of a type different than `size_t`, firstly because its length should
be a multiple of `size_t.sizeof`, and secondly because how the bits
are mapped:

size_t[] source = [1, 2, 3, 3424234, 724398, 230947, 389492]; enum sbits = size_t.sizeof * 8; auto ba = BitArray(source, source.length * sbits); foreach (n; 0 .. source.length * sbits) { auto nth_bit = cast(bool) (source[n / sbits] & (1L << (n % sbits))); assert(ba[n] == nth_bit);

Function BitArray.this

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.

ref this (
  void[] v,
  ulong numbits
) pure nothrow @nogc;

That means a particular care should be taken when passing an array of a type different than size_t, firstly because its length should be a multiple of size_t.sizeof, and secondly because how the bits are mapped:

size_t[] source = [1, 2, 3, 3424234, 724398, 230947, 389492];
enum sbits = size_t.sizeof * 8;
auto ba = BitArray(source, source.length * sbits);
foreach (n; 0 .. source.length * sbits)
{
    auto nth_bit = cast(bool) (source[n / sbits] & (1L << (n % sbits)));
    assert(ba[n] == nth_bit);
}

The least significant bit in any size_t unit is the starting bit of this unit, and the most significant bit is the last bit of this unit. Therefore, passing e.g. an array of ints may result in a different BitArray depending on the processor's endianness.

This constructor is the inverse of opCast.

Warning: All unmapped bits in the final word will be set to 0.

Parameters

NameDescription
v Source array. v.length must be a multple of size_t.sizeof.
numbits Number of bits to be mapped from the source array, i.e. length of the created BitArray.

Example

import std.algorithm.comparison : equal;

auto a = BitArray([1, 0, 0, 1, 1]);

// Inverse of the cast.
auto v = cast(void[]) a;
auto b = BitArray(v, a.length);

writeln(b.length); // 5
assert(b.bitsSet.equal([0, 3, 4]));

// a and b share the underlying data.
a[0] = 0;
writeln(b[0]); // 0
writeln(a); // b

Example

import std.algorithm.comparison : equal;

size_t[] source = [0b1100, 0b0011];
enum sbits = size_t.sizeof * 8;
auto ba = BitArray(source, source.length * sbits);
// The least significant bit in each unit is this unit's starting bit.
assert(ba.bitsSet.equal([2, 3, sbits, sbits + 1]));

Example

// Example from the doc for this constructor.
size_t[] source = [1, 0b101, 3, 3424234, 724398, 230947, 389492];
enum sbits = size_t.sizeof * 8;
auto ba = BitArray(source, source.length * sbits);
foreach (n; 0 .. source.length * sbits)
{
    auto nth_bit = cast(bool) (source[n / sbits] & (1L << (n % sbits)));
    writeln(ba[n]); // nth_bit
}

// Example of mapping only part of the array.
import std.algorithm.comparison : equal;

auto bc = BitArray(source, sbits + 1);
assert(bc.bitsSet.equal([0, sbits]));
// The unmapped bits from the final word have been cleared.
writeln(source[1]); // 1

Authors

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

License

Boost License 1.0.