elixir v1.7.0-rc.0 Bitwise

A set of macros that perform calculations on bits.

The macros 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 macros can be used in guards:

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

Link to this section Summary

Functions

Infix operator; calculates the bitwise AND of its arguments

Infix operator; calculates the result of an arithmetic left bitshift

Infix operator; calculates the result of an arithmetic right bitshift

Infix operator; calculates the bitwise XOR of its arguments

Calculates the bitwise AND of its arguments

Calculates the bitwise NOT of its argument

Calculates the bitwise OR of its arguments

Calculates the result of an arithmetic left bitshift

Calculates the result of an arithmetic right bitshift

Calculates the bitwise XOR of its arguments

Infix operator; calculates the bitwise OR of its arguments

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

Link to this section Functions

Link to this macro left &&& right (macro)

Infix operator; calculates the bitwise AND of its arguments.

iex> 9 &&& 3
1
Link to this macro left <<< right (macro)

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

iex> 1 <<< 2
4
iex> 1 <<< -2
0
iex> -1 <<< 2
-4
iex> -1 <<< -2
-1
Link to this macro left >>> right (macro)

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

iex> 1 >>> 2
0
iex> 1 >>> -2
4
iex> -1 >>> 2
-1
iex> -1 >>> -2
-4
Link to this macro left ^^^ right (macro)

Infix operator; calculates the bitwise XOR of its arguments.

iex> 9 ^^^ 3
10
Link to this macro band(left, right) (macro)

Calculates the bitwise AND of its arguments.

iex> band(9, 3)
1
Link to this macro bnot(expr) (macro)

Calculates the bitwise NOT of its argument.

iex> bnot(2)
-3
iex> bnot(2) &&& 3
1
Link to this macro bor(left, right) (macro)

Calculates the bitwise OR of its arguments.

iex> bor(9, 3)
11
Link to this macro bsl(left, right) (macro)

Calculates the result of an arithmetic left bitshift.

iex> bsl(1, 2)
4
iex> bsl(1, -2)
0
iex> bsl(-1, 2)
-4
iex> bsl(-1, -2)
-1
Link to this macro bsr(left, right) (macro)

Calculates the result of an arithmetic right bitshift.

iex> bsr(1, 2)
0
iex> bsr(1, -2)
4
iex> bsr(-1, 2)
-1
iex> bsr(-1, -2)
-4
Link to this macro bxor(left, right) (macro)

Calculates the bitwise XOR of its arguments.

iex> bxor(9, 3)
10
Link to this macro left ||| right (macro)

Infix operator; calculates the bitwise OR of its arguments.

iex> 9 ||| 3
11
Link to this macro ~~~expr (macro)

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

iex> ~~~2
-3
iex> ~~~2 &&& 3
1