Struct BitArray

Source
pub struct BitArray { /* private fields */ }

Implementations§

Source§

impl BitArray

Source

pub fn new(bit_count: usize) -> Self

Initializes a new BitArray.

§Arguments
  • bit_count - The maximum number of bits in the array.
§Panics

This function will panic if bit_count is zero.

Source

pub fn reset(&mut self)

Resets all bits in the array.

Source

pub const fn all_set(&self) -> bool

Checks if all bits are set.

§Returns
  • true if all bits in the array are set, otherwise false.
Source

pub fn first_unset_bit(&self) -> Option<usize>

Finds the first bit that is not set in the array.

§Returns
  • The index of the first unset bit, or None if all bits are set.
Source

pub fn first_set_bit(&self) -> Option<usize>

Finds the first bit that is set in the array.

§Returns
  • The index of the first set bit, or None if no bits are set.
Source

pub const fn count_set_bits(&self) -> usize

Returns the number of bits that are currently set to 1.

§Returns

The number of bits that are set in the BitArray.

Source

pub const fn bit_count(&self) -> usize

Returns the total number of bits in the BitArray.

§Returns

The total number of bits in the BitArray.

Source

pub fn set(&mut self, index: usize)

Sets the bit at the given index.

§Arguments
  • index - The zero-based index of the bit to set.
§Panics

This function will panic if the index is out of bounds.

Source

pub fn unset(&mut self, index: usize)

Unsets (clears) the bit at the given index.

§Arguments
  • index - The zero-based index of the bit to clear.
§Panics

This function will panic if the index is out of bounds.

Source

pub fn set_bit(&mut self, index: usize, set: bool)

Sets or unsets the bit at the given index based on the value of set.

§Arguments
  • index - The zero-based index of the bit to modify.
  • set - If true, the bit will be set (1). If false, the bit will be unset (0).
§Panics

This function will panic if the index is out of bounds.

Source

pub fn atom_from_index(&self, from_index: usize) -> u64

Returns the atom value that is located at the specified index.

§Arguments
  • from_index - The index from which to start reading.
§Returns

The atom value at the specified index.

Source

pub fn get(&self, index: usize) -> bool

Returns the bit value at the specified index.

§Arguments
  • index - The bit index to read from.
§Returns

The read bit value (0 or 1).

§Panics

This function will panic if the index is out of bounds.

Trait Implementations§

Source§

impl Clone for BitArray

Source§

fn clone(&self) -> BitArray

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BitArray

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the BitArray as a binary string with groups of 8 bits separated by a space.

§Arguments
  • f - The formatter used to output the debug string.
§Returns

A Result indicating whether the formatting was successful.

§Example
use bit_array_rs::BitArray;
let mut bit_array = BitArray::new(16);
bit_array.set(3);
bit_array.set(7);
bit_array.set(9);
bit_array.set(15);

assert_eq!(format!("{:?}", bit_array), "00010001 01000001");
Source§

impl Display for BitArray

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the BitArray as a continuous binary string without any spaces.

§Arguments
  • f - The formatter used to output the display string.
§Returns

A Result indicating whether the formatting was successful.

§Example
use bit_array_rs::BitArray;
let mut bit_array = BitArray::new(16);
bit_array.set(3);
bit_array.set(7);
bit_array.set(9);
bit_array.set(15);

assert_eq!(format!("{}", bit_array), "0001000101000001");
Source§

impl Index<usize> for BitArray

Source§

fn index(&self, index: usize) -> &Self::Output

Provides indexed access to individual bits in the BitArray.

§Arguments
  • index - The zero-based index of the bit to access.
§Returns

A reference to a boolean value (true or false) representing the state of the bit at the specified index.

§Panics

This function will panic if the index is out of bounds.

§Example
use bit_array_rs::BitArray;
let mut bit_array = BitArray::new(16);
bit_array.set(3);
assert_eq!(bit_array[3], true);
assert_eq!(bit_array[0], false);
Source§

type Output = bool

The returned type after indexing.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.