# Elixir v1.10.3 Bitwise View Source

A set of functions that perform calculations on bits.

All bitwise functions work only on integers; otherwise an `ArithmeticError` is raised.

The functions in this module come in two flavors: named or operators. For example:

    iex> use Bitwise
    iex> bnot(1) # named
    -2
    iex> 1 &&& 1 # operator
    1

If you prefer to use only operators or skip them, you can pass the following options:

- `:only_operators` - includes only operators
- `:skip_operators` - skips operators

For example:

    iex> use Bitwise, only_operators: true
    iex> 1 &&& 1
    1

When invoked with no options, `use Bitwise` is equivalent to `import Bitwise`.

All bitwise functions can be used in guards:

    iex> odd? = fn
    ...>   int when Bitwise.band(int, 1) == 1 -> true
    ...>   _ -> false
    ...> end
    iex> odd?.(1)
    true

All functions in this module are inlined by the compiler.

# Link to this section Summary

## Guards

left &&& right

Infix operator; calculates the bitwise AND of its arguments.

left <<< right

Infix operator; calculates the result of an arithmetic left bitshift.

left >>> right

Infix operator; calculates the result of an arithmetic right bitshift.

left ^^^ right

Infix operator; calculates the bitwise XOR of its arguments.

left ||| right

Infix operator; calculates the bitwise OR of its arguments.

~~~expr

Prefix (unary) operator; calculates the bitwise NOT of its argument.

band(left, right)

Calculates the bitwise AND of its arguments.

bnot(expr)

Calculates the bitwise NOT of its argument.

bor(left, right)

Calculates the bitwise OR of its arguments.

bsl(left, right)

Calculates the result of an arithmetic left bitshift.

bsr(left, right)

Calculates the result of an arithmetic right bitshift.

bxor(left, right)

Calculates the bitwise XOR of its arguments.

# Link to this section Guards

Link to this function

# left &&& right

View Source

## Specs

    integer() &&& integer() :: integer()

Infix operator; calculates the bitwise AND of its arguments.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> 9 &&& 3
    1

Link to this function

# left <<< right

View Source

## Specs

    integer() <<< integer() :: integer()

Infix operator; calculates the result of an arithmetic left bitshift.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> 1 <<< 2
    4

    iex> 1 <<< -2
    0

    iex> -1 <<< 2
    -4

    iex> -1 <<< -2
    -1

Link to this function

# left >>> right

View Source

## Specs

    integer() >>> integer() :: integer()

Infix operator; calculates the result of an arithmetic right bitshift.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> 1 >>> 2
    0

    iex> 1 >>> -2
    4

    iex> -1 >>> 2
    -1

    iex> -1 >>> -2
    -4

Link to this function

# left ^^^ right

View Source

## Specs

    integer() ^^^ integer() :: integer()

Infix operator; calculates the bitwise XOR of its arguments.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> 9 ^^^ 3
    10

Link to this function

# left ||| right

View Source

## Specs

    integer() ||| integer() :: integer()

Infix operator; calculates the bitwise OR of its arguments.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> 9 ||| 3
    11

Link to this function

# ~~~expr

View Source

## Specs

    ~~~integer() :: integer()

Prefix (unary) operator; calculates the bitwise NOT of its argument.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> ~~~2
    -3

    iex> ~~~2 &&& 3
    1

Link to this function

# band(left, right)

View Source

## Specs

    band(integer(), integer()) :: integer()

Calculates the bitwise AND of its arguments.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> band(9, 3)
    1

Link to this function

# bnot(expr)

View Source

## Specs

    bnot(integer()) :: integer()

Calculates the bitwise NOT of its argument.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> bnot(2)
    -3

    iex> bnot(2) &&& 3
    1

Link to this function

# bor(left, right)

View Source

## Specs

    bor(integer(), integer()) :: integer()

Calculates the bitwise OR of its arguments.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> bor(9, 3)
    11

Link to this function

# bsl(left, right)

View Source

## Specs

    bsl(integer(), integer()) :: integer()

Calculates the result of an arithmetic left bitshift.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> bsl(1, 2)
    4

    iex> bsl(1, -2)
    0

    iex> bsl(-1, 2)
    -4

    iex> bsl(-1, -2)
    -1

Link to this function

# bsr(left, right)

View Source

## Specs

    bsr(integer(), integer()) :: integer()

Calculates the result of an arithmetic right bitshift.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> bsr(1, 2)
    0

    iex> bsr(1, -2)
    4

    iex> bsr(-1, 2)
    -1

    iex> bsr(-1, -2)
    -4

Link to this function

# bxor(left, right)

View Source

## Specs

    bxor(integer(), integer()) :: integer()

Calculates the bitwise XOR of its arguments.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> bxor(9, 3)
    10