bitwise

Bitwise adapter over an integral type range. Consumes the range elements bit by bit, from the least significant bit to the most significant bit.

bitwise
(
R
)
(
auto ref R range
)
if (
isInputRange!R &&
isIntegral!(ElementType!R)
)

Parameters

R

an integral input range to iterate over

range R

range to consume bit by by

Return Value

Type: auto

A Bitwise input range with propagated forward, bidirectional and random access capabilities

Examples

import std.algorithm.comparison : equal;
import std.format : format;

// 00000011 00001001
ubyte[] arr = [3, 9];
auto r = arr.bitwise;

// iterate through it as with any other range
assert(format("%(%d%)", r) == "1100000010010000");
assert(format("%(%d%)", r.retro).equal("1100000010010000".retro));

auto r2 = r[5 .. $];
// set a bit
r[2] = 1;
assert(arr[0] == 7);
assert(r[5] == r2[0]);

You can use bitwise to implement an uniform bool generator

import std.algorithm.comparison : equal;
import std.random : rndGen;

auto rb = rndGen.bitwise;
static assert(isInfinite!(typeof(rb)));

auto rb2 = rndGen.bitwise;
// Don't forget that structs are passed by value
assert(rb.take(10).equal(rb2.take(10)));

Meta