on e3c3d21 help sign in
elk/libraries/Bitwise.elm
75 lines 2.0 kB history raw
2module Bitwise where
4{-| Library for [bitwise operations](http://en.wikipedia.org/wiki/Bitwise_operation).
6# Basic Operations
8@docs and, or, xor, complement
10# Bit Shifts
12@docs shiftLeft, shiftRight, shiftRightLogical
13-}
15import Native.Bitwise
17{-| Bitwise “and”
18-}
19and : Int -> Int -> Int
20and = Native.Bitwise.and
22{-| Bitwise “or”
23-}
24or : Int -> Int -> Int
25or = Native.Bitwise.or
27{-| Bitwise “xor”
28-}
29xor : Int -> Int -> Int
30xor = Native.Bitwise.xor
32{-| Flip each bit individually, often called “bitwise not”
33-}
34complement : Int -> Int
35complement = Native.Bitwise.complement
37{-| Shift bits to the left by a given offset, filling new bits with zeros.
38This can be used to multiply numbers by powers of two.
40 8 `shiftLeft` 1 == 16
41 8 `shiftLeft` 2 == 32
42-}
43shiftLeft : Int -> Int -> Int
44shiftLeft = Native.Bitwise.shiftLeft
46{-| Shift bits to the right by a given offset, filling new bits with
47whatever is the topmost bit. This can be used to divide numbers by powers of two.
49 32 `shiftRight` 1 == 16
50 32 `shiftRight` 2 == 8
51 -32 `shiftRight` 1 == -16
53This is called an [arithmatic right
54shift](http://en.wikipedia.org/wiki/Bitwise_operation#Arithmetic_shift),
55often written (>>), and sometimes called a “sign-propagating”
56right shift because it fills empty spots with copies of the highest bit.
57-}
58shiftRight : Int -> Int -> Int
59shiftRight = Native.Bitwise.shiftRightArithmatic
61{-| Shift bits to the right by a given offset, filling new bits with
62zeros.
64 32 `shiftRightLogical` 1 == 16
65 32 `shiftRightLogical` 2 == 8
66 -32 `shiftRightLogical` 1 == 2147483632
68This is called an [logical right
69shift](http://en.wikipedia.org/wiki/Bitwise_operation#Logical_shift), often written (>>>),
70and sometimes called a “zero-fill” right shift because it fills empty spots
71with zeros.
72-}
73shiftRightLogical : Int -> Int -> Int
74shiftRightLogical = Native.Bitwise.shiftRightLogical