Quirrel4.41.0

iostream.swap2

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

Byte-swaps a 16-bit value

Parameters

valnumberthe value whose low 16 bits get byte-swapped

Return value

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

Notes

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

Example

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

println("swap2(0x1234) =", swap2(0x1234))
println("swap2(swap2(0x1234)) =", swap2(swap2(0x1234)))  // swapping twice restores the value

// only the low 16 bits count; the discarded high bits do not come back
println("swap2(0x00011234) == swap2(0x1234) =", swap2(0x00011234) == swap2(0x1234))
Output:
swap2(0x1234) = 13330
swap2(swap2(0x1234)) = 4660
swap2(0x00011234) == swap2(0x1234) = true

See also

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