Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Dec 11, 2018
1 parent 5c3ae3d commit 615a675
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog


#### 1.2.0

- Added `chunksOf` function.
- Added `width` function.


#### 1.1.0

Added `concat` function.
Expand Down
2 changes: 1 addition & 1 deletion docs.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"Binary","comment":"\n\n@docs Bits\n\n\n# Convertors\n\n@docs fromHex, toHex, fromDecimal, toDecimal, fromIntegers, toIntegers, fromBooleans, toBooleans\n\n\n# Bitwise Operators\n\n@docs and, or, xor, not\n\n\n# Bit Shifting\n\n@docs shiftLeftBy, shiftRightBy, shiftRightZfBy, rotateLeftBy, rotateRightBy\n\n\n# Mathematical Operators\n\n@docs add, subtract\n\n\n# Utilities\n\n@docs concat, dropLeadingZeros, ensureBits, makeIsometric\n\n","unions":[{"name":"Bits","comment":" **The binary sequence.**\n\nUse convertors to make `Bits`.\n\n Binary.fromIntegers [ 0, 1, 0, 1 ]\n\n","args":[],"cases":[]}],"aliases":[],"values":[{"name":"add","comment":" Add two sets of bits together.\n\n -- ADD 1011\n -- 1011\n -- = 10110\n\n >>> add\n ..> (fromIntegers [ 1, 0, 1, 1 ])\n ..> (fromIntegers [ 1, 0, 1, 1 ])\n fromIntegers [ 1, 0, 1, 1, 0 ]\n\n >>> add\n ..> (fromIntegers [ 1, 1, 1, 0, 1 ])\n ..> (fromIntegers [ 1, 0, 1, 0 ])\n fromIntegers [ 1, 0, 0, 1, 1, 1 ]\n\n","type":"Binary.Bits -> Binary.Bits -> Binary.Bits"},{"name":"and","comment":" AND operator.\n\n -- 0101 (decimal 5)\n -- AND 0011 (decimal 3)\n -- = 0001 (decimal 1)\n\n >>> Binary.and\n ..> (fromHex \"5\")\n ..> (fromHex \"3\")\n ensureBits 3 (fromHex \"1\")\n\n","type":"Binary.Bits -> Binary.Bits -> Binary.Bits"},{"name":"concat","comment":" Concat multiple binary sequences.\n\n >>> [ fromIntegers [ 1, 0, 0, 0 ]\n ..> , fromIntegers [ 0, 1, 0, 1 ]\n ..> ]\n ..> |> concat\n ..> |> toDecimal\n 133\n\n","type":"List.List Binary.Bits -> Binary.Bits"},{"name":"dropLeadingZeros","comment":" Drops the leading zeros of a binary sequence.\n\n >>> dropLeadingZeros (fromIntegers [ 0, 0, 1, 0 ])\n fromIntegers [ 1, 0 ]\n\n","type":"Binary.Bits -> Binary.Bits"},{"name":"ensureBits","comment":" Ensure the binary sequence length is of certain size.\n\n >>> ensureBits 4 (fromIntegers [ 1, 0 ])\n fromIntegers [ 0, 0, 1, 0 ]\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"fromBooleans","comment":" Convert a list of booleans to `Bits`.\n\n >>> fromBooleans [ True, False, False, False ] |> toHex\n \"8\"\n\n","type":"List.List Basics.Bool -> Binary.Bits"},{"name":"fromDecimal","comment":" Convert a decimal to `Bits`.\n\n >>> fromDecimal 8 |> toIntegers\n [ 1, 0, 0, 0 ]\n\n","type":"Basics.Int -> Binary.Bits"},{"name":"fromHex","comment":" Convert a hex string to list of binary numbers.\n\n >>> fromHex \"8\" |> toIntegers\n [ 1, 0, 0, 0 ]\n\n","type":"String.String -> Binary.Bits"},{"name":"fromIntegers","comment":" Convert a list of integers to `Bits`.\n\nEverything below zero and zero itself becomes a `0` bit,\nand everything above zero becomes a `1` bit.\n\n >>> fromIntegers [ 1, 0, 0, 0 ] |> toHex\n \"8\"\n\n","type":"List.List Basics.Int -> Binary.Bits"},{"name":"makeIsometric","comment":" Makes two sequences isometric (equal in size).\n\n >>> makeIsometric\n ..> (fromIntegers [ 0, 1, 0 ])\n ..> (fromIntegers [ 1, 0, 0, 0 ])\n ( fromIntegers [ 0, 0, 1, 0 ]\n , fromIntegers [ 1, 0, 0, 0 ]\n )\n\n","type":"Binary.Bits -> Binary.Bits -> ( Binary.Bits, Binary.Bits )"},{"name":"not","comment":" NOT operator.\n\n -- NOT 0111 (decimal 7)\n -- = 1000 (decimal 8)\n\n >>> Binary.not\n ..> (fromIntegers [ 0, 1, 1, 1 ])\n fromIntegers [ 1, 0, 0, 0 ]\n\n","type":"Binary.Bits -> Binary.Bits"},{"name":"or","comment":" OR operator.\n\n -- 0101 (decimal 5)\n -- OR 0011 (decimal 3)\n -- = 0111 (decimal 7)\n\n >>> Binary.or\n ..> (fromHex \"5\")\n ..> (fromHex \"3\")\n fromHex \"7\"\n\n","type":"Binary.Bits -> Binary.Bits -> Binary.Bits"},{"name":"rotateLeftBy","comment":" Rotate a binary sequence to the left.\n\n_NOTE: Make sure your binary sequence is of the correct size before rotating!\nRotating 8 bits is not always the same as, for example, 16 bits._\n\n >>> rotateLeftBy 1 (ensureBits 32 <| fromHex \"17\")\n ensureBits 32 (fromHex \"2E\")\n\n >>> rotateLeftBy 2 (ensureBits 32 <| fromHex \"96\")\n ensureBits 32 (fromHex \"258\")\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"rotateRightBy","comment":" Rotate a binary sequence to the right.\n\n_NOTE: Make sure your binary sequence is of the correct size before rotating!\nRotating 8 bits is not always the same as, for example, 16 bits._\n\n >>> rotateRightBy 1 (ensureBits 64 <| fromHex \"17\")\n ensureBits 64 (fromHex \"800000000000000B\")\n\n >>> rotateRightBy 1 (ensureBits 32 <| fromHex \"96\")\n ensureBits 32 (fromHex \"4B\")\n\n >>> rotateRightBy 5 (ensureBits 32 <| fromHex \"96\")\n ensureBits 32 (fromHex \"B0000004\")\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"shiftLeftBy","comment":" Arithmetic/Logical left shift.\n\n -- LEFTSHIFT 00010111 (decimal 23)\n -- = 00101110 (decimal 46)\n\n >>> [ 0, 0, 0, 1, 0, 1, 1, 1 ]\n ..> |> fromIntegers\n ..> |> shiftLeftBy 1\n ..> |> toIntegers\n [ 0, 0, 1, 0, 1, 1, 1, 0 ]\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"shiftRightBy","comment":" Arithmetic right shift.\n\n -- ARI-RIGHTSHIFT 10010111 (decimal 151)\n -- = 11001011 (decimal 203)\n\n >>> [ 1, 0, 0, 1, 0, 1, 1, 1 ]\n ..> |> fromIntegers\n ..> |> shiftRightBy 1\n ..> |> toIntegers\n [ 1, 1, 0, 0, 1, 0, 1, 1 ]\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"shiftRightZfBy","comment":" Logical right shift.\n\n -- LOG-RIGHTSHIFT 10010111 (decimal 151)\n -- = 00001011 (decimal 11)\n\n >>> [ 0, 0, 0, 1, 0, 1, 1, 1 ]\n ..> |> fromIntegers\n ..> |> shiftRightZfBy 1\n ..> |> toIntegers\n [ 0, 0, 0, 0, 1, 0, 1, 1 ]\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"subtract","comment":" Subtract two sets of bits from each other.\n\n -- SUBTRACT 1011\n -- 11\n -- = 1010\n\n >>> subtract\n ..> (fromIntegers [ 1, 0, 1, 1 ])\n ..> (fromIntegers [ 1, 1 ])\n fromIntegers [ 1, 0, 0, 0 ]\n\n >>> subtract\n ..> (fromIntegers [ 1, 0, 0, 0, 1 ])\n ..> (fromIntegers [ 0, 0, 1, 0, 0 ])\n fromIntegers [ 0, 1, 1, 0, 1 ]\n\n","type":"Binary.Bits -> Binary.Bits -> Binary.Bits"},{"name":"toBooleans","comment":" Convert `Bits` to a list of booleans.\n\n >>> toBooleans <| fromHex \"8\"\n [ True, False, False, False ]\n\n","type":"Binary.Bits -> List.List Basics.Bool"},{"name":"toDecimal","comment":" Convert `Bits` to a decimal.\n\n >>> toDecimal <| fromIntegers [ 1, 0, 0, 0 ]\n 8\n\n","type":"Binary.Bits -> Basics.Int"},{"name":"toHex","comment":" Convert a list of binary numbers to a hex string.\n\n >>> toHex <| fromIntegers [ 1, 0, 0, 0 ]\n \"8\"\n\n","type":"Binary.Bits -> String.String"},{"name":"toIntegers","comment":" Convert `Bits` to a list of integers.\n\n >>> toIntegers <| fromHex \"8\"\n [ 1, 0, 0, 0 ]\n\n","type":"Binary.Bits -> List.List Basics.Int"},{"name":"xor","comment":" XOR operator.\n\n -- 0101 (decimal 5)\n -- XOR 0011 (decimal 3)\n -- = 0110 (decimal 6)\n\n >>> Binary.xor\n ..> (fromHex \"5\")\n ..> (fromHex \"3\")\n fromHex \"6\"\n\n","type":"Binary.Bits -> Binary.Bits -> Binary.Bits"}],"binops":[]}]
[{"name":"Binary","comment":"\n\n@docs Bits\n\n\n# Convertors\n\n@docs fromHex, toHex, fromDecimal, toDecimal, fromIntegers, toIntegers, fromBooleans, toBooleans\n\n\n# Bitwise Operators\n\n@docs and, or, xor, not\n\n\n# Bit Shifting\n\n@docs shiftLeftBy, shiftRightBy, shiftRightZfBy, rotateLeftBy, rotateRightBy\n\n\n# Mathematical Operators\n\n@docs add, subtract\n\n\n# Utilities\n\n@docs chunksOf, concat, dropLeadingZeros, ensureBits, makeIsometric, width\n\n","unions":[{"name":"Bits","comment":" **The binary sequence.**\n\nUse convertors to make `Bits`.\n\n Binary.fromIntegers [ 0, 1, 0, 1 ]\n\n","args":[],"cases":[]}],"aliases":[],"values":[{"name":"add","comment":" Add two sets of bits together.\n\n -- ADD 1011\n -- 1011\n -- = 10110\n\n >>> add\n ..> (fromIntegers [ 1, 0, 1, 1 ])\n ..> (fromIntegers [ 1, 0, 1, 1 ])\n fromIntegers [ 1, 0, 1, 1, 0 ]\n\n >>> add\n ..> (fromIntegers [ 1, 1, 1, 0, 1 ])\n ..> (fromIntegers [ 1, 0, 1, 0 ])\n fromIntegers [ 1, 0, 0, 1, 1, 1 ]\n\n","type":"Binary.Bits -> Binary.Bits -> Binary.Bits"},{"name":"and","comment":" AND operator.\n\n -- 0101 (decimal 5)\n -- AND 0011 (decimal 3)\n -- = 0001 (decimal 1)\n\n >>> Binary.and\n ..> (fromHex \"5\")\n ..> (fromHex \"3\")\n ensureBits 3 (fromHex \"1\")\n\n","type":"Binary.Bits -> Binary.Bits -> Binary.Bits"},{"name":"chunksOf","comment":" Split the binary sequence in multiple chunks.\n\n >>> fromIntegers [ 1, 0, 0, 0, 1, 0, 1, 0 ]\n ..> |> chunksOf 4\n ..> |> List.map toIntegers\n [ [ 1, 0, 0, 0 ]\n , [ 1, 0, 1, 0 ]\n ]\n\n","type":"Basics.Int -> Binary.Bits -> List.List Binary.Bits"},{"name":"concat","comment":" Concat multiple binary sequences.\n\n >>> [ fromIntegers [ 1, 0, 0, 0 ]\n ..> , fromIntegers [ 0, 1, 0, 1 ]\n ..> ]\n ..> |> concat\n ..> |> toDecimal\n 133\n\n","type":"List.List Binary.Bits -> Binary.Bits"},{"name":"dropLeadingZeros","comment":" Drops the leading zeros of a binary sequence.\n\n >>> dropLeadingZeros (fromIntegers [ 0, 0, 1, 0 ])\n fromIntegers [ 1, 0 ]\n\n","type":"Binary.Bits -> Binary.Bits"},{"name":"ensureBits","comment":" Ensure the binary sequence length is of certain size.\n\n >>> ensureBits 4 (fromIntegers [ 1, 0 ])\n fromIntegers [ 0, 0, 1, 0 ]\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"fromBooleans","comment":" Convert a list of booleans to `Bits`.\n\n >>> fromBooleans [ True, False, False, False ] |> toHex\n \"8\"\n\n","type":"List.List Basics.Bool -> Binary.Bits"},{"name":"fromDecimal","comment":" Convert a decimal to `Bits`.\n\n >>> fromDecimal 8 |> toIntegers\n [ 1, 0, 0, 0 ]\n\n","type":"Basics.Int -> Binary.Bits"},{"name":"fromHex","comment":" Convert a hex string to list of binary numbers.\n\n >>> fromHex \"8\" |> toIntegers\n [ 1, 0, 0, 0 ]\n\n","type":"String.String -> Binary.Bits"},{"name":"fromIntegers","comment":" Convert a list of integers to `Bits`.\n\nEverything below zero and zero itself becomes a `0` bit,\nand everything above zero becomes a `1` bit.\n\n >>> fromIntegers [ 1, 0, 0, 0 ] |> toHex\n \"8\"\n\n","type":"List.List Basics.Int -> Binary.Bits"},{"name":"makeIsometric","comment":" Makes two sequences isometric (equal in size).\n\n >>> makeIsometric\n ..> (fromIntegers [ 0, 1, 0 ])\n ..> (fromIntegers [ 1, 0, 0, 0 ])\n ( fromIntegers [ 0, 0, 1, 0 ]\n , fromIntegers [ 1, 0, 0, 0 ]\n )\n\n","type":"Binary.Bits -> Binary.Bits -> ( Binary.Bits, Binary.Bits )"},{"name":"not","comment":" NOT operator.\n\n -- NOT 0111 (decimal 7)\n -- = 1000 (decimal 8)\n\n >>> Binary.not\n ..> (fromIntegers [ 0, 1, 1, 1 ])\n fromIntegers [ 1, 0, 0, 0 ]\n\n","type":"Binary.Bits -> Binary.Bits"},{"name":"or","comment":" OR operator.\n\n -- 0101 (decimal 5)\n -- OR 0011 (decimal 3)\n -- = 0111 (decimal 7)\n\n >>> Binary.or\n ..> (fromHex \"5\")\n ..> (fromHex \"3\")\n fromHex \"7\"\n\n","type":"Binary.Bits -> Binary.Bits -> Binary.Bits"},{"name":"rotateLeftBy","comment":" Rotate a binary sequence to the left.\n\n_NOTE: Make sure your binary sequence is of the correct size before rotating!\nRotating 8 bits is not always the same as, for example, 16 bits._\n\n >>> rotateLeftBy 1 (ensureBits 32 <| fromHex \"17\")\n ensureBits 32 (fromHex \"2E\")\n\n >>> rotateLeftBy 2 (ensureBits 32 <| fromHex \"96\")\n ensureBits 32 (fromHex \"258\")\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"rotateRightBy","comment":" Rotate a binary sequence to the right.\n\n_NOTE: Make sure your binary sequence is of the correct size before rotating!\nRotating 8 bits is not always the same as, for example, 16 bits._\n\n >>> rotateRightBy 1 (ensureBits 64 <| fromHex \"17\")\n ensureBits 64 (fromHex \"800000000000000B\")\n\n >>> rotateRightBy 1 (ensureBits 32 <| fromHex \"96\")\n ensureBits 32 (fromHex \"4B\")\n\n >>> rotateRightBy 5 (ensureBits 32 <| fromHex \"96\")\n ensureBits 32 (fromHex \"B0000004\")\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"shiftLeftBy","comment":" Arithmetic/Logical left shift.\n\n -- LEFTSHIFT 00010111 (decimal 23)\n -- = 00101110 (decimal 46)\n\n >>> [ 0, 0, 0, 1, 0, 1, 1, 1 ]\n ..> |> fromIntegers\n ..> |> shiftLeftBy 1\n ..> |> toIntegers\n [ 0, 0, 1, 0, 1, 1, 1, 0 ]\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"shiftRightBy","comment":" Arithmetic right shift.\n\n -- ARI-RIGHTSHIFT 10010111 (decimal 151)\n -- = 11001011 (decimal 203)\n\n >>> [ 1, 0, 0, 1, 0, 1, 1, 1 ]\n ..> |> fromIntegers\n ..> |> shiftRightBy 1\n ..> |> toIntegers\n [ 1, 1, 0, 0, 1, 0, 1, 1 ]\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"shiftRightZfBy","comment":" Logical right shift.\n\n -- LOG-RIGHTSHIFT 10010111 (decimal 151)\n -- = 00001011 (decimal 11)\n\n >>> [ 0, 0, 0, 1, 0, 1, 1, 1 ]\n ..> |> fromIntegers\n ..> |> shiftRightZfBy 1\n ..> |> toIntegers\n [ 0, 0, 0, 0, 1, 0, 1, 1 ]\n\n","type":"Basics.Int -> Binary.Bits -> Binary.Bits"},{"name":"subtract","comment":" Subtract two sets of bits from each other.\n\n -- SUBTRACT 1011\n -- 11\n -- = 1010\n\n >>> subtract\n ..> (fromIntegers [ 1, 0, 1, 1 ])\n ..> (fromIntegers [ 1, 1 ])\n fromIntegers [ 1, 0, 0, 0 ]\n\n >>> subtract\n ..> (fromIntegers [ 1, 0, 0, 0, 1 ])\n ..> (fromIntegers [ 0, 0, 1, 0, 0 ])\n fromIntegers [ 0, 1, 1, 0, 1 ]\n\n","type":"Binary.Bits -> Binary.Bits -> Binary.Bits"},{"name":"toBooleans","comment":" Convert `Bits` to a list of booleans.\n\n >>> toBooleans <| fromHex \"8\"\n [ True, False, False, False ]\n\n","type":"Binary.Bits -> List.List Basics.Bool"},{"name":"toDecimal","comment":" Convert `Bits` to a decimal.\n\n >>> toDecimal <| fromIntegers [ 1, 0, 0, 0 ]\n 8\n\n","type":"Binary.Bits -> Basics.Int"},{"name":"toHex","comment":" Convert a list of binary numbers to a hex string.\n\n >>> toHex <| fromIntegers [ 1, 0, 0, 0 ]\n \"8\"\n\n","type":"Binary.Bits -> String.String"},{"name":"toIntegers","comment":" Convert `Bits` to a list of integers.\n\n >>> toIntegers <| fromHex \"8\"\n [ 1, 0, 0, 0 ]\n\n","type":"Binary.Bits -> List.List Basics.Int"},{"name":"width","comment":" Get the amount of bits in a binary sequence.\n\n >>> fromIntegers [ 1, 0, 0, 0 ]\n ..> |> width\n 4\n\n","type":"Binary.Bits -> Basics.Int"},{"name":"xor","comment":" XOR operator.\n\n -- 0101 (decimal 5)\n -- XOR 0011 (decimal 3)\n -- = 0110 (decimal 6)\n\n >>> Binary.xor\n ..> (fromHex \"5\")\n ..> (fromHex \"3\")\n fromHex \"6\"\n\n","type":"Binary.Bits -> Binary.Bits -> Binary.Bits"}],"binops":[]}]
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "icidasset/elm-binary",
"summary": "Work with binary data.",
"license": "MIT",
"version": "1.1.0",
"version": "1.2.0",
"exposed-modules": [
"Binary"
],
Expand Down
33 changes: 31 additions & 2 deletions src/Binary.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Binary exposing
, and, or, xor, not
, shiftLeftBy, shiftRightBy, shiftRightZfBy, rotateLeftBy, rotateRightBy
, add, subtract
, concat, dropLeadingZeros, ensureBits, makeIsometric
, chunksOf, concat, dropLeadingZeros, ensureBits, makeIsometric, width
)

{-|
Expand Down Expand Up @@ -34,7 +34,7 @@ module Binary exposing
# Utilities
@docs concat, dropLeadingZeros, ensureBits, makeIsometric
@docs chunksOf, concat, dropLeadingZeros, ensureBits, makeIsometric, width
-}

Expand Down Expand Up @@ -545,6 +545,23 @@ subtract_ { bits, minuend, subtrahend } =
-- UTILITIES


{-| Split the binary sequence in multiple chunks.
>>> fromIntegers [ 1, 0, 0, 0, 1, 0, 1, 0 ]
..> |> chunksOf 4
..> |> List.map toIntegers
[ [ 1, 0, 0, 0 ]
, [ 1, 0, 1, 0 ]
]
-}
chunksOf : Int -> Bits -> List Bits
chunksOf n (Bits bits) =
bits
|> List.greedyGroupsOf n
|> List.map Bits


{-| Concat multiple binary sequences.
>>> [ fromIntegers [ 1, 0, 0, 0 ]
Expand Down Expand Up @@ -608,6 +625,18 @@ makeIsometric (Bits a) (Bits b) =
)


{-| Get the amount of bits in a binary sequence.
>>> fromIntegers [ 1, 0, 0, 0 ]
..> |> width
4
-}
width : Bits -> Int
width (Bits bits) =
List.length bits



------------------------------------------------------------------------
-- PRIVATE
Expand Down

0 comments on commit 615a675

Please sign in to comment.