Bitwise

Core

Library for bitwise operations.

Values are treated as 32-bit signed integers, matching JavaScript Bitwise so dual-target code stays consistent.

Basic Operations

@docs and, or, xor, complement

Bit Shifts

@docs shiftLeftBy, shiftRightBy, shiftRightZfBy

Values

and

and : Int -> Int -> Int

Bitwise AND

Examples

and 5 3 == 1
and 7 1 == 1

or

or : Int -> Int -> Int

Bitwise OR

Examples

or 5 3 == 7
or 4 1 == 5

xor

xor : Int -> Int -> Int

Bitwise XOR

Examples

xor 5 3 == 6
xor 7 1 == 6

complement

complement : Int -> Int

Flip each bit individually, often called bitwise NOT

Examples

complement 0 == -1
complement -1 == 0

shiftLeftBy

shiftLeftBy : Int -> Int -> Int

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.

Examples

shiftLeftBy 1 5 == 10
shiftLeftBy 5 1 == 32

shiftRightBy

shiftRightBy : Int -> Int -> Int

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.

Examples

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

shiftRightZfBy : Int -> Int -> Int

Shift bits to the right by a given offset, filling new bits with zeros.

Examples

shiftRightZfBy 1 32 == 16
shiftRightZfBy 2 32 == 8
shiftRightZfBy 1 -32 == 2147483632

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