folkertdev/elm-int64/1.0.0

Int64

An efficient 64-bit integer with correct overflow.

Bitwise operators in javascript can only use 32 bits. Sometimes, external protocols use 64-bit integers. This package implementes such integers with "correct" overflow behavior.

This is a low-level package focussed on speed. The 64-bit integers are represented as a 2-tuple of 32-bit numbers.

type Int64

A 64-bit integer type with correct overflow behavior

Internally, the number is stored as two 32-bit integers.

fromInt : Int -> Int64

Convert a Int to Int64. This is guaranteed to work for integers in the safe JS range.

fromInt 42
    |> toSignedString
    --> "42"
fromInt32s : Int -> Int -> Int64

Give two integers, corresponding to the upper and lower 32 bits

fromInt32s 4 2
    |> toHex
    --> "0000000400000002"

Arithmetic

64-bit addition, with correct overflow

fromInt32s 0xFFFFFFFF 0xFFFFFFFF
    |> Int64.add (Int64.fromInt 1)
    |> Int64.toUnsignedString
    --> "0"


fromInt32s 0xFFFFFFFF 0xFFFFFFFF
    |> Int64.add (Int64.fromInt 2)
    |> Int64.toUnsignedString
    --> "1"

64-bit subtraction, with correct overflow

-- equivalent to `0 - 1`
Int64.subtract  (Int64.fromInt 0) (Int64.fromInt 1)
    |> Int64.toUnsignedString
    --> "18446744073709551615"


-- equivalent to `0 - 1`
Int64.subtract  (Int64.fromInt 0) (Int64.fromInt 1)
    |> Int64.toSignedString
    --> "-1"




-- equivalent to `1 - 0`
Int64.subtract  (Int64.fromInt 1) (Int64.fromInt 0)
    |> Int64.toUnsignedString
    --> "1"

Bitwise

Bitwise and

Bitwise or

Bitwise xor

Bitwise complement

shiftLeftBy : Int -> Int64 -> Int64

Left bitwise shift, typically written <<

Fills in zeros from the right.

Int64.fromInt32s 0xDEADBEAF 0xBAAAAAAD
    |> Int64.shiftLeftBy 16
    |> Int64.toHex
    --> "beafbaaaaaad0000"

Right bitwise shift, typically written >> (but >>> in JavaScript)

Fills in zeros from the left.

Int64.fromInt32s 0xDEADBEAF 0xBAAAAAAD
    |> Int64.shiftRightZfBy 16
    |> Int64.toHex
    --> "0000deadbeafbaaa"

Left bitwise rotation

Int64.fromInt32s 0xDEADBEAF 0xBAAAAAAD
    |> Int64.rotateLeftBy 16
    |> Int64.toHex
    --> "beafbaaaaaaddead"

Right bitwise rotation

Int64.fromInt32s 0xDEADBEAF 0xBAAAAAAD
    |> Int64.rotateRightBy 16
    |> Int64.toHex
    --> "aaaddeadbeafbaaa"

Compare

signedCompare : Int64 -> Int64 -> Order

Compare two Int64 values, intepreting the bits as a signed integer.

Compare two Int64 values, intepreting the bits as an unsigned integer.

Conversion to String

toSignedString : Int64 -> String

Interpret a Int64 as an unsigned integer, and give its string representation

toSignedString (fromInt 10)
    --> "10"


toSignedString (fromInt -10)
    --> "-10"

Interpret a Int64 as an unsigned integer, and give its string representation

toUnsignedString (fromInt 10)
    --> "10"


toUnsignedString (fromInt -10)
    --> "18446744073709551606"
toHex : Int64 -> String

Convert a Int64 to a hexadecimal string

toHex (fromInt (256 - 1))
    -->  "00000000000000ff"
toBitString : Int64 -> String

Bits as a string of 0s and 1s in big-endian order.

toBitString (fromInt 42)
    --> "0000000000000000000000000000000000000000000000000000000000101010"

Conversion to Bytes

decoder : Endianness -> Decoder Int64

A elm/bytes Decoder for Int64

encoder : Endianness -> Int64 -> Encoder

A elm/bytes Encoder for Int64

toByteValues : Int64 -> List Int

Convert an Int64 to its 8 byte values in big-endian order

toByteValues  (fromInt 0xDEADBEAF)
    --> [0,0,0,0,222,173,190,175]
toBits : Int64 -> List Bool

The individual bits as a list of Bool in big-endian order.

toBits (fromInt 10)
    --> [False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,True,False,True,False]