#u64

Unsigned 64-bit integers, the U64 type.

A U64 holds a value from 0 to 18446744073709551615. Its arithmetic wraps: +, - and * give the result modulo 2^64, so 18446744073709551615 + 1 is 0 and 0 - 1 is 18446744073709551615. / and % are unsigned division and remainder, and a zero divisor panics as it does for Int.

An integer literal in U64 position may be as large as the type holds, including above Int's range, as in 0x9E3779B97F4A7C15; a negative one is a compile-time error. Converting a computed Int never wraps silently: fromInt panics on a negative value, tryFromInt answers None, and truncate takes a negative value in two's complement and is the only conversion into U64 that discards anything.

Int holds 63 bits, so the way back narrows: toInt answers None for a value above 4611686018427387903, and toIntTruncating keeps the low 63 bits.

Within the family, fromU8, fromU16 and fromU32 widen a narrower value.

mulWide, addCarry and subBorrow are the operations multi-word arithmetic is built from: the full 128-bit product, and addition and subtraction with a carry or borrow in and out.

bitAnd, bitOr, bitXor, bitNot, shiftLeft and shiftRight have the same names as the prelude's Int operations. Import the module qualified, as import u64 as U64, and write U64.shiftLeft.

toBytesBE and toBytesLE write a value as 8 bytes, most or least significant first, and fromBytesBE and fromBytesLE read one back.

U64 values compare, hash, and render in decimal like Int. toHex renders the 16-digit hexadecimal form.

#Conversions

#tryFromInt

tryFromInt : Int -> Option U64
tryFromInt n

The value as a U64, or None when it is negative.

> tryFromInt 200 |> map toIntTruncating
Some 200
> tryFromInt (-1) |> map toIntTruncating
None

#truncate

truncate : Int -> U64
truncate n

The value modulo 2^64, as a U64.

This is the one conversion from Int that can change the number: a negative value is taken in two's complement.

> truncate (-1) == maxBound
True
> toIntTruncating (truncate 42)
42

#toInt

toInt : U64 -> Option Int
toInt x

The value as an Int, or None when it is above 4611686018427387903, the largest Int.

> toInt 42
Some 42
> toInt 0x4000000000000000
None

#toIntTruncating

toIntTruncating : U64 -> Int
toIntTruncating x

The low 63 bits of the value, as an Int.

This is the one conversion to Int that can change the number: bit 63 is discarded, and bit 62 becomes the sign.

> toIntTruncating 42
42
> toIntTruncating maxBound
-1

#fromU8

fromU8 : U8 -> U64
fromU8 x

A U8 as a U64, the same number.

> toIntTruncating (fromU8 (255 : U8))
255

#fromU16

fromU16 : U16 -> U64
fromU16 x

A U16 as a U64, the same number.

> toIntTruncating (fromU16 (65535 : U16))
65535

#fromU32

fromU32 : U32 -> U64
fromU32 x

A U32 as a U64, the same number.

> toIntTruncating (fromU32 (4294967295 : U32))
4294967295

#Bit operations

#bitAnd

bitAnd : U64 -> U64 -> U64
bitAnd a b

The bitwise and of two values.

> toIntTruncating (bitAnd 12 10)
8

#bitOr

bitOr : U64 -> U64 -> U64
bitOr a b

The bitwise or of two values.

> toIntTruncating (bitOr 12 10)
14

#bitXor

bitXor : U64 -> U64 -> U64
bitXor a b

The bitwise exclusive or of two values.

> toIntTruncating (bitXor 12 10)
6

#bitNot

bitNot : U64 -> U64
bitNot x

Every bit of the value inverted.

> bitNot 0 == maxBound
True

#shiftLeft

shiftLeft : U64 -> Int -> U64
shiftLeft x k

The value shifted left by a number of bits, the vacated bits zero.

Bits shifted past bit 63 are discarded, and a shift of 64 or more gives 0. A negative shift panics.

> toIntTruncating (shiftLeft 1 40)
1099511627776
> shiftLeft 1 64 == 0
True

#shiftRight

shiftRight : U64 -> Int -> U64
shiftRight x k

The value shifted right by a number of bits, the vacated bits zero.

A shift of 64 or more gives 0. A negative shift panics.

> toIntTruncating (shiftRight maxBound 60)
15

#rotateLeft

rotateLeft : U64 -> Int -> U64
rotateLeft x k

The 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 64, so a negative amount rotates right.

> toIntTruncating (rotateLeft 0x8000000000000000 1)
1

#rotateRight

rotateRight : U64 -> Int -> U64
rotateRight x k

The 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 64, so a negative amount rotates left.

> rotateRight 1 1 == 0x8000000000000000
True

#popCount

popCount : U64 -> Int
popCount x

The number of one bits.

> popCount maxBound
64

#leadingZeros

leadingZeros : U64 -> Int
leadingZeros x

The number of zero bits above the highest one bit, 64 for 0.

> leadingZeros 1
63

#trailingZeros

trailingZeros : U64 -> Int
trailingZeros x

The number of zero bits below the lowest one bit, 64 for 0.

> trailingZeros 0x100000000
32

#Multi-word arithmetic

#mulWide

mulWide : U64 -> U64 -> (U64, U64)
mulWide a b

The full product of two values, as its high and low 64 bits.

> mulWide maxBound maxBound == (0xFFFFFFFFFFFFFFFE, 1)
True

#addCarry

addCarry : U64 -> U64 -> Bool -> (U64, Bool)
addCarry a b carry

The sum of two values and a carry in, as the low 64 bits and the carry out.

> addCarry maxBound 1 False == (0, True)
True
> addCarry 2 3 True == (6, False)
True

#subBorrow

subBorrow : U64 -> U64 -> Bool -> (U64, Bool)
subBorrow a b borrow

The difference of two values less a borrow in, as the low 64 bits and the borrow out.

> subBorrow 0 1 False == (maxBound, True)
True
> subBorrow 5 3 True == (1, False)
True

#Bytes

#toBytesBE

toBytesBE : U64 -> Bytes
toBytesBE x

The value as 8 bytes, most significant first.

> B.toArray (toBytesBE 0x0102030405060708)
[|1, 2, 3, 4, 5, 6, 7, 8|]

#toBytesLE

toBytesLE : U64 -> Bytes
toBytesLE x

The value as 8 bytes, least significant first.

> B.toArray (toBytesLE 0x0102030405060708)
[|8, 7, 6, 5, 4, 3, 2, 1|]

#fromBytesBE

fromBytesBE : Bytes -> Option U64
fromBytesBE b

The value written as exactly 8 bytes, most significant first, or None when the byte string has any other length.

> fromBytesBE (toBytesBE 0x0102030405060708) == Some 0x0102030405060708
True

#fromBytesLE

fromBytesLE : Bytes -> Option U64
fromBytesLE b

The value written as exactly 8 bytes, least significant first, or None when the byte string has any other length.

> fromBytesLE (toBytesLE 0x0102030405060708) == Some 0x0102030405060708
True

#Rendering

#toHex

toHex : U64 -> String
toHex x

The value in hexadecimal: 16 lowercase digits, zero-padded, with no prefix.

> toHex 255
"00000000000000ff"

#Impls

#Instances

  • U64: Bounded, Hashable, Display, Debug, Eq, Ord, Num

#Num U64

impl Num U64

Arithmetic modulo 2^64. fromInt panics on a negative Int; it is what a literal in a generic Num a => position becomes, and u64.tryFromInt and u64.truncate are the non-panicking conversions.