Skip to main content

Mojo function

bitcast

bitcast[src_dtype: DType, src_width: Int, //, dtype: DType, width: Int = src_width](val: SIMD[src_dtype, src_width]) -> SIMD[dtype, 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

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

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

Constraints:

The bitwidth of the two types must be the same.

Parameters:

  • src_dtype (DType): The source type.
  • src_width (Int): The source width.
  • dtype (DType): The target type.
  • width (Int): The target width.

Args:

  • val (SIMD[src_dtype, src_width]): The source value.

Returns:

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

Was this page helpful?