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