@bitCast

@bitCast

@bitCast(comptime DestType: type, value: var) DestType

Converts a value of one type to another type.

Asserts that @sizeOf(@typeOf(value)) == @sizeOf(DestType).

Asserts that @typeId(DestType) != @import("builtin").TypeId.Pointer. Use @ptrCast or @intToPtr if you need this.

Can be used for these things for example:

  • Convert f32 to u32 bits
  • Convert i32 to u32 preserving twos complement

Works at compile-time if value is known at compile time. It's a compile error to bitcast a struct to a scalar type of the same size since structs have undefined layout. However if the struct is packed then it works.