BitSpaces represents one or more related BitSpaces whose bit values will not overlap.

Constructors

Properties

Constructors

Properties

bitmask: number

Returns a bitmask containing all values in all created spaces.

create: <T extends string>(...propNames: readonly T[]) => BitSpace<T>

Creates and returns a new BitSpace that is bit shifted to the left as many bits as the ending bit of the previous space created by this instances, so that each new space created is non-overlapping with previous ones.

The numeric values of the properties are guaranteed to be in the same order, increasing in value, as the keys passed to the function.

LisnUsageError If the number of bits in the space will exceed 32.

const spaces = new BitSpaces();
const spaceA = spaces.create("up", "down");

// spaces.nBits => 2
// spaces.bitmask => 3
//
// spaceA:
// {
// bit: {
// up: 1, // at bit 0, i.e. 1 << 0
// down: 2, // at bit 1, i.e. 1 << 1
// },
// start: 0,
// end: 1,
// bitmask: 3, // 1 << 0 | 1 << 1
// has: (p) => p === "up" || p === "down",
// bitmaskFor: (pStart, pEnd) => ...
// nameOf: (v) => v === 1 ? "up" : v === 2 ? "down" : null
// }

const spaceB = spaces.create("left", "right");

// spaces.nBits => 4
// spaces.bitmask => 15
//
// spaceB:
// {
// bit: {
// left: 4, // at bit 2, i.e. 1 << 2
// right: 8, // at bit 3, i.e. 1 << 3
// },
// start: 2,
// end: 3,
// bitmask: 12, // 1 << 2 | 1 << 3
// has: (p) => p === "left" || p === "right",
// bitmaskFor: (pStart, pEnd) => ...
// nameOf: (v) => v === 4 ? "left" : v === 8 ? "right" : null
// }
nBits: number

Returns the number of bits all created spaces span, i.e. the end bit of the one + 1.