icidasset/elm-binary/1.1.0

NOTE: the latest version is 2.1.0

Binary

type Bits

The binary sequence.

Use convertors to make Bits.

Binary.fromIntegers [ 0, 1, 0, 1 ]

Convertors

fromHex : String -> Bits

Convert a hex string to list of binary numbers.

>>> fromHex "8" |> toIntegers
[ 1, 0, 0, 0 ]
toHex : Bits -> String

Convert a list of binary numbers to a hex string.

>>> toHex <| fromIntegers [ 1, 0, 0, 0 ]
"8"
fromDecimal : Int -> Bits

Convert a decimal to Bits.

>>> fromDecimal 8 |> toIntegers
[ 1, 0, 0, 0 ]
toDecimal : Bits -> Int

Convert Bits to a decimal.

>>> toDecimal <| fromIntegers [ 1, 0, 0, 0 ]
8
fromIntegers : List Int -> Bits

Convert a list of integers to Bits.

Everything below zero and zero itself becomes a 0 bit, and everything above zero becomes a 1 bit.

>>> fromIntegers [ 1, 0, 0, 0 ] |> toHex
"8"
toIntegers : Bits -> List Int

Convert Bits to a list of integers.

>>> toIntegers <| fromHex "8"
[ 1, 0, 0, 0 ]
fromBooleans : List Bool -> Bits

Convert a list of booleans to Bits.

>>> fromBooleans [ True, False, False, False ] |> toHex
"8"
toBooleans : Bits -> List Bool

Convert Bits to a list of booleans.

>>> toBooleans <| fromHex "8"
[ True, False, False, False ]

Bitwise Operators

and : Bits -> Bits -> Bits

AND operator.

--     0101 (decimal 5)
-- AND 0011 (decimal 3)
--   = 0001 (decimal 1)


>>> Binary.and
..>   (fromHex "5")
..>   (fromHex "3")
ensureBits 3 (fromHex "1")
or : Bits -> Bits -> Bits

OR operator.

--    0101 (decimal 5)
-- OR 0011 (decimal 3)
--  = 0111 (decimal 7)


>>> Binary.or
..>   (fromHex "5")
..>   (fromHex "3")
fromHex "7"
xor : Bits -> Bits -> Bits

XOR operator.

--     0101 (decimal 5)
-- XOR 0011 (decimal 3)
--   = 0110 (decimal 6)


>>> Binary.xor
..>   (fromHex "5")
..>   (fromHex "3")
fromHex "6"

NOT operator.

-- NOT 0111 (decimal 7)
--   = 1000 (decimal 8)


>>> Binary.not
..>   (fromIntegers [ 0, 1, 1, 1 ])
fromIntegers [ 1, 0, 0, 0 ]

Bit Shifting

shiftLeftBy : Int -> Bits -> Bits

Arithmetic/Logical left shift.

-- LEFTSHIFT 00010111 (decimal 23)
--         = 00101110 (decimal 46)


>>> [ 0, 0, 0, 1, 0, 1, 1, 1 ]
..>   |> fromIntegers
..>   |> shiftLeftBy 1
..>   |> toIntegers
[ 0, 0, 1, 0, 1, 1, 1, 0 ]
shiftRightBy : Int -> Bits -> Bits

Arithmetic right shift.

-- ARI-RIGHTSHIFT 10010111 (decimal 151)
--              = 11001011 (decimal 203)


>>> [ 1, 0, 0, 1, 0, 1, 1, 1 ]
..>   |> fromIntegers
..>   |> shiftRightBy 1
..>   |> toIntegers
[ 1, 1, 0, 0, 1, 0, 1, 1 ]

Logical right shift.

-- LOG-RIGHTSHIFT 10010111 (decimal 151)
--              = 00001011 (decimal 11)


>>> [ 0, 0, 0, 1, 0, 1, 1, 1 ]
..>   |> fromIntegers
..>   |> shiftRightZfBy 1
..>   |> toIntegers
[ 0, 0, 0, 0, 1, 0, 1, 1 ]
rotateLeftBy : Int -> Bits -> Bits

Rotate a binary sequence to the left.

NOTE: Make sure your binary sequence is of the correct size before rotating! Rotating 8 bits is not always the same as, for example, 16 bits.

>>> rotateLeftBy 1 (ensureBits 32 <| fromHex "17")
ensureBits 32 (fromHex "2E")


>>> rotateLeftBy 2 (ensureBits 32 <| fromHex "96")
ensureBits 32 (fromHex "258")
rotateRightBy : Int -> Bits -> Bits

Rotate a binary sequence to the right.

NOTE: Make sure your binary sequence is of the correct size before rotating! Rotating 8 bits is not always the same as, for example, 16 bits.

>>> rotateRightBy 1 (ensureBits 64 <| fromHex "17")
ensureBits 64 (fromHex "800000000000000B")


>>> rotateRightBy 1 (ensureBits 32 <| fromHex "96")
ensureBits 32 (fromHex "4B")


>>> rotateRightBy 5 (ensureBits 32 <| fromHex "96")
ensureBits 32 (fromHex "B0000004")

Mathematical Operators

add : Bits -> Bits -> Bits

Add two sets of bits together.

-- ADD 1011
--     1011
--  = 10110


>>> add
..>   (fromIntegers [ 1, 0, 1, 1 ])
..>   (fromIntegers [ 1, 0, 1, 1 ])
fromIntegers [ 1, 0, 1, 1, 0  ]


>>> add
..>   (fromIntegers [ 1, 1, 1, 0, 1 ])
..>   (fromIntegers [ 1, 0, 1, 0 ])
fromIntegers [ 1, 0, 0, 1, 1, 1  ]

Subtract two sets of bits from each other.

-- SUBTRACT 1011
--          11
--        = 1010


>>> subtract
..>   (fromIntegers [ 1, 0, 1, 1 ])
..>   (fromIntegers [ 1, 1 ])
fromIntegers [ 1, 0, 0, 0  ]


>>> subtract
..>   (fromIntegers [ 1, 0, 0, 0, 1 ])
..>   (fromIntegers [ 0, 0, 1, 0, 0 ])
fromIntegers [ 0, 1, 1, 0, 1  ]

Utilities

concat : List Bits -> Bits

Concat multiple binary sequences.

>>> [ fromIntegers [ 1, 0, 0, 0 ]
..> , fromIntegers [ 0, 1, 0, 1 ]
..> ]
..>   |> concat
..>   |> toDecimal
133

Drops the leading zeros of a binary sequence.

>>> dropLeadingZeros (fromIntegers [ 0, 0, 1, 0 ])
fromIntegers [ 1, 0 ]
ensureBits : Int -> Bits -> Bits

Ensure the binary sequence length is of certain size.

>>> ensureBits 4 (fromIntegers [ 1, 0 ])
fromIntegers [ 0, 0, 1, 0 ]

Makes two sequences isometric (equal in size).

>>> makeIsometric
..>   (fromIntegers [ 0, 1, 0 ])
..>   (fromIntegers [ 1, 0, 0, 0 ])
( fromIntegers [ 0, 0, 1, 0 ]
, fromIntegers [ 1, 0, 0, 0 ]
)