@bitCast

@bitCast

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

Converts a value of one type to another type.

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

Asserts that @typeInfo(DestType) != .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.