Skip to main content
Log in

Mojo function

pack_bits

pack_bits[width: Int, //, new_type: DType = _uint($0)](val: SIMD[bool, width]) -> SIMD[new_type, 1]

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

flags = SIMD[DType.bool, 8](1, 1, 0, 1, 0, 0, 0, 0)
i = pack_bits[DType.uint8](flags)
print(flags, i) # [True, True, False, True, False, False, False, False] 11
from memory import pack_bits

flags = SIMD[DType.bool, 8](1, 1, 0, 1, 0, 0, 0, 0)
i = pack_bits[DType.uint8](flags)
print(flags, i) # [True, True, False, True, False, False, False, False] 11

Constraints:

The width of the bool vector must be the same as the bitwidth of the target type.

Parameters:

  • width (Int): The source width.
  • new_type (DType): The target type.

Args:

  • val (SIMD[bool, width]): The source value.

Returns:

A new integer scalar which has the same bitwidth as the bool vector.