on master help sign in
Add a library for bitwise operators
2f77399Evan Czaplicki
libraries/Bits.elmnew file+53
@@ -0,0 +1,53 @@
1+
2+module Bits where
3+
4+{-| Library for [bitwise operations](http://en.wikipedia.org/wiki/Bitwise_operation).
5+
6+@docs and, or, xor, complement, shiftLeft, shiftRight, shiftRightLogical
7+-}
8+
9+import Native.Bits
10+
11+{-| Performs the AND operation on each pair of bits.
12+-}
13+and : Int -> Int -> Int
14+and = Native.Bits.and
15+
16+{-| Performs the OR operation on each pair of bits.
17+-}
18+or : Int -> Int -> Int
19+or = Native.Bits.or
20+
21+{-| Performs the XOR operation on each pair of bits.
22+-}
23+xor : Int -> Int -> Int
24+xor = Native.Bits.xor
25+
26+{-| Performs the NOT operation on each bit.
27+-}
28+complement : Int -> Int
29+complement = Native.Bits.complement
30+
31+{-| Shift bits to the left by a given offset, filling new bits with zeros.
32+-}
33+shiftLeft : Int -> Int -> Int
34+shiftLeft = Native.Bits.shiftLeft
35+
36+{-| Shift bits to the right by a given offset, filling new bits with
37+whatever is the topmost bit. This is an [arithmatic right
38+shift](http://en.wikipedia.org/wiki/Bitwise_operation#Arithmetic_shift),
39+often written (>>), and sometimes called a “sign-propagating”
40+right shift because it fills empty spots with copies of the highest bit.
41+-}
42+shiftRight : Int -> Int -> Int
43+shiftRight = Native.Bits.shiftRightArithmatic
44+
45+{-| Shift bits to the right by a given offset, filling new bits with
46+zeros. This is an [logical right
47+shift](http://en.wikipedia.org/wiki/Bitwise_operation#Logical_shift), often written (>>>),
48+and sometimes called a “zero-fill” right shift because it fills empty spots
49+with zeros.
50+-}
51+shiftRightLogical : Int -> Int -> Int
52+shiftRightLogical = Native.Bits.shiftRightLogical
53+
libraries/Native/Bits.jsnew file+25
@@ -0,0 +1,25 @@
1+Elm.Native.Bits = {};
2+Elm.Native.Bits.make = function(elm) {
3+ elm.Native = elm.Native || {};
4+ elm.Native.Bits = elm.Native.Bits || {};
5+ if (elm.Native.Bits.values) return elm.Native.Bits.values;
6+
7+ function and(a,b) { return a & b; }
8+ function or (a,b) { return a | b; }
9+ function xor(a,b) { return a ^ b; }
10+ function not(a) { return ~a; }
11+ function sll(a,offset) { return a << offset; }
12+ function sra(a,offset) { return a >> offset; }
13+ function srl(a,offset) { return a >>> offset; }
14+
15+ return elm.Native.Bits.values = {
16+ and: A2(and),
17+ or : A2(or ),
18+ xor: A2(xor),
19+ complement: not,
20+ shiftLeft : A2(sll),
21+ shiftRightArithmatic: A2(sra),
22+ shiftRightLogical : A2(srl),
23+ };
24+
25+};