iostream.swap2
Defined in module
"iostream"from "iostream" import swap2Byte-swaps a 16-bit value
Parameters
val | number | the 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
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) = trueSee also
swap4 | Byte-swaps a 32-bit value |
swap2 | Byte-swaps the blob contents as an array of 16-bit values |
| iostream | module index |