Skip to main content

Mojo function

pack_bits

pack_bits[src_width: Int, //, dtype: DType = _uint(src_width), 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] 11

Constraints:

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?