# Pfx.bits

You're seeing just the function `bits`, go back to Pfx module for more information.

# bits(pfx, ranges)

View Source

## Specs

`bits(prefix(), [{integer(), integer()}]) :: bitstring()`

Returns the concatenation of 1 or more series of bits of the given `pfx`.

## Examples

``````iex> bits("1.2.3.4", [{0, 8}, {-1, -8}])
<<1, 4>>

iex> bits("1.2.3.0/24", [{0, 8}, {-1, -8}])
<<1, 0>>

iex> bits({1, 2, 3, 4}, [{0, 8}, {-1, -8}])
<<1, 4>>

iex> bits({{1, 2, 3, 0}, 24}, [{0,8}, {-1, -8}])
<<1, 0>>

iex> bits(%Pfx{bits: <<1, 2, 3, 4>>, maxlen: 32}, [{0,8}, {-1, -8}])
<<1, 4>>``````

# bits(prefix, position, length)

View Source

## Specs

`bits(prefix(), integer(), integer()) :: bitstring()`

Returns `length` bits, starting at `position` for given `pfx`.

Negative `position`'s are relative to the end of the `pfx.bits` bitstring, while negative `length` will collect bits going left instead of to the right. Note that the bit at given `position` is always included in the result regardless of direction. Finally, a `length` of `0` results in an empty bitstring.

## Examples

``````# last two bytes
iex> bits("128.0.128.1", 16, 16)
<<128, 1>>

iex> bits({128, 0, 128, 1}, 16, 16) # same
<<128, 1>>

iex> bits({128, 0, 128, 1}, 31, -16) # same
<<128, 1>>

iex> bits({{128, 0, 128, 1}, 32}, 31, -16) # same
<<128, 1>>

# first byte
iex> bits(%Pfx{bits: <<128, 0, 0, 1>>, maxlen: 32}, 0, 8)
<<128>>

# same as
iex> bits(%Pfx{bits: <<128, 0, 0, 1>>, maxlen: 32}, 7, -8)
<<128>>

# missing bits are filled in as `0`
iex> x = new(<<128>>, 32)
iex> bits(x, 0, 32)
<<128, 0, 0, 0>>

iex> x = new(<<128>>, 32)
iex> bits(x, 0, 16)
<<128, 0>>

iex> x = new(<<128>>, 32)
iex> bits(x, 15, -16)
<<128, 0>>

# the last 5 bits
iex> x = new(<<255>>, 32)
iex> bits(x, 7, -5)
<<0b11111::size(5)>>``````