Skip to main content
Log in

Mojo function

bitcast

bitcast[type: DType, width: Int, //, new_type: DType, new_width: Int = $1](val: SIMD[type, width]) -> SIMD[new_type, new_width]

Bitcasts a SIMD value to another SIMD value.

For a discussion of byte order, see Converting data: bitcasting and byte order in the Mojo Manual.

Examples:

The following example uses bitcast to break a 32-bit integer into a vector of four 8-bit integers:

from memory import bitcast

one = SIMD[DType.uint32, 1](4631)
many = bitcast[DType.uint8, 4](one)
print(one, many) # 4631 [23, 18, 0, 0]
from memory import bitcast

one = SIMD[DType.uint32, 1](4631)
many = bitcast[DType.uint8, 4](one)
print(one, many) # 4631 [23, 18, 0, 0]

Constraints:

The bitwidth of the two types must be the same.

Parameters:

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

Args:

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

Returns:

A new SIMD value with the specified type and width with a bitcopy of the source SIMD value.