#u16
Unsigned 16-bit integers, the U16 type.
A U16 holds a value from 0 to 65535. Its arithmetic wraps: +,
- and * give the result modulo 2^16, so 65535 + 1 is 0 and 0 - 1 is 65535. / and % are unsigned division and remainder, and a
zero divisor panics as it does for Int.
An integer literal in U16 position must fit, so (65536 : U16) is a
compile-time error. Converting a computed Int never wraps silently:
fromInt panics when the value is out of range, tryFromInt answers
None, and truncate keeps the low 16 bits and is the only conversion
that discards any. toInt goes the other way and always succeeds.
Within the family, fromU8 widens a narrower value, and truncateU32
keeps the low 16 bits of a wider one.
bitAnd, bitOr, bitXor, bitNot, shiftLeft and shiftRight have
the same names as the prelude's Int operations. Import the module
qualified, as import u16 as U16, and write U16.shiftLeft.
toBytesBE and toBytesLE write a value as 2 bytes, most or least
significant first, and fromBytesBE and fromBytesLE read one back.
U16 values compare, hash, and render in decimal like Int. toHex
renders the 4-digit hexadecimal form.
#Conversions
#tryFromInt
tryFromInt : Int -> Option U16
tryFromInt nThe value as a U16, or None when it is outside 0 to 65535.
> tryFromInt 200 |> map toInt
Some 200
> tryFromInt 65536 |> map toInt
None#truncate
truncate : Int -> U16
truncate nThe low 16 bits of the value, as a U16.
This is the one conversion that discards bits: a value outside 0 to
65535 is reduced modulo 2^16, and a negative value is taken in two's
complement.
> toInt (truncate 65537)
1
> toInt (truncate (-1))
65535#toInt
toInt : U16 -> Int
toInt xThe value as an Int, from 0 to 65535.
#fromU8
fromU8 : U8 -> U16
fromU8 xA U8 as a U16, the same number.
> toInt (fromU8 (255 : U8))
255#truncateU32
truncateU32 : U32 -> U16
truncateU32 xThe low 16 bits of a U32, as a U16.
#truncateU64
truncateU64 : U64 -> U16
truncateU64 xThe low 16 bits of a U64, as a U16.
> toInt (truncateU64 (u64Truncate 0x123456))
13398#Bit operations
#bitAnd
bitAnd : U16 -> U16 -> U16
bitAnd a bThe bitwise and of two values.
> toInt (bitAnd 12 10)
8#bitOr
bitOr : U16 -> U16 -> U16
bitOr a bThe bitwise or of two values.
> toInt (bitOr 12 10)
14#bitXor
bitXor : U16 -> U16 -> U16
bitXor a bThe bitwise exclusive or of two values.
> toInt (bitXor 12 10)
6#bitNot
bitNot : U16 -> U16
bitNot xEvery bit of the value inverted, within 16 bits.
> toInt (bitNot 0)
65535#shiftLeft
shiftLeft : U16 -> Int -> U16
shiftLeft x kThe value shifted left by a number of bits, the vacated bits zero.
Bits shifted past bit 15 are discarded, and a shift of 16 or more
gives 0. A negative shift panics.
> toInt (shiftLeft 1 4)
16
> toInt (shiftLeft 1 16)
0#shiftRight
shiftRight : U16 -> Int -> U16
shiftRight x kThe value shifted right by a number of bits, the vacated bits zero.
A shift of 16 or more gives 0. A negative shift panics.
> toInt (shiftRight 16 4)
1#rotateLeft
rotateLeft : U16 -> Int -> U16
rotateLeft x kThe value rotated left by a number of bits: the bits shifted out at the top come back in at the bottom.
The amount is taken modulo 16, so a negative amount rotates right.
> toInt (rotateLeft 32768 1)
1#rotateRight
rotateRight : U16 -> Int -> U16
rotateRight x kThe value rotated right by a number of bits: the bits shifted out at the bottom come back in at the top.
The amount is taken modulo 16, so a negative amount rotates left.
> toInt (rotateRight 1 1)
32768#popCount
popCount : U16 -> Int
popCount xThe number of one bits.
> popCount 255
8#leadingZeros
leadingZeros : U16 -> Int
leadingZeros xThe number of zero bits above the highest one bit, 16 for 0.
> leadingZeros 1
15#trailingZeros
trailingZeros : U16 -> Int
trailingZeros xThe number of zero bits below the lowest one bit, 16 for 0.
> trailingZeros 8
3#Bytes
#toBytesBE
toBytesBE : U16 -> Bytes
toBytesBE xThe value as 2 bytes, most significant first.
> B.toArray (toBytesBE 258)
[|1, 2|]#toBytesLE
toBytesLE : U16 -> Bytes
toBytesLE xThe value as 2 bytes, least significant first.
> B.toArray (toBytesLE 258)
[|2, 1|]#fromBytesBE
fromBytesBE : Bytes -> Option U16
fromBytesBE bThe value written as exactly 2 bytes, most significant first, or
None when the byte string has any other length.
> fromBytesBE (toBytesBE 258) |> map toInt
Some 258#fromBytesLE
fromBytesLE : Bytes -> Option U16
fromBytesLE bThe value written as exactly 2 bytes, least significant first, or
None when the byte string has any other length.
> fromBytesLE (toBytesLE 258) |> map toInt
Some 258#Rendering
#toHex
toHex : U16 -> String
toHex xThe value in hexadecimal: 4 lowercase digits, zero-padded, with no prefix.
> toHex 255
"00ff"#Instances
#Instances
U16:Eq,Ord,Num,Bounded,Hashable,Display,Debug
#Num U16
impl Num U16Arithmetic modulo 2^16. fromInt panics when the Int is outside 0
to 65535; it is what a literal in a generic Num a => position becomes,
and tryFromInt and truncate are the non-panicking conversions.