Trait malachite_base::num::logic::traits::BitBlockAccess
source · [−]pub trait BitBlockAccess: Sized {
type Bits;
fn get_bits(&self, start: u64, end: u64) -> Self::Bits;
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits);
fn get_bits_owned(self, start: u64, end: u64) -> Self::Bits { ... }
}
Expand description
Defines functions that access or modify blocks of adjacent bits in a number.
Required Associated Types
Required Methods
Extracts a block of bits whose first index is start
and last index is end - 1
.
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Assigns the least-significant end - start
bits of bits
to bits start
through
end - 1
(inclusive) of self
.
Provided Methods
fn get_bits_owned(self, start: u64, end: u64) -> Self::Bits
fn get_bits_owned(self, start: u64, end: u64) -> Self::Bits
Extracts a block of bits whose first index is start
and last index is end - 1
, taking
ownership of self
.
Worst-case complexity
For the default implementation, same as get_bits
.
Panics
For the default implementation, ee panics for get_bits
.
Examples
use malachite_base::num::logic::traits::BitBlockAccess;
assert_eq!((-0x5433i16).get_bits_owned(4, 8), 0xc);
assert_eq!((-0x5433i16).get_bits_owned(5, 9), 14);
assert_eq!((-0x5433i16).get_bits_owned(5, 5), 0);
assert_eq!((-0x5433i16).get_bits_owned(100, 104), 0xf);
Implementations on Foreign Types
sourceimpl BitBlockAccess for u8
impl BitBlockAccess for u8
sourcefn get_bits(&self, start: u64, end: u64) -> Self
fn get_bits(&self, start: u64, end: u64) -> Self
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The block of bits has the same type as the input. If end
is greater than the
type’s width, the high bits of the result are all 0.
Let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Panics if start < end
.
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The block of bits has the same type as the input. If bits
has fewer bits than
end - start
, the high bits are interpreted as 0. If end
is greater than the
type’s width, the high bits of bits
must be 0.
Let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p$.
Then $$ n \gets \sum_{i=0}^{W-1} 2^{c_i}, $$ where $$ \{c_0, c_1, c_2, \ldots, c_ {W-1}\} = \{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots, b_ {W-1}\}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
, or if end > W
and bits
W - start
through end - start
of bits
are nonzero.
Examples
See here.
type Bits = u8
sourceimpl BitBlockAccess for u16
impl BitBlockAccess for u16
sourcefn get_bits(&self, start: u64, end: u64) -> Self
fn get_bits(&self, start: u64, end: u64) -> Self
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The block of bits has the same type as the input. If end
is greater than the
type’s width, the high bits of the result are all 0.
Let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Panics if start < end
.
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The block of bits has the same type as the input. If bits
has fewer bits than
end - start
, the high bits are interpreted as 0. If end
is greater than the
type’s width, the high bits of bits
must be 0.
Let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p$.
Then $$ n \gets \sum_{i=0}^{W-1} 2^{c_i}, $$ where $$ \{c_0, c_1, c_2, \ldots, c_ {W-1}\} = \{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots, b_ {W-1}\}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
, or if end > W
and bits
W - start
through end - start
of bits
are nonzero.
Examples
See here.
type Bits = u16
sourceimpl BitBlockAccess for u32
impl BitBlockAccess for u32
sourcefn get_bits(&self, start: u64, end: u64) -> Self
fn get_bits(&self, start: u64, end: u64) -> Self
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The block of bits has the same type as the input. If end
is greater than the
type’s width, the high bits of the result are all 0.
Let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Panics if start < end
.
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The block of bits has the same type as the input. If bits
has fewer bits than
end - start
, the high bits are interpreted as 0. If end
is greater than the
type’s width, the high bits of bits
must be 0.
Let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p$.
Then $$ n \gets \sum_{i=0}^{W-1} 2^{c_i}, $$ where $$ \{c_0, c_1, c_2, \ldots, c_ {W-1}\} = \{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots, b_ {W-1}\}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
, or if end > W
and bits
W - start
through end - start
of bits
are nonzero.
Examples
See here.
type Bits = u32
sourceimpl BitBlockAccess for u64
impl BitBlockAccess for u64
sourcefn get_bits(&self, start: u64, end: u64) -> Self
fn get_bits(&self, start: u64, end: u64) -> Self
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The block of bits has the same type as the input. If end
is greater than the
type’s width, the high bits of the result are all 0.
Let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Panics if start < end
.
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The block of bits has the same type as the input. If bits
has fewer bits than
end - start
, the high bits are interpreted as 0. If end
is greater than the
type’s width, the high bits of bits
must be 0.
Let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p$.
Then $$ n \gets \sum_{i=0}^{W-1} 2^{c_i}, $$ where $$ \{c_0, c_1, c_2, \ldots, c_ {W-1}\} = \{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots, b_ {W-1}\}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
, or if end > W
and bits
W - start
through end - start
of bits
are nonzero.
Examples
See here.
type Bits = u64
sourceimpl BitBlockAccess for u128
impl BitBlockAccess for u128
sourcefn get_bits(&self, start: u64, end: u64) -> Self
fn get_bits(&self, start: u64, end: u64) -> Self
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The block of bits has the same type as the input. If end
is greater than the
type’s width, the high bits of the result are all 0.
Let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Panics if start < end
.
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The block of bits has the same type as the input. If bits
has fewer bits than
end - start
, the high bits are interpreted as 0. If end
is greater than the
type’s width, the high bits of bits
must be 0.
Let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p$.
Then $$ n \gets \sum_{i=0}^{W-1} 2^{c_i}, $$ where $$ \{c_0, c_1, c_2, \ldots, c_ {W-1}\} = \{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots, b_ {W-1}\}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
, or if end > W
and bits
W - start
through end - start
of bits
are nonzero.
Examples
See here.
type Bits = u128
sourceimpl BitBlockAccess for usize
impl BitBlockAccess for usize
sourcefn get_bits(&self, start: u64, end: u64) -> Self
fn get_bits(&self, start: u64, end: u64) -> Self
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The block of bits has the same type as the input. If end
is greater than the
type’s width, the high bits of the result are all 0.
Let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Panics if start < end
.
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The block of bits has the same type as the input. If bits
has fewer bits than
end - start
, the high bits are interpreted as 0. If end
is greater than the
type’s width, the high bits of bits
must be 0.
Let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p$.
Then $$ n \gets \sum_{i=0}^{W-1} 2^{c_i}, $$ where $$ \{c_0, c_1, c_2, \ldots, c_ {W-1}\} = \{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots, b_ {W-1}\}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
, or if end > W
and bits
W - start
through end - start
of bits
are nonzero.
Examples
See here.
type Bits = usize
sourceimpl BitBlockAccess for i8
impl BitBlockAccess for i8
sourcefn get_bits(&self, start: u64, end: u64) -> Self::Bits
fn get_bits(&self, start: u64, end: u64) -> Self::Bits
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The type of the block of bits is the unsigned version of the input type. If end
is
greater than the type’s width, the high bits of the result are all 0, or all 1,
depending on the input value’s sign; and if the input is negative and end - start
is greater than the type’s width, the function panics.
If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
If $n < 0$, let $$ 2^W + n = \sum_{i=0}^{W-1} 2^{b_i}, $$ where
- $W$ is the type’s width
- for all $i$, $b_i\in \{0, 1\}$, and $b_i = 1$ for $i \geq W$. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
or (self < 0
and
end - start > W
).
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The type of the block of bits is the unsigned version of the input type. If bits
has fewer bits than end - start
, the high bits are interpreted as 0 or 1,
depending on the sign of self
. If end
is greater than the type’s width, the high
bits of bits
must be 0 or 1, depending on the sign of self
.
The sign of self
remains unchanged, since only a finite number of bits are changed
and the sign is determined by the implied infinite prefix of bits.
If $n \geq 0$ and $j \neq W - 1$, let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p - 1$. Then
$$
n \gets \sum_{i=0}^{W-1} 2^{c_i},
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
If $n < 0$ or $j = W - 1$, let
$$
2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 1$ for all $i \geq W + p - 1$. Then
$$
f(n, p, q, m) = \left ( \sum_{i=0}^{W-1} 2^{c_i} \right ) - 2^W,
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width Panics if start < end
, or if end >= W
and bits
W - start
through end - start
of bits
are not equal to the original sign bit
of self
.
Examples
See here.
type Bits = u8
sourceimpl BitBlockAccess for i16
impl BitBlockAccess for i16
sourcefn get_bits(&self, start: u64, end: u64) -> Self::Bits
fn get_bits(&self, start: u64, end: u64) -> Self::Bits
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The type of the block of bits is the unsigned version of the input type. If end
is
greater than the type’s width, the high bits of the result are all 0, or all 1,
depending on the input value’s sign; and if the input is negative and end - start
is greater than the type’s width, the function panics.
If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
If $n < 0$, let $$ 2^W + n = \sum_{i=0}^{W-1} 2^{b_i}, $$ where
- $W$ is the type’s width
- for all $i$, $b_i\in \{0, 1\}$, and $b_i = 1$ for $i \geq W$. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
or (self < 0
and
end - start > W
).
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The type of the block of bits is the unsigned version of the input type. If bits
has fewer bits than end - start
, the high bits are interpreted as 0 or 1,
depending on the sign of self
. If end
is greater than the type’s width, the high
bits of bits
must be 0 or 1, depending on the sign of self
.
The sign of self
remains unchanged, since only a finite number of bits are changed
and the sign is determined by the implied infinite prefix of bits.
If $n \geq 0$ and $j \neq W - 1$, let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p - 1$. Then
$$
n \gets \sum_{i=0}^{W-1} 2^{c_i},
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
If $n < 0$ or $j = W - 1$, let
$$
2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 1$ for all $i \geq W + p - 1$. Then
$$
f(n, p, q, m) = \left ( \sum_{i=0}^{W-1} 2^{c_i} \right ) - 2^W,
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width Panics if start < end
, or if end >= W
and bits
W - start
through end - start
of bits
are not equal to the original sign bit
of self
.
Examples
See here.
type Bits = u16
sourceimpl BitBlockAccess for i32
impl BitBlockAccess for i32
sourcefn get_bits(&self, start: u64, end: u64) -> Self::Bits
fn get_bits(&self, start: u64, end: u64) -> Self::Bits
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The type of the block of bits is the unsigned version of the input type. If end
is
greater than the type’s width, the high bits of the result are all 0, or all 1,
depending on the input value’s sign; and if the input is negative and end - start
is greater than the type’s width, the function panics.
If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
If $n < 0$, let $$ 2^W + n = \sum_{i=0}^{W-1} 2^{b_i}, $$ where
- $W$ is the type’s width
- for all $i$, $b_i\in \{0, 1\}$, and $b_i = 1$ for $i \geq W$. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
or (self < 0
and
end - start > W
).
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The type of the block of bits is the unsigned version of the input type. If bits
has fewer bits than end - start
, the high bits are interpreted as 0 or 1,
depending on the sign of self
. If end
is greater than the type’s width, the high
bits of bits
must be 0 or 1, depending on the sign of self
.
The sign of self
remains unchanged, since only a finite number of bits are changed
and the sign is determined by the implied infinite prefix of bits.
If $n \geq 0$ and $j \neq W - 1$, let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p - 1$. Then
$$
n \gets \sum_{i=0}^{W-1} 2^{c_i},
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
If $n < 0$ or $j = W - 1$, let
$$
2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 1$ for all $i \geq W + p - 1$. Then
$$
f(n, p, q, m) = \left ( \sum_{i=0}^{W-1} 2^{c_i} \right ) - 2^W,
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width Panics if start < end
, or if end >= W
and bits
W - start
through end - start
of bits
are not equal to the original sign bit
of self
.
Examples
See here.
type Bits = u32
sourceimpl BitBlockAccess for i64
impl BitBlockAccess for i64
sourcefn get_bits(&self, start: u64, end: u64) -> Self::Bits
fn get_bits(&self, start: u64, end: u64) -> Self::Bits
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The type of the block of bits is the unsigned version of the input type. If end
is
greater than the type’s width, the high bits of the result are all 0, or all 1,
depending on the input value’s sign; and if the input is negative and end - start
is greater than the type’s width, the function panics.
If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
If $n < 0$, let $$ 2^W + n = \sum_{i=0}^{W-1} 2^{b_i}, $$ where
- $W$ is the type’s width
- for all $i$, $b_i\in \{0, 1\}$, and $b_i = 1$ for $i \geq W$. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
or (self < 0
and
end - start > W
).
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The type of the block of bits is the unsigned version of the input type. If bits
has fewer bits than end - start
, the high bits are interpreted as 0 or 1,
depending on the sign of self
. If end
is greater than the type’s width, the high
bits of bits
must be 0 or 1, depending on the sign of self
.
The sign of self
remains unchanged, since only a finite number of bits are changed
and the sign is determined by the implied infinite prefix of bits.
If $n \geq 0$ and $j \neq W - 1$, let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p - 1$. Then
$$
n \gets \sum_{i=0}^{W-1} 2^{c_i},
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
If $n < 0$ or $j = W - 1$, let
$$
2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 1$ for all $i \geq W + p - 1$. Then
$$
f(n, p, q, m) = \left ( \sum_{i=0}^{W-1} 2^{c_i} \right ) - 2^W,
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width Panics if start < end
, or if end >= W
and bits
W - start
through end - start
of bits
are not equal to the original sign bit
of self
.
Examples
See here.
type Bits = u64
sourceimpl BitBlockAccess for i128
impl BitBlockAccess for i128
sourcefn get_bits(&self, start: u64, end: u64) -> Self::Bits
fn get_bits(&self, start: u64, end: u64) -> Self::Bits
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The type of the block of bits is the unsigned version of the input type. If end
is
greater than the type’s width, the high bits of the result are all 0, or all 1,
depending on the input value’s sign; and if the input is negative and end - start
is greater than the type’s width, the function panics.
If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
If $n < 0$, let $$ 2^W + n = \sum_{i=0}^{W-1} 2^{b_i}, $$ where
- $W$ is the type’s width
- for all $i$, $b_i\in \{0, 1\}$, and $b_i = 1$ for $i \geq W$. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
or (self < 0
and
end - start > W
).
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The type of the block of bits is the unsigned version of the input type. If bits
has fewer bits than end - start
, the high bits are interpreted as 0 or 1,
depending on the sign of self
. If end
is greater than the type’s width, the high
bits of bits
must be 0 or 1, depending on the sign of self
.
The sign of self
remains unchanged, since only a finite number of bits are changed
and the sign is determined by the implied infinite prefix of bits.
If $n \geq 0$ and $j \neq W - 1$, let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p - 1$. Then
$$
n \gets \sum_{i=0}^{W-1} 2^{c_i},
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
If $n < 0$ or $j = W - 1$, let
$$
2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 1$ for all $i \geq W + p - 1$. Then
$$
f(n, p, q, m) = \left ( \sum_{i=0}^{W-1} 2^{c_i} \right ) - 2^W,
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width Panics if start < end
, or if end >= W
and bits
W - start
through end - start
of bits
are not equal to the original sign bit
of self
.
Examples
See here.
type Bits = u128
sourceimpl BitBlockAccess for isize
impl BitBlockAccess for isize
sourcefn get_bits(&self, start: u64, end: u64) -> Self::Bits
fn get_bits(&self, start: u64, end: u64) -> Self::Bits
Extracts a block of adjacent bits from a number.
The first index is start
and last index is end - 1
.
The type of the block of bits is the unsigned version of the input type. If end
is
greater than the type’s width, the high bits of the result are all 0, or all 1,
depending on the input value’s sign; and if the input is negative and end - start
is greater than the type’s width, the function panics.
If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$; so finitely many of the bits are 1, and the rest are 0. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
If $n < 0$, let $$ 2^W + n = \sum_{i=0}^{W-1} 2^{b_i}, $$ where
- $W$ is the type’s width
- for all $i$, $b_i\in \{0, 1\}$, and $b_i = 1$ for $i \geq W$. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width. Panics if start < end
or (self < 0
and
end - start > W
).
Examples
See here.
sourcefn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits)
Replaces a block of adjacent bits in a number with other bits.
The least-significant end - start
bits of bits
are assigned to bits start
through end - 1
, inclusive, of self
.
The type of the block of bits is the unsigned version of the input type. If bits
has fewer bits than end - start
, the high bits are interpreted as 0 or 1,
depending on the sign of self
. If end
is greater than the type’s width, the high
bits of bits
must be 0 or 1, depending on the sign of self
.
The sign of self
remains unchanged, since only a finite number of bits are changed
and the sign is determined by the implied infinite prefix of bits.
If $n \geq 0$ and $j \neq W - 1$, let
$$
n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 0$ for all $i \geq W + p - 1$. Then
$$
n \gets \sum_{i=0}^{W-1} 2^{c_i},
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
If $n < 0$ or $j = W - 1$, let
$$
2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
$$
where for all $i$, $b_i\in \{0, 1\}$ and $W$ is $t::WIDTH
.
Let
$$
m = \sum_{i=0}^k 2^{d_i},
$$
where for all $i$, $d_i\in \{0, 1\}$.
Also, let $p, q \in \mathbb{N}$, where $d_i = 1$ for all $i \geq W + p - 1$. Then
$$
f(n, p, q, m) = \left ( \sum_{i=0}^{W-1} 2^{c_i} \right ) - 2^W,
$$
where
$$
\{c_0, c_1, c_2, \ldots, c_ {W-1}\} =
\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
b_ {W-1}\}.
$$
Worst-case complexity
Constant time and additional memory.
Panics
Let W
be the type’s width Panics if start < end
, or if end >= W
and bits
W - start
through end - start
of bits
are not equal to the original sign bit
of self
.
Examples
See here.