# `Bitwise`
[🔗](https://github.com/elixir-lang/elixir/blob/v1.20.0-rc.3/lib/elixir/lib/bitwise.ex#L5)

A set of functions that perform calculations on bits.

All bitwise functions work only on integers, otherwise an
`ArithmeticError` is raised. The functions `band/2`,
`bor/2`, `bsl/2`, and `bsr/2` also have operators,
respectively: `&&&/2`, `|||/2`, `<<</2`, and `>>>/2`.

## Guards

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.

# `&&&`

```elixir
@spec integer() &amp;&amp;&amp; integer() :: integer()
```

Bitwise AND operator.

Calculates the bitwise AND of its arguments.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> 9 &&& 3
    1

# `<<<`

```elixir
@spec integer() &lt;&lt;&lt; integer() :: integer()
```

Arithmetic left bitshift 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

# `>>>`

```elixir
@spec integer() &gt;&gt;&gt; integer() :: integer()
```

Arithmetic right bitshift 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

# `band`

```elixir
@spec 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

# `bnot`

```elixir
@spec bnot(integer()) :: integer()
```

Calculates the bitwise NOT of the argument.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> bnot(2)
    -3

    iex> bnot(2) &&& 3
    1

# `bor`

```elixir
@spec 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

# `bsl`

```elixir
@spec 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

# `bsr`

```elixir
@spec 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

# `bxor`

```elixir
@spec 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

# `|||`

```elixir
@spec integer() ||| integer() :: integer()
```

Bitwise OR operator.

Calculates the bitwise OR of its arguments.

Allowed in guard tests. Inlined by the compiler.

## Examples

    iex> 9 ||| 3
    11

---

*Consult [api-reference.md](api-reference.md) for complete listing*
