Modules

Bit_Operations

moduleSource

Available on macOS, Linux, Windows, Wasm

#import "Bit_Operations";

Byte swapping, bit scans and population count for integers of any size.

Example↗
assert(popcount(cast(u32) 0b1011) == 3);
assert(byte_swap(cast(u16) 0x0102) == 0x0201);

Procedures#

(input: $T) -> T

Reverses the byte order of input and returns the result; input is untouched.

assert(byte_swap(cast(u32) 0x11223344) == 0x44332211);
assert(byte_swap(cast(u16) 0x00ff) == 0xff00);
assert(byte_swap(byte_swap(cast(u64) 0x0102030405060708)) == 0x0102030405060708);
↗
(value: $T) -> s32

The number of bits set in value, counting only its size_of(T) bytes.

assert(popcount(cast(u32) 0) == 0);
assert(popcount(cast(u32) 0b1011) == 3);
assert(popcount(cast(s8) -1) == 8);
assert(popcount(cast(u64) 0xFFFF_FFFF_FFFF_FFFF) == 64);
↗