#u8
Unsigned 8-bit integers, the U8 type.
A U8 holds a value from 0 to 255. Its arithmetic wraps: +, -
and * give the result modulo 2^8, so 255 + 1 is 0 and 0 - 1 is
255. / and % are unsigned division and remainder, and a zero
divisor panics as it does for Int.
An integer literal in U8 position must fit, so (256 : U8) 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 8 bits and is the only conversion
that discards any. toInt goes the other way and always succeeds.
Within the family, truncateU16 and truncateU32 keep the low 8 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 u8 as U8, and write U8.shiftLeft.
U8 values compare, hash, and render in decimal like Int. toHex
renders the 2-digit hexadecimal form.
#Conversions
#tryFromInt
tryFromInt : Int -> Option U8
tryFromInt nThe value as a U8, or None when it is outside 0 to 255.
> tryFromInt 200 |> map toInt
Some 200
> tryFromInt 256 |> map toInt
None#truncate
truncate : Int -> U8
truncate nThe low 8 bits of the value, as a U8.
This is the one conversion that discards bits: a value outside 0 to
255 is reduced modulo 2^8, and a negative value is taken in two's
complement.
> toInt (truncate 257)
1
> toInt (truncate (-1))
255#toInt
toInt : U8 -> Int
toInt xThe value as an Int, from 0 to 255.
#truncateU16
truncateU16 : U16 -> U8
truncateU16 xThe low 8 bits of a U16, as a U8.
#truncateU32
truncateU32 : U32 -> U8
truncateU32 xThe low 8 bits of a U32, as a U8.
#truncateU64
truncateU64 : U64 -> U8
truncateU64 xThe low 8 bits of a U64, as a U8.
> toInt (truncateU64 (u64Truncate 0x1234))
52#Bit operations
#bitAnd
bitAnd : U8 -> U8 -> U8
bitAnd a bThe bitwise and of two values.
> toInt (bitAnd 12 10)
8#bitOr
bitOr : U8 -> U8 -> U8
bitOr a bThe bitwise or of two values.
> toInt (bitOr 12 10)
14#bitXor
bitXor : U8 -> U8 -> U8
bitXor a bThe bitwise exclusive or of two values.
> toInt (bitXor 12 10)
6#bitNot
bitNot : U8 -> U8
bitNot xEvery bit of the value inverted, within 8 bits.
> toInt (bitNot 0)
255#shiftLeft
shiftLeft : U8 -> Int -> U8
shiftLeft x kThe value shifted left by a number of bits, the vacated bits zero.
Bits shifted past bit 7 are discarded, and a shift of 8 or more
gives 0. A negative shift panics.
> toInt (shiftLeft 1 4)
16
> toInt (shiftLeft 1 8)
0#shiftRight
shiftRight : U8 -> Int -> U8
shiftRight x kThe value shifted right by a number of bits, the vacated bits zero.
A shift of 8 or more gives 0. A negative shift panics.
> toInt (shiftRight 16 4)
1#rotateLeft
rotateLeft : U8 -> Int -> U8
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 8, so a negative amount rotates right.
> toInt (rotateLeft 128 1)
1#rotateRight
rotateRight : U8 -> Int -> U8
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 8, so a negative amount rotates left.
> toInt (rotateRight 1 1)
128#popCount
popCount : U8 -> Int
popCount xThe number of one bits.
> popCount 255
8#leadingZeros
leadingZeros : U8 -> Int
leadingZeros xThe number of zero bits above the highest one bit, 8 for 0.
> leadingZeros 1
7#trailingZeros
trailingZeros : U8 -> Int
trailingZeros xThe number of zero bits below the lowest one bit, 8 for 0.
> trailingZeros 8
3#Rendering
#toHex
toHex : U8 -> String
toHex xThe value in hexadecimal: 2 lowercase digits, zero-padded, with no prefix.
> toHex 255
"ff"#Instances
#Instances
U8:Eq,Ord,Num,Bounded,Hashable,Display,Debug
#Num U8
impl Num U8Arithmetic modulo 2^8. fromInt panics when the Int is outside 0
to 255; it is what a literal in a generic Num a => position becomes,
and tryFromInt and truncate are the non-panicking conversions.