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
Name | Description |
---|---|
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
|
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
Name | Type | Description |
---|---|---|
dim [get]
|
ulong | |
dup [get]
|
BitArray | Duplicates the BitArray and its contents.
|
length [get]
|
ulong | |
length [set]
|
ulong | Sets the amount of bits in the BitArray .
Warning: increasing length may overwrite bits in
final word up to the next word boundary. i.e. D dynamic
array extension semantics are not followed.
|
reverse [get]
|
BitArray | Reverses the bits of the BitArray .
|
sort [get]
|
BitArray | Sorts the BitArray 's elements.
|
Methods
Name | Description |
---|---|
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
|
Flips a single bit, specified by pos
|
opApply
|
Support for foreach loops for BitArray .
|
opBinary
|
Support for binary bitwise operators for BitArray .
|
opCast
|
Convert to void[] .
|
opCast
|
Convert to size_t[] .
|
opCat
|
Support for binary operator ~ for BitArray .
|
opCatAssign
|
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
|
opCat_r
|
Support for binary operator ~ for BitArray .
|
opCmp
|
Supports comparison operators for BitArray .
|
opCom
|
Support for unary operator ~ for BitArray .
|
opEquals
|
Support for operators == and != for BitArray .
|
opIndex
|
Gets the i 'th bit in the BitArray .
|
opIndexAssign
|
Sets the i 'th bit in the BitArray .
|
opOpAssign
|
Support for operator op= for BitArray .
|
opOpAssign
|
Operator >>= support.
|
opOpAssign
|
Operator <<= support.
|
opSliceAssign
|
Sets all the values in the BitArray to the
value specified by val .
|
opSliceAssign
|
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 .
|
toHash
|
Support for hashing for BitArray .
|
toString
|
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
Copyright © 1999-2018 by the D Language Foundation | Page generated by ddox.