on 63243ca help sign in
elk/libraries/Bitwise.elm
74 lines 1.9 kB history raw
1module Bitwise where
3{-| Library for [bitwise operations](http://en.wikipedia.org/wiki/Bitwise_operation).
5# Basic Operations
7@docs and, or, xor, complement
9# Bit Shifts
11@docs shiftLeft, shiftRight, shiftRightLogical
12-}
14import Native.Bitwise
16{-| Bitwise AND
17-}
18and : Int -> Int -> Int
19and = Native.Bitwise.and
21{-| Bitwise OR
22-}
23or : Int -> Int -> Int
24or = Native.Bitwise.or
26{-| Bitwise XOR
27-}
28xor : Int -> Int -> Int
29xor = Native.Bitwise.xor
31{-| Flip each bit individually, often called bitwise NOT
32-}
33complement : Int -> Int
34complement = Native.Bitwise.complement
36{-| Shift bits to the left by a given offset, filling new bits with zeros.
37This can be used to multiply numbers by powers of two.
39 8 `shiftLeft` 1 == 16
40 8 `shiftLeft` 2 == 32
41-}
42shiftLeft : Int -> Int -> Int
43shiftLeft = Native.Bitwise.shiftLeft
45{-| Shift bits to the right by a given offset, filling new bits with
46whatever is the topmost bit. This can be used to divide numbers by powers of two.
48 32 `shiftRight` 1 == 16
49 32 `shiftRight` 2 == 8
50 -32 `shiftRight` 1 == -16
52This is called an [arithmatic right
53shift](http://en.wikipedia.org/wiki/Bitwise_operation#Arithmetic_shift),
54often written (>>), and sometimes called a sign-propagating
55right shift because it fills empty spots with copies of the highest bit.
56-}
57shiftRight : Int -> Int -> Int
58shiftRight = Native.Bitwise.shiftRightArithmatic
60{-| Shift bits to the right by a given offset, filling new bits with
61zeros.
63 32 `shiftRightLogical` 1 == 16
64 32 `shiftRightLogical` 2 == 8
65 -32 `shiftRightLogical` 1 == 2147483632
67This is called an [logical right
68shift](http://en.wikipedia.org/wiki/Bitwise_operation#Logical_shift), often written (>>>),
69and sometimes called a zero-fill right shift because it fills empty spots
70with zeros.
71-}
72shiftRightLogical : Int -> Int -> Int
73shiftRightLogical = Native.Bitwise.shiftRightLogical