Module

Data.UInt

Package
purescript-uint
Repository
zaquest/purescript-uint

This module provides 32-bit unsigned integers. Provided type UInt is based on the x >>> 0 trick analogous to how PureScript's Int is based on x | 0 trick. The type has range from 0 to 4294967295.

#fromInt Source

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' Source

fromInt' :: Int -> Maybe UInt

Converts positive Ints into UInt. Returns Nothing for negative Ints

> fromInt' 123
(Just 123u)

> fromInt' (-123)
Nothing

#toInt Source

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' Source

toInt' :: UInt -> Maybe Int

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

> toInt' (fromInt 123)
(Just 123)

> toInt' (fromInt (-1))
Nothing

#fromNumber Source

fromNumber :: Number -> UInt

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

#fromNumber' Source

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 Source

toNumber :: UInt -> Number

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

#floor Source

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 Source

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 Source

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

#pow Source

pow :: UInt -> UInt -> UInt

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

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

#and Source

and :: UInt -> UInt -> UInt

Bitwise AND.

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

#(.&.) Source

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

#or Source

or :: UInt -> UInt -> UInt

Bitwise OR.

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

#(.|.) Source

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

#xor Source

xor :: UInt -> UInt -> UInt

Bitwise XOR.

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

#(.^.) Source

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

#shl Source

shl :: UInt -> UInt -> UInt

Bitwise shift left.

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

#shr Source

shr :: UInt -> UInt -> UInt

Bitwise shift right while preserving sign.

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

#zshr Source

zshr :: UInt -> UInt -> UInt

Bitwise zero-fill shift right.

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

#complement Source

complement :: UInt -> UInt

Bitwise NOT.

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

#toString Source

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 Source

fromString :: String -> Maybe UInt

Tries to parse an UInt from a String.