Modules

Bit_Operations

moduleSource
macOSLinuxWindowswasm

#import "Bit_Operations";

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

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);
↗
(input: *$T)

Reverses the byte order of input where it is stored, the in-place form of byte_swap.

value: u32 = 0xAABBCCDD;
byte_swap_in_place(*value);
assert(value == 0xDDCCBBAA);
↗
(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);
↗