lauber00 / elm-int64 / 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 alias Int64 =
( Basics.Int, Basics.Int )

A 64-bit integer type with correct overflow behavior

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

fromInt : Basics.Int -> Int64

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

fromInt 42
    |> toSignedString
    --> "42"

fromInt32s : Basics.Int -> Basics.Int -> Int64

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

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

Arithmetic

add : Int64 -> Int64 -> Int64

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"

subtract : Int64 -> Int64 -> Int64

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

and : Int64 -> Int64 -> Int64

Bitwise and

or : Int64 -> Int64 -> Int64

Bitwise or

xor : Int64 -> Int64 -> Int64

Bitwise xor

complement : Int64 -> Int64

Bitwise complement

shiftLeftBy : Basics.Int -> Int64 -> Int64

Left bitwise shift, typically written <<

Fills in zeros from the right.

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

shiftRightZfBy : Basics.Int -> Int64 -> Int64

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

Fills in zeros from the left.

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

rotateLeftBy : Basics.Int -> Int64 -> Int64

Left bitwise rotation

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

rotateRightBy : Basics.Int -> Int64 -> Int64

Right bitwise rotation

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

Compare

signedCompare : Int64 -> Int64 -> Basics.Order

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

unsignedCompare : Int64 -> Int64 -> Basics.Order

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"

toUnsignedString : Int64 -> String

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 : Bytes.Endianness -> Bytes.Decode.Decoder Int64

A elm/bytes Decoder for Int64

encoder : Bytes.Endianness -> Int64 -> Bytes.Encode.Encoder

A elm/bytes Encoder for Int64

toByteValues : Int64 -> List Basics.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 Basics.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]

ZigZag

fromZigZag : Int64 -> Int64

Convert an Int64 with zigZag algorithm to its original Int64 value

fromZigZag (0, 20)
    --> (0, 10)

toZigZag : Int64 -> Int64

Convert an Int64 to a new Int64 with zigZag algorithm

toZigZag (0, 10)
    --> (0, 20)

Conversion to Int

toInt : Int64 -> Basics.Int

Give an Int64 var, corresponding to Int var

toInt (0, 2)
    --> 2