nri-prelude-0.2.0.0: A Prelude inspired by the Elm programming language

Safe HaskellNone
LanguageHaskell2010

Bitwise

Contents

Description

Library for bitwise operations.

Synopsis

Basic Operations

and :: Int -> Int -> Int Source #

Bitwise AND

or :: Int -> Int -> Int Source #

Bitwise OR

xor :: Int -> Int -> Int Source #

Bitwise XOR

complement :: Int -> Int Source #

Flip each bit individually, often called bitwise NOT

Bit Shifts

shiftLeftBy :: Int -> Int -> Int Source #

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

shiftRightBy :: Int -> Int -> Int Source #

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, often written >>, and sometimes called a sign-propagating right shift because it fills empty spots with copies of the highest bit.

shiftRightZfBy :: Int -> Int -> Int Source #

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 == 9223372036854775792

This is called an logical right shift, often written >>>, and sometimes called a zero-fill right shift because it fills empty spots with zeros.