on master help sign in
Make explanations a bit more helpful
abf05d7Evan Czaplicki
libraries/Bits.elm+29−7
@@ -3,38 +3,54 @@ module Bits where
3
3
4{-| Library for [bitwise operations](http://en.wikipedia.org/wiki/Bitwise_operation).
4{-| Library for [bitwise operations](http://en.wikipedia.org/wiki/Bitwise_operation).
5
5
6+# Basic Operations
7+
8+@docs and, or, xor, complement
9+
10+# Bit Shifts
11+
6−@docs and, or, xor, complement, shiftLeft, shiftRight, shiftRightLogical
12+@docs shiftLeft, shiftRight, shiftRightLogical
7-}
13-}
8
14
9import Native.Bits
15import Native.Bits
10
16
11−{-| Performs the AND operation on each pair of bits.
17+{-| Bitwise “and”
12-}
18-}
13and : Int -> Int -> Int
19and : Int -> Int -> Int
14and = Native.Bits.and
20and = Native.Bits.and
15
21
16−{-| Performs the OR operation on each pair of bits.
22+{-| Bitwise “or”
17-}
23-}
18or : Int -> Int -> Int
24or : Int -> Int -> Int
19or = Native.Bits.or
25or = Native.Bits.or
20
26
21−{-| Performs the XOR operation on each pair of bits.
27+{-| Bitwise “xor”
22-}
28-}
23xor : Int -> Int -> Int
29xor : Int -> Int -> Int
24xor = Native.Bits.xor
30xor = Native.Bits.xor
25
31
26−{-| Performs the NOT operation on each bit.
32+{-| Flip each bit individually, often called “bitwise not”
27-}
33-}
28complement : Int -> Int
34complement : Int -> Int
29complement = Native.Bits.complement
35complement = Native.Bits.complement
30
36
31{-| Shift bits to the left by a given offset, filling new bits with zeros.
37{-| Shift bits to the left by a given offset, filling new bits with zeros.
38+This can be used to multiply numbers by powers of two.
39+
40+ 8 `shiftLeft` 1 == 16
41+ 8 `shiftLeft` 2 == 32
32-}
42-}
33shiftLeft : Int -> Int -> Int
43shiftLeft : Int -> Int -> Int
34shiftLeft = Native.Bits.shiftLeft
44shiftLeft = Native.Bits.shiftLeft
35
45
36{-| Shift bits to the right by a given offset, filling new bits with
46{-| Shift bits to the right by a given offset, filling new bits with
47+whatever is the topmost bit. This can be used to divide numbers by powers of two.
48+
49+ 32 `shiftRight` 1 == 16
50+ 32 `shiftRight` 2 == 8
51+ -32 `shiftRight` 1 == -16
52+
37−whatever is the topmost bit. This is an [arithmatic right
53+This is called an [arithmatic right
38shift](http://en.wikipedia.org/wiki/Bitwise_operation#Arithmetic_shift),
54shift](http://en.wikipedia.org/wiki/Bitwise_operation#Arithmetic_shift),
39often written (>>), and sometimes called a “sign-propagating”
55often written (>>), and sometimes called a “sign-propagating”
40right shift because it fills empty spots with copies of the highest bit.
56right shift because it fills empty spots with copies of the highest bit.
@@ -43,7 +59,13 @@ shiftRight : Int -> Int -> Int
43shiftRight = Native.Bits.shiftRightArithmatic
59shiftRight = Native.Bits.shiftRightArithmatic
44
60
45{-| Shift bits to the right by a given offset, filling new bits with
61{-| Shift bits to the right by a given offset, filling new bits with
62+zeros.
63+
64+ 32 `shiftRightLogical` 1 == 16
65+ 32 `shiftRightLogical` 2 == 8
66+ -32 `shiftRightLogical` 1 == 2147483632
67+
46−zeros. This is an [logical right
68+This is called an [logical right
47shift](http://en.wikipedia.org/wiki/Bitwise_operation#Logical_shift), often written (>>>),
69shift](http://en.wikipedia.org/wiki/Bitwise_operation#Logical_shift), often written (>>>),
48and sometimes called a “zero-fill” right shift because it fills empty spots
70and sometimes called a “zero-fill” right shift because it fills empty spots
49with zeros.
71with zeros.