src/littlesugar/bitwiseCopy

Search:
Group by:

Procs

proc bitwiseCast[T](source: auto): T

Same to cast in Nim but slightly safer.

Unlike cast operator, if sizeof(T) > sizeof(source), all bits in return value after first sizeof(source) bytes are 0.

Example:

doAssert bitwiseCast[float32](1'i32 shl 30'i32) == 2.0
doAssert bitwiseCast[array[3, int]](100) == [100, 0, 0]
proc bitwiseCopy[T, U](dest: var T; source: U)

Copy the bit pattern of source to dest.

Unlike cast operator, if sizeof(T) > sizeof(U), it doesn't change bits of dest after first sizeof(U) bytes.

When T or U are ref, seq, string or array/object/tuple type that contains these type, it is compile error.

Example:

var
  x: float32
  y = 1'i32 shl 30'i32
bitwiseCopy(x, y)
doAssert x == 2.0

var
  ary = [1, 2, 3]
  src = 100
bitwiseCopy(ary, src)
doAssert ary == [100, 2, 3]