2013-10-29 18:50:55 +00:00
|
|
|
module Bitwise where
|
2013-10-29 17:47:37 +00:00
|
|
|
|
|
|
|
{-| Library for [bitwise operations](http://en.wikipedia.org/wiki/Bitwise_operation).
|
|
|
|
|
2013-10-29 18:06:55 +00:00
|
|
|
# Basic Operations
|
|
|
|
|
|
|
|
@docs and, or, xor, complement
|
|
|
|
|
|
|
|
# Bit Shifts
|
|
|
|
|
|
|
|
@docs shiftLeft, shiftRight, shiftRightLogical
|
2013-10-29 17:47:37 +00:00
|
|
|
-}
|
|
|
|
|
2013-10-29 18:50:55 +00:00
|
|
|
import Native.Bitwise
|
2013-10-29 17:47:37 +00:00
|
|
|
|
2013-12-17 23:20:39 +00:00
|
|
|
{-| Bitwise AND
|
2013-10-29 17:47:37 +00:00
|
|
|
-}
|
|
|
|
and : Int -> Int -> Int
|
2013-10-29 18:50:55 +00:00
|
|
|
and = Native.Bitwise.and
|
2013-10-29 17:47:37 +00:00
|
|
|
|
2013-12-17 23:20:39 +00:00
|
|
|
{-| Bitwise OR
|
2013-10-29 17:47:37 +00:00
|
|
|
-}
|
|
|
|
or : Int -> Int -> Int
|
2013-10-29 18:50:55 +00:00
|
|
|
or = Native.Bitwise.or
|
2013-10-29 17:47:37 +00:00
|
|
|
|
2013-12-17 23:20:39 +00:00
|
|
|
{-| Bitwise XOR
|
2013-10-29 17:47:37 +00:00
|
|
|
-}
|
|
|
|
xor : Int -> Int -> Int
|
2013-10-29 18:50:55 +00:00
|
|
|
xor = Native.Bitwise.xor
|
2013-10-29 17:47:37 +00:00
|
|
|
|
2013-12-17 23:20:39 +00:00
|
|
|
{-| Flip each bit individually, often called bitwise NOT
|
2013-10-29 17:47:37 +00:00
|
|
|
-}
|
|
|
|
complement : Int -> Int
|
2013-10-29 18:50:55 +00:00
|
|
|
complement = Native.Bitwise.complement
|
2013-10-29 17:47:37 +00:00
|
|
|
|
|
|
|
{-| Shift bits to the left by a given offset, filling new bits with zeros.
|
2013-10-29 18:06:55 +00:00
|
|
|
This can be used to multiply numbers by powers of two.
|
|
|
|
|
|
|
|
8 `shiftLeft` 1 == 16
|
|
|
|
8 `shiftLeft` 2 == 32
|
2013-10-29 17:47:37 +00:00
|
|
|
-}
|
|
|
|
shiftLeft : Int -> Int -> Int
|
2013-10-29 18:50:55 +00:00
|
|
|
shiftLeft = Native.Bitwise.shiftLeft
|
2013-10-29 17:47:37 +00:00
|
|
|
|
|
|
|
{-| Shift bits to the right by a given offset, filling new bits with
|
2013-10-29 18:06:55 +00:00
|
|
|
whatever is the topmost bit. This can be used to divide numbers by powers of two.
|
|
|
|
|
|
|
|
32 `shiftRight` 1 == 16
|
|
|
|
32 `shiftRight` 2 == 8
|
|
|
|
-32 `shiftRight` 1 == -16
|
|
|
|
|
|
|
|
This is called an [arithmatic right
|
2013-10-29 17:47:37 +00:00
|
|
|
shift](http://en.wikipedia.org/wiki/Bitwise_operation#Arithmetic_shift),
|
2013-12-17 23:20:39 +00:00
|
|
|
often written (>>), and sometimes called a sign-propagating
|
2013-10-29 17:47:37 +00:00
|
|
|
right shift because it fills empty spots with copies of the highest bit.
|
|
|
|
-}
|
|
|
|
shiftRight : Int -> Int -> Int
|
2013-10-29 18:50:55 +00:00
|
|
|
shiftRight = Native.Bitwise.shiftRightArithmatic
|
2013-10-29 17:47:37 +00:00
|
|
|
|
|
|
|
{-| Shift bits to the right by a given offset, filling new bits with
|
2013-10-29 18:06:55 +00:00
|
|
|
zeros.
|
|
|
|
|
|
|
|
32 `shiftRightLogical` 1 == 16
|
|
|
|
32 `shiftRightLogical` 2 == 8
|
|
|
|
-32 `shiftRightLogical` 1 == 2147483632
|
|
|
|
|
|
|
|
This is called an [logical right
|
2013-10-29 17:47:37 +00:00
|
|
|
shift](http://en.wikipedia.org/wiki/Bitwise_operation#Logical_shift), often written (>>>),
|
2013-12-17 23:20:39 +00:00
|
|
|
and sometimes called a zero-fill right shift because it fills empty spots
|
2013-10-29 17:47:37 +00:00
|
|
|
with zeros.
|
|
|
|
-}
|
|
|
|
shiftRightLogical : Int -> Int -> Int
|
2013-10-29 18:50:55 +00:00
|
|
|
shiftRightLogical = Native.Bitwise.shiftRightLogical
|
2013-10-29 17:47:37 +00:00
|
|
|
|