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 operatorsFor 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.
Bitwise AND operator.
Arithmetic left bitshift operator.
Arithmetic right bitshift operator.
Bitwise XOR operator.
Bitwise OR operator.
Bitwise NOT unary operator.
Calculates the bitwise AND of its arguments.
Calculates the bitwise NOT of the 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.
integer() &&& integer() :: integer()
Bitwise AND operator.
Calculates the bitwise AND of its arguments.
Allowed in guard tests. Inlined by the compiler.
iex> 9 &&& 3 1
integer() <<< integer() :: integer()
Arithmetic left bitshift operator.
Calculates the result of an arithmetic left bitshift.
Allowed in guard tests. Inlined by the compiler.
iex> 1 <<< 2 4 iex> 1 <<< -2 0 iex> -1 <<< 2 -4 iex> -1 <<< -2 -1
integer() >>> integer() :: integer()
Arithmetic right bitshift operator.
Calculates the result of an arithmetic right bitshift.
Allowed in guard tests. Inlined by the compiler.
iex> 1 >>> 2 0 iex> 1 >>> -2 4 iex> -1 >>> 2 -1 iex> -1 >>> -2 -4
integer() ^^^ integer() :: integer()
Bitwise XOR operator.
Calculates the bitwise XOR of its arguments.
Allowed in guard tests. Inlined by the compiler.
iex> 9 ^^^ 3 10
integer() ||| integer() :: integer()
Bitwise OR operator.
Calculates the bitwise OR of its arguments.
Allowed in guard tests. Inlined by the compiler.
iex> 9 ||| 3 11
~~~integer() :: integer()
Bitwise NOT unary operator.
Calculates the bitwise NOT of the argument.
Allowed in guard tests. Inlined by the compiler.
iex> ~~~2 -3 iex> ~~~2 &&& 3 1
band(integer(), integer()) :: integer()
Calculates the bitwise AND of its arguments.
Allowed in guard tests. Inlined by the compiler.
iex> band(9, 3) 1
bnot(integer()) :: integer()
Calculates the bitwise NOT of the argument.
Allowed in guard tests. Inlined by the compiler.
iex> bnot(2) -3 iex> bnot(2) &&& 3 1
bor(integer(), integer()) :: integer()
Calculates the bitwise OR of its arguments.
Allowed in guard tests. Inlined by the compiler.
iex> bor(9, 3) 11
bsl(integer(), integer()) :: integer()
Calculates the result of an arithmetic left bitshift.
Allowed in guard tests. Inlined by the compiler.
iex> bsl(1, 2) 4 iex> bsl(1, -2) 0 iex> bsl(-1, 2) -4 iex> bsl(-1, -2) -1
bsr(integer(), integer()) :: integer()
Calculates the result of an arithmetic right bitshift.
Allowed in guard tests. Inlined by the compiler.
iex> bsr(1, 2) 0 iex> bsr(1, -2) 4 iex> bsr(-1, 2) -1 iex> bsr(-1, -2) -4
bxor(integer(), integer()) :: integer()
Calculates the bitwise XOR of its arguments.
Allowed in guard tests. Inlined by the compiler.
iex> bxor(9, 3) 10
© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.11.2/Bitwise.html