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.
Convert a Int to Int64. This is guaranteed to work for integers in the safe JS range.
fromInt 42
|> toSignedString
--> "42"Give two integers, corresponding to the upper and lower 32 bits
fromInt32s 4 2
|> toHex
--> "0000000400000002"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 complement
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 two Int64 values, intepreting the bits as a signed integer.
Compare two Int64 values, intepreting the bits as an unsigned integer.
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"Convert a Int64 to a hexadecimal string
toHex (fromInt (256 - 1))
--> "00000000000000ff"Bits as a string of 0s and 1s in big-endian order.
toBitString (fromInt 42)
--> "0000000000000000000000000000000000000000000000000000000000101010"Convert an Int64 to its 8 byte values in big-endian order
toByteValues (fromInt 0xDEADBEAF)
--> [0,0,0,0,222,173,190,175]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]
A 64-bit integer type with correct overflow behavior
Internally, the number is stored as two 32-bit integers.