Quirrel4.41.0

iostream.swap4

Defined in module "iostream"
from "iostream" import swap4
pure fastcall swap4(val: number): int

Byte-swaps a 32-bit value

Parameters

valnumberthe value whose low 32 bits get byte-swapped

Return value

An int in [0, 4294967295]: the four low bytes of val, in reverse order.

Notes

Only the low 32 bits of val matter; higher bits are discarded, not carried into the result. Swapping is its own inverse, so swap4(swap4(val) & 0xFFFFFFFF) restores the original low 32 bits.

Example

examples/iostream/swap4.nut
from "iostream" import swap4

println("swap4(0x11223344) =", swap4(0x11223344))
println("swap4(swap4(0x11223344)) =", swap4(swap4(0x11223344)))  // swapping twice restores the value

// only the low 32 bits count; the discarded high bits do not come back
println("swap4(0x100000000 + 0x11223344) == swap4(0x11223344) =", swap4(0x100000000 + 0x11223344) == swap4(0x11223344))
Output:
swap4(0x11223344) = 1144201745
swap4(swap4(0x11223344)) = 287454020
swap4(0x100000000 + 0x11223344) == swap4(0x11223344) = true

See also

swap2Byte-swaps a 16-bit value
swap4Byte-swaps the blob contents as an array of 32-bit values
casti2fReinterprets the bits of an integer as a float
iostreammodule index