#i32
Signed 32-bit integers, the I32 type.
An I32 holds a value from -2147483648 to 2147483647. Its arithmetic
wraps in two's complement: +, - and * give the result modulo 2^32, so
2147483647 + 1 is -2147483648. / and % truncate toward zero, as C
does, so -7 / 2 is -3 and -7 % 2 is -1; minBound / -1 wraps to
minBound, and a zero divisor panics as it does for Int. negate and
abs wrap the same way, so both give minBound for minBound.
An integer literal in I32 position must fit, so (2147483648 : I32) 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.
fromU8 and fromU16 widen an unsigned value, and truncateI64 keeps the
low 32 bits of an I64. fromBits and toBits convert to and from U32
by keeping the 32 bits and changing how they are read, so the bit
operations the u32 module has and this one does not (rotations, bit
counts, bytes, hexadecimal) are reached through them.
bitAnd, bitOr, bitXor, bitNot, shiftLeft and shiftRight have the
same names as the prelude's Int operations. Import the module qualified,
as import i32 as I32, and write I32.shiftLeft. shiftRight is
arithmetic: the vacated bits copy the sign.
I32 values compare, hash, and render in decimal like Int.
#Conversions
#tryFromInt
tryFromInt : Int -> Option I32
tryFromInt nThe value as an I32, or None when it is outside -2147483648 to
2147483647.
> tryFromInt (-200) |> map toInt
Some (-200)
> tryFromInt 2147483648 |> map toInt
None#truncate
truncate : Int -> I32
truncate nThe low 32 bits of the value, read in two's complement, as an I32.
This is the one conversion from Int that discards bits: a value outside
-2147483648 to 2147483647 is reduced modulo 2^32.
> toInt (truncate 2147483648)
-2147483648
> toInt (truncate 4294967295)
-1#toInt
toInt : I32 -> Int
toInt xThe value as an Int, from -2147483648 to 2147483647.
#fromU8
fromU8 : U8 -> I32
fromU8 xA U8 as an I32, the same number.
> toInt (fromU8 (255 : U8))
255#fromU16
fromU16 : U16 -> I32
fromU16 xA U16 as an I32, the same number.
> toInt (fromU16 (65535 : U16))
65535#truncateI64
truncateI64 : I64 -> I32
truncateI64 xThe low 32 bits of an I64, read in two's complement, as an I32.
> toInt (truncateI64 (i64FromBits (u64Truncate (-1))))
-1#fromBits
fromBits : U32 -> I32
fromBits xA U32's 32 bits read in two's complement: the same bits, a different
number when the top bit is set.
> toInt (fromBits (4294967295 : U32))
-1
> toInt (fromBits (7 : U32))
7#toBits
toBits : I32 -> U32
toBits xThe value's 32 bits in two's complement, as a U32.
> u32ToInt (toBits (-1))
4294967295#Bit operations
#bitAnd
bitAnd : I32 -> I32 -> I32
bitAnd a bThe bitwise and of two values.
> toInt (bitAnd (-4) 7)
4#bitOr
bitOr : I32 -> I32 -> I32
bitOr a bThe bitwise or of two values.
> toInt (bitOr 12 10)
14#bitXor
bitXor : I32 -> I32 -> I32
bitXor a bThe bitwise exclusive or of two values.
> toInt (bitXor (-1) 5)
-6#bitNot
bitNot : I32 -> I32
bitNot xEvery bit of the value inverted, so bitNot x is -x - 1.
> toInt (bitNot 0)
-1#shiftLeft
shiftLeft : I32 -> Int -> I32
shiftLeft x kThe value shifted left by a number of bits, the vacated bits zero.
Bits shifted past bit 31 are discarded, the sign bit included, and a shift
of 32 or more gives 0. A negative shift panics.
> toInt (shiftLeft 1 31)
-2147483648
> toInt (shiftLeft 1 32)
0#shiftRight
shiftRight : I32 -> Int -> I32
shiftRight x kThe value shifted right by a number of bits, the vacated bits copies of the sign bit, so a negative value stays negative.
A shift of 32 or more gives 0, or -1 for a negative value. A negative
shift panics.
> toInt (shiftRight (-16) 2)
-4
> toInt (shiftRight (-1) 40)
-1#Instances
#Instances
I32:Eq,Ord,Num,Bounded,Hashable,Display,Debug
#Num I32
impl Num I32Arithmetic modulo 2^32 in two's complement. fromInt panics when the
Int is outside -2147483648 to 2147483647; it is what a literal in a
generic Num a => position becomes, and tryFromInt and truncate are the
non-panicking conversions. abs and negate of minBound are minBound.