-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathBitwise.elm
93 lines (65 loc) · 2.02 KB
/
Bitwise.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module Bitwise exposing
( and, or, xor, complement
, shiftLeftBy, shiftRightBy, shiftRightZfBy
)
{-| Library for [bitwise operations](https://en.wikipedia.org/wiki/Bitwise_operation).
# Basic Operations
@docs and, or, xor, complement
# Bit Shifts
@docs shiftLeftBy, shiftRightBy, shiftRightZfBy
-}
import Basics exposing (Int)
import Elm.Kernel.Bitwise
{-| Bitwise AND
-}
and : Int -> Int -> Int
and =
Elm.Kernel.Bitwise.and
{-| Bitwise OR
-}
or : Int -> Int -> Int
or =
Elm.Kernel.Bitwise.or
{-| Bitwise XOR
-}
xor : Int -> Int -> Int
xor =
Elm.Kernel.Bitwise.xor
{-| Flip each bit individually, often called bitwise NOT
-}
complement : Int -> Int
complement =
Elm.Kernel.Bitwise.complement
{-| Shift bits to the left by a given offset, filling new bits with zeros.
This can be used to multiply numbers by powers of two.
shiftLeftBy 1 5 == 10
shiftLeftBy 5 1 == 32
-}
shiftLeftBy : Int -> Int -> Int
shiftLeftBy =
Elm.Kernel.Bitwise.shiftLeftBy
{-| Shift bits to the right by a given offset, filling new bits with
whatever is the topmost bit. This can be used to divide numbers by powers of two.
shiftRightBy 1 32 == 16
shiftRightBy 2 32 == 8
shiftRightBy 1 -32 == -16
This is called an [arithmetic right shift][ars], often written `>>`, and
sometimes called a sign-propagating right shift because it fills empty spots
with copies of the highest bit.
[ars]: https://en.wikipedia.org/wiki/Bitwise_operation#Arithmetic_shift
-}
shiftRightBy : Int -> Int -> Int
shiftRightBy =
Elm.Kernel.Bitwise.shiftRightBy
{-| Shift bits to the right by a given offset, filling new bits with zeros.
shiftRightZfBy 1 32 == 16
shiftRightZfBy 2 32 == 8
shiftRightZfBy 1 -32 == 2147483632
This is called an [logical right shift][lrs], often written `>>>`, and
sometimes called a zero-fill right shift because it fills empty spots with
zeros.
[lrs]: https://en.wikipedia.org/wiki/Bitwise_operation#Logical_shift
-}
shiftRightZfBy : Int -> Int -> Int
shiftRightZfBy =
Elm.Kernel.Bitwise.shiftRightZfBy