Module

Data.UInt

#UInt

newtype UInt

32-bit unsigned integer. Range from 0 to 4294967295.

Instances

#fromInt

fromInt :: Int -> UInt

Cast an Int to an UInt turning negative Ints into UInts in range from 2^31 to 2^32-1.

> fromInt 123
123u

> fromInt (-123)
4294967173u

#fromInt'

fromInt' :: Int -> Maybe UInt

Converts positive Ints into UInt. Returns Nothing for negative Ints

> fromInt' 123
(Just 123u)

> fromInt' (-123)
Nothing

#toInt

toInt :: UInt -> Int

Cast an UInt to an Int turning UInts in range from 2^31 to 2^32-1 into negative Ints.

> toInt (fromInt 123)
123

> toInt (fromInt (-1))
-1

#toInt'

toInt' :: UInt -> Maybe Int

Converts UInts in range from 0 to 2^31-1 into Ints. Returns Nothing for UInt's in range from 2^31 to 2^32-1.

> toInt' (fromInt 123)
(Just 123)

> toInt' (fromInt (-1))
Nothing

#fromNumber

fromNumber :: Number -> UInt

Cast a Number n to UInt by performing 0-bit unsigned right shift n >>> 0.

#fromNumber'

fromNumber' :: Number -> Maybe UInt

Convert a Number which is already an UInt to UInt. Fails for non-integers and integers not in range from 0 to 2^32-1.

#toNumber

toNumber :: UInt -> Number

Cast an UInt to a Number, which is always safe to do.

#floor

floor :: Number -> UInt

Convert a Number to an UInt. Takes the closest integer equal to or less than the argument. Values outside the UInt range are clamped.

> floor 27.1
27u

> floor 27.9
27u

> floor (-27.1)
0u

> floor (1.0e65)
4294967295u

> floor (-1.0e65)
0u

#ceil

ceil :: Number -> UInt

Convert a Number to an UInt. Takes the closest integer equal to or greater than the argument. Values outside the UInt range are clamped.

> ceil 27.1
28u

> ceil 27.9
28u

> ceil (-27.1)
0u

> ceil (1.0e65)
4294967295u

> ceil (-1.0e65)
0u

#round

round :: Number -> UInt

Convert a Number to an UInt, by taking the nearest integer to the argument. Values outside the UInt range are clamped.

> round 27.1
27u

> round 27.9
28u

> round (-27.1)
0u

> round (-27.9)
0u

> round (1.0e65)
4294967295u

> round (-1.0e65)
0u

#even

even :: UInt -> Boolean

Returns whether an Int53 is an even number.

> even (fromInt 0)
true

> even (fromInt 1)
false

#odd

odd :: UInt -> Boolean

Returns whether an Int53 is an odd number.

> odd (fromInt 0)
false

> odd (fromInt 1)
true

#pow

pow :: UInt -> UInt -> UInt

Raises the first argument to the power of the second argument (the exponent).

> pow (fromInt 2) (fromInt 3)
8u

#and

and :: UInt -> UInt -> UInt

Bitwise AND.

> and (fromInt 6) (fromInt 4)
4u

#(.&.)

Operator alias for Data.UInt.and (left-associative / precedence 10)

#or

or :: UInt -> UInt -> UInt

Bitwise OR.

> or (fromInt 4) (fromInt 2)
6u

#(.|.)

Operator alias for Data.UInt.or (left-associative / precedence 10)

#xor

xor :: UInt -> UInt -> UInt

Bitwise XOR.

> xor (fromInt 6) (fromInt 4)
0u

#(.^.)

Operator alias for Data.UInt.xor (left-associative / precedence 10)

#shl

shl :: UInt -> UInt -> UInt

Bitwise shift left.

> shl (fromInt 4) (fromInt 1)
8u

#shr

shr :: UInt -> UInt -> UInt

Bitwise shift right while preserving sign.

> shr (fromInt 4) (fromInt 1)
2u

#zshr

zshr :: UInt -> UInt -> UInt

Bitwise zero-fill shift right.

> shr (fromInt 4) (fromInt 1)
2u

#complement

complement :: UInt -> UInt

Bitwise NOT.

> complement (fromInt 0xFF...)
0u

#toString

toString :: UInt -> String

Converts an UInt to a String.

There is also a Show instance (so you can use show), but that appends u suffix, which isn't always what you'll want.

 toString (fromInt 42) == "42"
 show (fromInt 42) == "42u"

#fromString

fromString :: String -> Maybe UInt

Tries to parse an UInt from a String.

Modules