# Trait malachite_base::num::logic::traits::BitAccess

pub trait BitAccess {
fn get_bit(&self, index: u64) -> bool;
fn set_bit(&mut self, index: u64);
fn clear_bit(&mut self, index: u64);

fn assign_bit(&mut self, index: u64, bit: bool) { ... }
fn flip_bit(&mut self, index: u64) { ... }
}
Expand description

Defines functions that access or modify individual bits in a number.

## Required Methods

Determines whether a number has a true or false bit at index.

Sets the bit at index to true.

Sets the bit at index to false.

## Provided Methods

Sets the bit at index to whichever value bit is.

##### Worst-case complexity

$T(n) = O(\max(T_S(n), T_C(n)))$,

$M(n) = O(\max(M_S(n), M_C(n)))$

where $T$ is time, $M$ is additional memory, $T_S$ and $M_S$ are the complexities of set_bit, and $T_C$ and $M_C$ are the complexities of clear_bit.

##### Panics

See panics for set_bit and clear_bit.

Sets the bit at index to the opposite of its original value.

##### Worst-case complexity

$T(n) = O(T_G(n) + \max(T_S(n), T_C(n)))$,

$M(n) = O(M_G(n) + \max(M_S(n), M_C(n)))$

where $T$ is time, $M$ is additional memory, $T_G$ and $M_G$ are the complexities of get_bit, $T_S$ and $M_S$ are the complexities of set_bit, and $T_C$ and $M_C$ are the complexities of clear_bit.

##### Panics

See panics for get_bit, set_bit and clear_bit.

## Implementations on Foreign Types

Determines whether the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in its binary expansion, is 0 or 1.

false means 0 and true means 1. Getting bits beyond the type’s width is allowed; those bits are false.

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, j) = (b_j = 1)$.

##### Examples

See here.

Sets the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in its binary expansion, to 1.

Setting bits beyond the type’s width is disallowed.

Let $$n = \sum_{i=0}^{W-1} 2^{b_i},$$ where for all $i$, $b_i\in \{0, 1\}$, and $W$ is the width of the type. Then $$n \gets \begin{cases} n + 2^j & \text{if} \quad b_j = 0, \\ n & \text{otherwise}, \end{cases}$$ where $j < W$.

##### Examples

See here.

Sets the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in its binary expansion, to 0.

Clearing bits beyond the type’s width is allowed; since those bits are already false, clearing them does nothing.

Let $$n = \sum_{i=0}^{W-1} 2^{b_i},$$ where for all $i$, $b_i\in \{0, 1\}$, and $W$ is the width of the type. Then $$n \gets \begin{cases} n - 2^j & \text{if} \quad b_j = 1, \\ n & \text{otherwise}. \end{cases}$$

##### Examples

See here.

Determines whether the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in its binary expansion, is 0 or 1.

false means 0 and true means 1. Getting bits beyond the type’s width is allowed; those bits are false.

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, j) = (b_j = 1)$.

##### Examples

See here.

Sets the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in its binary expansion, to 1.

Setting bits beyond the type’s width is disallowed.

Let $$n = \sum_{i=0}^{W-1} 2^{b_i},$$ where for all $i$, $b_i\in \{0, 1\}$, and $W$ is the width of the type. Then $$n \gets \begin{cases} n + 2^j & \text{if} \quad b_j = 0, \\ n & \text{otherwise}, \end{cases}$$ where $j < W$.

##### Examples

See here.

Sets the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in its binary expansion, to 0.

Clearing bits beyond the type’s width is allowed; since those bits are already false, clearing them does nothing.

Let $$n = \sum_{i=0}^{W-1} 2^{b_i},$$ where for all $i$, $b_i\in \{0, 1\}$, and $W$ is the width of the type. Then $$n \gets \begin{cases} n - 2^j & \text{if} \quad b_j = 1, \\ n & \text{otherwise}. \end{cases}$$

##### Examples

See here.

Determines whether the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in its binary expansion, is 0 or 1.

false means 0 and true means 1. Getting bits beyond the type’s width is allowed; those bits are false.

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, j) = (b_j = 1)$.

##### Examples

See here.

Sets the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in its binary expansion, to 1.

Setting bits beyond the type’s width is disallowed.

Let $$n = \sum_{i=0}^{W-1} 2^{b_i},$$ where for all $i$, $b_i\in \{0, 1\}$, and $W$ is the width of the type. Then $$n \gets \begin{cases} n + 2^j & \text{if} \quad b_j = 0, \\ n & \text{otherwise}, \end{cases}$$ where $j < W$.

##### Examples

See here.

Sets the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in its binary expansion, to 0.

Clearing bits beyond the type’s width is allowed; since those bits are already false, clearing them does nothing.

Let $$n = \sum_{i=0}^{W-1} 2^{b_i},$$ where for all $i$, $b_i\in \{0, 1\}$, and $W$ is the width of the type. Then $$n \gets \begin{cases} n - 2^j & \text{if} \quad b_j = 1, \\ n & \text{otherwise}. \end{cases}$$

##### Examples

See here.

Determines whether the $i$th bit of a signed primitive integer is 0 or 1.

false means 0 and true means 1. Getting bits beyond the type’s width is allowed; those bits are true if the value is negative, and false otherwise.

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, i) = (b_i = 1)$.

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, j) = (b_j = 1)$.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 1.

Setting bits beyond the type’s width is disallowed if the number is non-negative.

If $n \geq 0$ and $j \neq W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n + 2^j & \text{if} \quad b_j = 0, \\ n & \text{otherwise}, \end{cases}$$ where $n < 0$ or $j < W$.

##### Panics

Panics if $n \geq 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 0.

Clearing bits beyond the type’s width is disallowed if the number is negative.

If $n \geq 0$ or $j = W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n - 2^j & \text{if} \quad b_j = 1, \\ n & \text{otherwise}, \end{cases}$$ where $n \geq 0$ or $j < W$.

##### Panics

Panics if $n < 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Determines whether the $i$th bit of a signed primitive integer is 0 or 1.

false means 0 and true means 1. Getting bits beyond the type’s width is allowed; those bits are true if the value is negative, and false otherwise.

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, i) = (b_i = 1)$.

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, j) = (b_j = 1)$.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 1.

Setting bits beyond the type’s width is disallowed if the number is non-negative.

If $n \geq 0$ and $j \neq W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n + 2^j & \text{if} \quad b_j = 0, \\ n & \text{otherwise}, \end{cases}$$ where $n < 0$ or $j < W$.

##### Panics

Panics if $n \geq 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 0.

Clearing bits beyond the type’s width is disallowed if the number is negative.

If $n \geq 0$ or $j = W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n - 2^j & \text{if} \quad b_j = 1, \\ n & \text{otherwise}, \end{cases}$$ where $n \geq 0$ or $j < W$.

##### Panics

Panics if $n < 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Determines whether the $i$th bit of a signed primitive integer is 0 or 1.

false means 0 and true means 1. Getting bits beyond the type’s width is allowed; those bits are true if the value is negative, and false otherwise.

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, i) = (b_i = 1)$.

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, j) = (b_j = 1)$.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 1.

Setting bits beyond the type’s width is disallowed if the number is non-negative.

If $n \geq 0$ and $j \neq W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n + 2^j & \text{if} \quad b_j = 0, \\ n & \text{otherwise}, \end{cases}$$ where $n < 0$ or $j < W$.

##### Panics

Panics if $n \geq 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 0.

Clearing bits beyond the type’s width is disallowed if the number is negative.

If $n \geq 0$ or $j = W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n - 2^j & \text{if} \quad b_j = 1, \\ n & \text{otherwise}, \end{cases}$$ where $n \geq 0$ or $j < W$.

##### Panics

Panics if $n < 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Determines whether the $i$th bit of a signed primitive integer is 0 or 1.

false means 0 and true means 1. Getting bits beyond the type’s width is allowed; those bits are true if the value is negative, and false otherwise.

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, i) = (b_i = 1)$.

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, j) = (b_j = 1)$.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 1.

Setting bits beyond the type’s width is disallowed if the number is non-negative.

If $n \geq 0$ and $j \neq W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n + 2^j & \text{if} \quad b_j = 0, \\ n & \text{otherwise}, \end{cases}$$ where $n < 0$ or $j < W$.

##### Panics

Panics if $n \geq 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 0.

Clearing bits beyond the type’s width is disallowed if the number is negative.

If $n \geq 0$ or $j = W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n - 2^j & \text{if} \quad b_j = 1, \\ n & \text{otherwise}, \end{cases}$$ where $n \geq 0$ or $j < W$.

##### Panics

Panics if $n < 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Determines whether the $i$th bit of a signed primitive integer is 0 or 1.

false means 0 and true means 1. Getting bits beyond the type’s width is allowed; those bits are true if the value is negative, and false otherwise.

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, i) = (b_i = 1)$.

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, j) = (b_j = 1)$.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 1.

Setting bits beyond the type’s width is disallowed if the number is non-negative.

If $n \geq 0$ and $j \neq W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n + 2^j & \text{if} \quad b_j = 0, \\ n & \text{otherwise}, \end{cases}$$ where $n < 0$ or $j < W$.

##### Panics

Panics if $n \geq 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 0.

Clearing bits beyond the type’s width is disallowed if the number is negative.

If $n \geq 0$ or $j = W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n - 2^j & \text{if} \quad b_j = 1, \\ n & \text{otherwise}, \end{cases}$$ where $n \geq 0$ or $j < W$.

##### Panics

Panics if $n < 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Determines whether the $i$th bit of a signed primitive integer is 0 or 1.

false means 0 and true means 1. Getting bits beyond the type’s width is allowed; those bits are true if the value is negative, and false otherwise.

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, i) = (b_i = 1)$.

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, j) = (b_j = 1)$.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 1.

Setting bits beyond the type’s width is disallowed if the number is non-negative.

If $n \geq 0$ and $j \neq W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n + 2^j & \text{if} \quad b_j = 0, \\ n & \text{otherwise}, \end{cases}$$ where $n < 0$ or $j < W$.

##### Panics

Panics if $n \geq 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.

##### Examples

See here.

Sets the $i$th bit of a signed primitive integer to 0.

Clearing bits beyond the type’s width is disallowed if the number is negative.

If $n \geq 0$ or $j = W - 1$, let $$n = \sum_{i=0}^{W-1} 2^{b_i};$$ but 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 the width of the type. Then $$n \gets \begin{cases} n - 2^j & \text{if} \quad b_j = 1, \\ n & \text{otherwise}, \end{cases}$$ where $n \geq 0$ or $j < W$.

##### Worst-case complexity

Panics if $n < 0$ and $i \geq W$, where $n$ is self, $i$ is index and $W$ is the width of the type.