Mojo function
pack_bits
pack_bits[src_width: Int, //, dtype: DType = DType._uint1 if (src_width == 1) else DType._uint2 if (src_width == 2) else DType._uint4 if (src_width == 4) else DType.uint8 if (src_width == 8) else DType.uint16 if (src_width == 16) else DType.uint32 if (src_width == 32) else DType.uint64 if (src_width == 64) else DType.uint128 if (src_width == 128) else DType.uint256 if (src_width == 256) else DType.invalid, width: Int = 1](val: SIMD[DType.bool, src_width]) -> SIMD[dtype, width]
Packs a SIMD vector of bool values into an integer.
Examples:
This example packs a vector of 8 bool values into a single 8-bit integer.
from memory import pack_bits
bits = SIMD[DType.bool, 8](1, 1, 0, 1, 0, 0, 0, 0)
u8 = pack_bits[DType.uint8](bits)
print(bits, u8) # [True, True, False, True, False, False, False, False] 11Constraints:
The logical bitwidth of the bool vector must be the same as the bitwidth of the target type. The target type must be a unsigned type.
Parameters:
- βsrc_width (
Int): The source width. - βdtype (
DType): The target type. - βwidth (
Int): The target width. 
Args:
- βval (
SIMD): The source value. 
Returns:
SIMD: A new integer scalar which has the same bitwidth as the bool vector.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!