Skip to main content

Bitwise

Struct Bitwise 

Source
#[repr(transparent)]
pub struct Bitwise<T>(pub T);
Expand description

🛠️ Provides constant bitwise operations on T.


📍 num/fin


It’s implemented for: u8, u16, u32, u64, u128 and usize.

§Panic behavior

Unchecked bit operations panic in debug builds when a bit index or range is out of bounds. In release builds they do not panic; the invalid index produces a wrapped shift and a non-meaningful result.

Checked variants never panic and return an error instead.

See also BitOps for the related trait.

§Methods

The methods are the same for all unsigned primitives. The following list of methods links to the u8 implementation:

Tuple Fields§

§0: T

Implementations§

Source§

impl Bitwise<u8>

§Implementations for u8.

§Constants and mask constructors for u8.

Source

pub const BITS: u32 = u8::BITS

The size in bits.

Source

pub const fn mask_range(start: u32, end: u32) -> Self

Returns a bitmask of ones from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<u8>::mask_range(1, 4).0]
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    2.8 ns   163.0 ns
  64    2.0 ns    21.6 ns
  32    2.4 ns    12.1 ns
  16    2.6 ns     7.6 ns
   8    2.9 ns     8.4 ns
Source

pub const fn mask_range_checked( start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Returns a bitmask of ones from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<u8>::mask_range_checked(1, 4).unwrap().0];
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    3.7 ns   145.0 ns
  64    3.2 ns    38.2 ns
  32    3.2 ns     9.0 ns
  16    3.2 ns     7.0 ns
   8    3.1 ns     6.7 ns
Source

pub const fn is_set_mask(self, mask: u8) -> bool

Returns true if any bit selected by mask is 1 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert![b.is_set_mask(0b1000_0000)];
Source

pub const fn set_mask(self, mask: u8) -> Self

Sets all bits selected by mask to 1.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert_eq![0b1010_1111, b.set_mask(0b0000_1111).0];
Source

pub const fn is_unset_mask(self, mask: u8) -> bool

Returns true if all bits selected by mask are 0 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert![b.is_unset_mask(0b0100_0000)];
Source

pub const fn unset_mask(self, mask: u8) -> Self

Sets all bits selected by mask to 0.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_mask(0b1000_0000).0];
Source§

impl Bitwise<u8>

§Get methods for u8.

Source

pub const fn get_range(self, start: u32, end: u32) -> Self

Gets the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range(1, 4).0];
Source

pub const fn get_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range_checked(1, 4).unwrap().0];
Source

pub const fn get_value_range(self, start: u32, end: u32) -> Self

Gets the value of the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1011_0110);
assert_eq![0b1011, b.get_value_range(1, 4).0];
Source

pub const fn get_value_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the value of the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1011_0110);
assert_eq![0b1011, b.get_value_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<u8>

§Set ops for u8.

Source

pub const fn is_set(self, nth: u32) -> bool

Returns true if the nth bit is 1.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<u8>(0b1000_0000);
assert![b.is_set(7)];
Source

pub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is 1.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u8>(0b1000_0000);
assert![b.is_set_checked(7).unwrap()];
Source

pub const fn set(self, nth: u32) -> Self

Sets the nth bit.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<u8>(0);
assert_eq![0b0000_1000, b.set(3).0];
Source

pub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Sets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u8>(0);
assert_eq![0b0000_1000, b.set_checked(3).unwrap().0];
Source

pub const fn is_set_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 1.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b0011_1000);
assert![b.is_set_range(3, 5)];
Source

pub const fn is_set_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 1.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b0011_1000);
assert![b.is_set_range_checked(3, 5).unwrap()];
Source

pub const fn set_range(self, start: u32, end: u32) -> Self

Sets the bits in self to 1, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range(1, 4).0];
Source

pub const fn set_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the bits in self to 1, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range_checked(1, 4).unwrap().0];
Source

pub const fn set_all(self) -> Self

Sets all the bits to 1.

§Examples
let b = Bitwise::<u8>(0);
assert_eq![!0 as u8, b.set_all().0];
Source

pub const fn set_value_range(self, value: u8, start: u32, end: u32) -> Self

Sets the given value into the bits from the [start..=end] range.

Leaves the rest of the bits unchanged.

The value is first masked to fit the size of the range, and then it is inserted into the specified bit range of self, replacing the existing bits in that range. The rest of the bits in self remain unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range(0b101, 1, 3).0];
Source

pub const fn set_value_range_checked( self, value: u8, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range_checked(0b101, 1, 3).unwrap().0];
Source

pub const fn set_value_range_checked_strict( self, value: u8, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given checked value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

A value fits when it can be represented by end - start + 1 bits.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS, MismatchedIndices if start > end, and MismatchedCapacity if value does not fit within the specified bit range.

§Examples
let b = Bitwise::<u8>(0);
assert![b.set_value_range_checked_strict(0b100, 0, 1).is_err()];
Source§

impl Bitwise<u8>

§Unset ops for u8.

Source

pub const fn is_unset(self, nth: u32) -> bool

Returns true if the nth bit is 0.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert![b.is_unset(6)];
Source

pub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is unset to 0.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert![b.is_unset_checked(6).unwrap()];
Source

pub const fn unset(self, nth: u32) -> Self

Unsets the nth bit.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert_eq![0b0010_0000, b.unset(7).0];
Source

pub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Unsets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_checked(7).unwrap().0];
Source

pub const fn is_unset_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 0.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1000_0001);
assert![b.is_unset_range(1, 6)];
Source

pub const fn is_unset_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 0.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1000_0001);
assert![b.is_unset_range_checked(1, 6).unwrap()];
Source

pub const fn unset_range(self, start: u32, end: u32) -> Self

Unsets the bits in self to 0, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range(1, 4).0];
Source

pub const fn unset_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Unsets the bits in self to 0, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range_checked(1, 4).unwrap().0];
Source

pub const fn unset_all(self) -> Self

Unsets all the bits to 0.

§Examples
let b = Bitwise::<u8>(0b1111_1111);
assert_eq![0, b.unset_all().0];
Source§

impl Bitwise<u8>

§Flip ops for u8.

Source

pub const fn flip(self, nth: u32) -> Self

Flips the nth bit.

§Panics

Panics in debug mode if `nth >= BITS.

§Examples
let b = Bitwise::<u8>(0b0000_0001);
assert_eq![0, b.flip(0).0];
Source

pub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Flips the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u8>(0b0000_0001);
assert_eq![0, b.flip_checked(0).unwrap().0];
Source

pub const fn flip_range(self, start: u32, end: u32) -> Self

Flips the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range(4, 7).0];
Source

pub const fn flip_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Flips the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range_checked(4, 7).unwrap().0];
Source

pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self

Conditionally flips bits in a range if cond is true.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if(4, 7, false).0];
Source

pub const fn flip_range_if_checked( self, start: u32, end: u32, cond: bool, ) -> Result<Self, MismatchedBounds>

Conditionally flips bits in a range if cond is true.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if_checked(4, 7, false).unwrap().0];
Source§

impl Bitwise<u8>

§Reverse ops for u8.

Source

pub const fn reverse_range(self, start: u32, end: u32) -> Self

Reverses the order of the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range(1, 4).0];
Source

pub const fn reverse_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Reverses the order of the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<u8>

§Count ops for u8.

Source

pub const fn count_ones_range(self, start: u32, end: u32) -> u32

Counts the number of 1s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1011_0110);
assert_eq![3, b.count_ones_range(1, 4)];
Source

pub const fn count_ones_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 1s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1011_0110);
assert_eq![3, b.count_ones_range_checked(1, 4).unwrap()];
Source

pub const fn count_zeros_range(self, start: u32, end: u32) -> u32

Counts the number of 0s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1011_0110);
assert_eq![1, b.count_zeros_range(1, 4)];
Source

pub const fn count_zeros_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 0s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1011_0110);
assert_eq![1, b.count_zeros_range_checked(1, 4).unwrap()];
Source§

impl Bitwise<u8>

§Find ops for u8.

Source

pub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range(0, 7)];
Source

pub const fn find_first_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 0 in self from the [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range(0, 7)];
Source

pub const fn find_first_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 0 in self from the checked [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range(0, 7)];
Source

pub const fn find_last_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 0 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u8>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range(0, 7)];
Source

pub const fn find_last_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 0 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u8>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range_checked(0, 7).unwrap()];
Source§

impl Bitwise<u16>

§Implementations for u16.

§Constants and mask constructors for u16.

Source

pub const BITS: u32 = u16::BITS

The size in bits.

Source

pub const fn mask_range(start: u32, end: u32) -> Self

Returns a bitmask of ones from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<u16>::mask_range(1, 4).0]
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    2.8 ns   163.0 ns
  64    2.0 ns    21.6 ns
  32    2.4 ns    12.1 ns
  16    2.6 ns     7.6 ns
   8    2.9 ns     8.4 ns
Source

pub const fn mask_range_checked( start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Returns a bitmask of ones from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<u16>::mask_range_checked(1, 4).unwrap().0];
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    3.7 ns   145.0 ns
  64    3.2 ns    38.2 ns
  32    3.2 ns     9.0 ns
  16    3.2 ns     7.0 ns
   8    3.1 ns     6.7 ns
Source

pub const fn is_set_mask(self, mask: u16) -> bool

Returns true if any bit selected by mask is 1 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert![b.is_set_mask(0b1000_0000)];
Source

pub const fn set_mask(self, mask: u16) -> Self

Sets all bits selected by mask to 1.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert_eq![0b1010_1111, b.set_mask(0b0000_1111).0];
Source

pub const fn is_unset_mask(self, mask: u16) -> bool

Returns true if all bits selected by mask are 0 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert![b.is_unset_mask(0b0100_0000)];
Source

pub const fn unset_mask(self, mask: u16) -> Self

Sets all bits selected by mask to 0.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_mask(0b1000_0000).0];
Source§

impl Bitwise<u16>

§Get methods for u16.

Source

pub const fn get_range(self, start: u32, end: u32) -> Self

Gets the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range(1, 4).0];
Source

pub const fn get_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range_checked(1, 4).unwrap().0];
Source

pub const fn get_value_range(self, start: u32, end: u32) -> Self

Gets the value of the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1011_0110);
assert_eq![0b1011, b.get_value_range(1, 4).0];
Source

pub const fn get_value_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the value of the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1011_0110);
assert_eq![0b1011, b.get_value_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<u16>

§Set ops for u16.

Source

pub const fn is_set(self, nth: u32) -> bool

Returns true if the nth bit is 1.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<u16>(0b1000_0000);
assert![b.is_set(7)];
Source

pub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is 1.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u16>(0b1000_0000);
assert![b.is_set_checked(7).unwrap()];
Source

pub const fn set(self, nth: u32) -> Self

Sets the nth bit.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<u16>(0);
assert_eq![0b0000_1000, b.set(3).0];
Source

pub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Sets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u16>(0);
assert_eq![0b0000_1000, b.set_checked(3).unwrap().0];
Source

pub const fn is_set_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 1.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b0011_1000);
assert![b.is_set_range(3, 5)];
Source

pub const fn is_set_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 1.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b0011_1000);
assert![b.is_set_range_checked(3, 5).unwrap()];
Source

pub const fn set_range(self, start: u32, end: u32) -> Self

Sets the bits in self to 1, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range(1, 4).0];
Source

pub const fn set_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the bits in self to 1, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range_checked(1, 4).unwrap().0];
Source

pub const fn set_all(self) -> Self

Sets all the bits to 1.

§Examples
let b = Bitwise::<u16>(0);
assert_eq![!0 as u16, b.set_all().0];
Source

pub const fn set_value_range(self, value: u16, start: u32, end: u32) -> Self

Sets the given value into the bits from the [start..=end] range.

Leaves the rest of the bits unchanged.

The value is first masked to fit the size of the range, and then it is inserted into the specified bit range of self, replacing the existing bits in that range. The rest of the bits in self remain unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range(0b101, 1, 3).0];
Source

pub const fn set_value_range_checked( self, value: u16, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range_checked(0b101, 1, 3).unwrap().0];
Source

pub const fn set_value_range_checked_strict( self, value: u16, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given checked value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

A value fits when it can be represented by end - start + 1 bits.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS, MismatchedIndices if start > end, and MismatchedCapacity if value does not fit within the specified bit range.

§Examples
let b = Bitwise::<u16>(0);
assert![b.set_value_range_checked_strict(0b100, 0, 1).is_err()];
Source§

impl Bitwise<u16>

§Unset ops for u16.

Source

pub const fn is_unset(self, nth: u32) -> bool

Returns true if the nth bit is 0.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert![b.is_unset(6)];
Source

pub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is unset to 0.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert![b.is_unset_checked(6).unwrap()];
Source

pub const fn unset(self, nth: u32) -> Self

Unsets the nth bit.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert_eq![0b0010_0000, b.unset(7).0];
Source

pub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Unsets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_checked(7).unwrap().0];
Source

pub const fn is_unset_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 0.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1000_0001);
assert![b.is_unset_range(1, 6)];
Source

pub const fn is_unset_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 0.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1000_0001);
assert![b.is_unset_range_checked(1, 6).unwrap()];
Source

pub const fn unset_range(self, start: u32, end: u32) -> Self

Unsets the bits in self to 0, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range(1, 4).0];
Source

pub const fn unset_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Unsets the bits in self to 0, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range_checked(1, 4).unwrap().0];
Source

pub const fn unset_all(self) -> Self

Unsets all the bits to 0.

§Examples
let b = Bitwise::<u16>(0b1111_1111);
assert_eq![0, b.unset_all().0];
Source§

impl Bitwise<u16>

§Flip ops for u16.

Source

pub const fn flip(self, nth: u32) -> Self

Flips the nth bit.

§Panics

Panics in debug mode if `nth >= BITS.

§Examples
let b = Bitwise::<u16>(0b0000_0001);
assert_eq![0, b.flip(0).0];
Source

pub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Flips the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u16>(0b0000_0001);
assert_eq![0, b.flip_checked(0).unwrap().0];
Source

pub const fn flip_range(self, start: u32, end: u32) -> Self

Flips the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range(4, 7).0];
Source

pub const fn flip_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Flips the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range_checked(4, 7).unwrap().0];
Source

pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self

Conditionally flips bits in a range if cond is true.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if(4, 7, false).0];
Source

pub const fn flip_range_if_checked( self, start: u32, end: u32, cond: bool, ) -> Result<Self, MismatchedBounds>

Conditionally flips bits in a range if cond is true.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if_checked(4, 7, false).unwrap().0];
Source§

impl Bitwise<u16>

§Reverse ops for u16.

Source

pub const fn reverse_range(self, start: u32, end: u32) -> Self

Reverses the order of the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range(1, 4).0];
Source

pub const fn reverse_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Reverses the order of the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<u16>

§Count ops for u16.

Source

pub const fn count_ones_range(self, start: u32, end: u32) -> u32

Counts the number of 1s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1011_0110);
assert_eq![3, b.count_ones_range(1, 4)];
Source

pub const fn count_ones_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 1s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1011_0110);
assert_eq![3, b.count_ones_range_checked(1, 4).unwrap()];
Source

pub const fn count_zeros_range(self, start: u32, end: u32) -> u32

Counts the number of 0s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1011_0110);
assert_eq![1, b.count_zeros_range(1, 4)];
Source

pub const fn count_zeros_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 0s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1011_0110);
assert_eq![1, b.count_zeros_range_checked(1, 4).unwrap()];
Source§

impl Bitwise<u16>

§Find ops for u16.

Source

pub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range(0, 7)];
Source

pub const fn find_first_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 0 in self from the [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range(0, 7)];
Source

pub const fn find_first_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 0 in self from the checked [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range(0, 7)];
Source

pub const fn find_last_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 0 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u16>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range(0, 7)];
Source

pub const fn find_last_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 0 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u16>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range_checked(0, 7).unwrap()];
Source§

impl Bitwise<u32>

§Implementations for u32.

§Constants and mask constructors for u32.

Source

pub const BITS: u32 = u32::BITS

The size in bits.

Source

pub const fn mask_range(start: u32, end: u32) -> Self

Returns a bitmask of ones from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<u32>::mask_range(1, 4).0]
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    2.8 ns   163.0 ns
  64    2.0 ns    21.6 ns
  32    2.4 ns    12.1 ns
  16    2.6 ns     7.6 ns
   8    2.9 ns     8.4 ns
Source

pub const fn mask_range_checked( start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Returns a bitmask of ones from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<u32>::mask_range_checked(1, 4).unwrap().0];
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    3.7 ns   145.0 ns
  64    3.2 ns    38.2 ns
  32    3.2 ns     9.0 ns
  16    3.2 ns     7.0 ns
   8    3.1 ns     6.7 ns
Source

pub const fn is_set_mask(self, mask: u32) -> bool

Returns true if any bit selected by mask is 1 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert![b.is_set_mask(0b1000_0000)];
Source

pub const fn set_mask(self, mask: u32) -> Self

Sets all bits selected by mask to 1.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert_eq![0b1010_1111, b.set_mask(0b0000_1111).0];
Source

pub const fn is_unset_mask(self, mask: u32) -> bool

Returns true if all bits selected by mask are 0 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert![b.is_unset_mask(0b0100_0000)];
Source

pub const fn unset_mask(self, mask: u32) -> Self

Sets all bits selected by mask to 0.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_mask(0b1000_0000).0];
Source§

impl Bitwise<u32>

§Get methods for u32.

Source

pub const fn get_range(self, start: u32, end: u32) -> Self

Gets the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range(1, 4).0];
Source

pub const fn get_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range_checked(1, 4).unwrap().0];
Source

pub const fn get_value_range(self, start: u32, end: u32) -> Self

Gets the value of the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1011_0110);
assert_eq![0b1011, b.get_value_range(1, 4).0];
Source

pub const fn get_value_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the value of the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1011_0110);
assert_eq![0b1011, b.get_value_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<u32>

§Set ops for u32.

Source

pub const fn is_set(self, nth: u32) -> bool

Returns true if the nth bit is 1.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<u32>(0b1000_0000);
assert![b.is_set(7)];
Source

pub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is 1.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u32>(0b1000_0000);
assert![b.is_set_checked(7).unwrap()];
Source

pub const fn set(self, nth: u32) -> Self

Sets the nth bit.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<u32>(0);
assert_eq![0b0000_1000, b.set(3).0];
Source

pub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Sets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u32>(0);
assert_eq![0b0000_1000, b.set_checked(3).unwrap().0];
Source

pub const fn is_set_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 1.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b0011_1000);
assert![b.is_set_range(3, 5)];
Source

pub const fn is_set_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 1.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b0011_1000);
assert![b.is_set_range_checked(3, 5).unwrap()];
Source

pub const fn set_range(self, start: u32, end: u32) -> Self

Sets the bits in self to 1, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range(1, 4).0];
Source

pub const fn set_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the bits in self to 1, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range_checked(1, 4).unwrap().0];
Source

pub const fn set_all(self) -> Self

Sets all the bits to 1.

§Examples
let b = Bitwise::<u32>(0);
assert_eq![!0 as u32, b.set_all().0];
Source

pub const fn set_value_range(self, value: u32, start: u32, end: u32) -> Self

Sets the given value into the bits from the [start..=end] range.

Leaves the rest of the bits unchanged.

The value is first masked to fit the size of the range, and then it is inserted into the specified bit range of self, replacing the existing bits in that range. The rest of the bits in self remain unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range(0b101, 1, 3).0];
Source

pub const fn set_value_range_checked( self, value: u32, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range_checked(0b101, 1, 3).unwrap().0];
Source

pub const fn set_value_range_checked_strict( self, value: u32, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given checked value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

A value fits when it can be represented by end - start + 1 bits.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS, MismatchedIndices if start > end, and MismatchedCapacity if value does not fit within the specified bit range.

§Examples
let b = Bitwise::<u32>(0);
assert![b.set_value_range_checked_strict(0b100, 0, 1).is_err()];
Source§

impl Bitwise<u32>

§Unset ops for u32.

Source

pub const fn is_unset(self, nth: u32) -> bool

Returns true if the nth bit is 0.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert![b.is_unset(6)];
Source

pub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is unset to 0.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert![b.is_unset_checked(6).unwrap()];
Source

pub const fn unset(self, nth: u32) -> Self

Unsets the nth bit.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert_eq![0b0010_0000, b.unset(7).0];
Source

pub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Unsets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_checked(7).unwrap().0];
Source

pub const fn is_unset_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 0.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1000_0001);
assert![b.is_unset_range(1, 6)];
Source

pub const fn is_unset_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 0.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1000_0001);
assert![b.is_unset_range_checked(1, 6).unwrap()];
Source

pub const fn unset_range(self, start: u32, end: u32) -> Self

Unsets the bits in self to 0, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range(1, 4).0];
Source

pub const fn unset_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Unsets the bits in self to 0, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range_checked(1, 4).unwrap().0];
Source

pub const fn unset_all(self) -> Self

Unsets all the bits to 0.

§Examples
let b = Bitwise::<u32>(0b1111_1111);
assert_eq![0, b.unset_all().0];
Source§

impl Bitwise<u32>

§Flip ops for u32.

Source

pub const fn flip(self, nth: u32) -> Self

Flips the nth bit.

§Panics

Panics in debug mode if `nth >= BITS.

§Examples
let b = Bitwise::<u32>(0b0000_0001);
assert_eq![0, b.flip(0).0];
Source

pub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Flips the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u32>(0b0000_0001);
assert_eq![0, b.flip_checked(0).unwrap().0];
Source

pub const fn flip_range(self, start: u32, end: u32) -> Self

Flips the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range(4, 7).0];
Source

pub const fn flip_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Flips the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range_checked(4, 7).unwrap().0];
Source

pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self

Conditionally flips bits in a range if cond is true.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if(4, 7, false).0];
Source

pub const fn flip_range_if_checked( self, start: u32, end: u32, cond: bool, ) -> Result<Self, MismatchedBounds>

Conditionally flips bits in a range if cond is true.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if_checked(4, 7, false).unwrap().0];
Source§

impl Bitwise<u32>

§Reverse ops for u32.

Source

pub const fn reverse_range(self, start: u32, end: u32) -> Self

Reverses the order of the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range(1, 4).0];
Source

pub const fn reverse_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Reverses the order of the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<u32>

§Count ops for u32.

Source

pub const fn count_ones_range(self, start: u32, end: u32) -> u32

Counts the number of 1s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1011_0110);
assert_eq![3, b.count_ones_range(1, 4)];
Source

pub const fn count_ones_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 1s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1011_0110);
assert_eq![3, b.count_ones_range_checked(1, 4).unwrap()];
Source

pub const fn count_zeros_range(self, start: u32, end: u32) -> u32

Counts the number of 0s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1011_0110);
assert_eq![1, b.count_zeros_range(1, 4)];
Source

pub const fn count_zeros_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 0s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1011_0110);
assert_eq![1, b.count_zeros_range_checked(1, 4).unwrap()];
Source§

impl Bitwise<u32>

§Find ops for u32.

Source

pub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range(0, 7)];
Source

pub const fn find_first_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 0 in self from the [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range(0, 7)];
Source

pub const fn find_first_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 0 in self from the checked [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range(0, 7)];
Source

pub const fn find_last_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 0 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u32>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range(0, 7)];
Source

pub const fn find_last_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 0 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u32>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range_checked(0, 7).unwrap()];
Source§

impl Bitwise<u64>

§Implementations for u64.

§Constants and mask constructors for u64.

Source

pub const BITS: u32 = u64::BITS

The size in bits.

Source

pub const fn mask_range(start: u32, end: u32) -> Self

Returns a bitmask of ones from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<u64>::mask_range(1, 4).0]
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    2.8 ns   163.0 ns
  64    2.0 ns    21.6 ns
  32    2.4 ns    12.1 ns
  16    2.6 ns     7.6 ns
   8    2.9 ns     8.4 ns
Source

pub const fn mask_range_checked( start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Returns a bitmask of ones from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<u64>::mask_range_checked(1, 4).unwrap().0];
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    3.7 ns   145.0 ns
  64    3.2 ns    38.2 ns
  32    3.2 ns     9.0 ns
  16    3.2 ns     7.0 ns
   8    3.1 ns     6.7 ns
Source

pub const fn is_set_mask(self, mask: u64) -> bool

Returns true if any bit selected by mask is 1 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert![b.is_set_mask(0b1000_0000)];
Source

pub const fn set_mask(self, mask: u64) -> Self

Sets all bits selected by mask to 1.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert_eq![0b1010_1111, b.set_mask(0b0000_1111).0];
Source

pub const fn is_unset_mask(self, mask: u64) -> bool

Returns true if all bits selected by mask are 0 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert![b.is_unset_mask(0b0100_0000)];
Source

pub const fn unset_mask(self, mask: u64) -> Self

Sets all bits selected by mask to 0.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_mask(0b1000_0000).0];
Source§

impl Bitwise<u64>

§Get methods for u64.

Source

pub const fn get_range(self, start: u32, end: u32) -> Self

Gets the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range(1, 4).0];
Source

pub const fn get_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range_checked(1, 4).unwrap().0];
Source

pub const fn get_value_range(self, start: u32, end: u32) -> Self

Gets the value of the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1011_0110);
assert_eq![0b1011, b.get_value_range(1, 4).0];
Source

pub const fn get_value_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the value of the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1011_0110);
assert_eq![0b1011, b.get_value_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<u64>

§Set ops for u64.

Source

pub const fn is_set(self, nth: u32) -> bool

Returns true if the nth bit is 1.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<u64>(0b1000_0000);
assert![b.is_set(7)];
Source

pub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is 1.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u64>(0b1000_0000);
assert![b.is_set_checked(7).unwrap()];
Source

pub const fn set(self, nth: u32) -> Self

Sets the nth bit.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<u64>(0);
assert_eq![0b0000_1000, b.set(3).0];
Source

pub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Sets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u64>(0);
assert_eq![0b0000_1000, b.set_checked(3).unwrap().0];
Source

pub const fn is_set_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 1.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b0011_1000);
assert![b.is_set_range(3, 5)];
Source

pub const fn is_set_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 1.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b0011_1000);
assert![b.is_set_range_checked(3, 5).unwrap()];
Source

pub const fn set_range(self, start: u32, end: u32) -> Self

Sets the bits in self to 1, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range(1, 4).0];
Source

pub const fn set_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the bits in self to 1, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range_checked(1, 4).unwrap().0];
Source

pub const fn set_all(self) -> Self

Sets all the bits to 1.

§Examples
let b = Bitwise::<u64>(0);
assert_eq![!0 as u64, b.set_all().0];
Source

pub const fn set_value_range(self, value: u64, start: u32, end: u32) -> Self

Sets the given value into the bits from the [start..=end] range.

Leaves the rest of the bits unchanged.

The value is first masked to fit the size of the range, and then it is inserted into the specified bit range of self, replacing the existing bits in that range. The rest of the bits in self remain unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range(0b101, 1, 3).0];
Source

pub const fn set_value_range_checked( self, value: u64, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range_checked(0b101, 1, 3).unwrap().0];
Source

pub const fn set_value_range_checked_strict( self, value: u64, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given checked value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

A value fits when it can be represented by end - start + 1 bits.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS, MismatchedIndices if start > end, and MismatchedCapacity if value does not fit within the specified bit range.

§Examples
let b = Bitwise::<u64>(0);
assert![b.set_value_range_checked_strict(0b100, 0, 1).is_err()];
Source§

impl Bitwise<u64>

§Unset ops for u64.

Source

pub const fn is_unset(self, nth: u32) -> bool

Returns true if the nth bit is 0.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert![b.is_unset(6)];
Source

pub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is unset to 0.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert![b.is_unset_checked(6).unwrap()];
Source

pub const fn unset(self, nth: u32) -> Self

Unsets the nth bit.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert_eq![0b0010_0000, b.unset(7).0];
Source

pub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Unsets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_checked(7).unwrap().0];
Source

pub const fn is_unset_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 0.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1000_0001);
assert![b.is_unset_range(1, 6)];
Source

pub const fn is_unset_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 0.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1000_0001);
assert![b.is_unset_range_checked(1, 6).unwrap()];
Source

pub const fn unset_range(self, start: u32, end: u32) -> Self

Unsets the bits in self to 0, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range(1, 4).0];
Source

pub const fn unset_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Unsets the bits in self to 0, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range_checked(1, 4).unwrap().0];
Source

pub const fn unset_all(self) -> Self

Unsets all the bits to 0.

§Examples
let b = Bitwise::<u64>(0b1111_1111);
assert_eq![0, b.unset_all().0];
Source§

impl Bitwise<u64>

§Flip ops for u64.

Source

pub const fn flip(self, nth: u32) -> Self

Flips the nth bit.

§Panics

Panics in debug mode if `nth >= BITS.

§Examples
let b = Bitwise::<u64>(0b0000_0001);
assert_eq![0, b.flip(0).0];
Source

pub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Flips the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u64>(0b0000_0001);
assert_eq![0, b.flip_checked(0).unwrap().0];
Source

pub const fn flip_range(self, start: u32, end: u32) -> Self

Flips the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range(4, 7).0];
Source

pub const fn flip_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Flips the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range_checked(4, 7).unwrap().0];
Source

pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self

Conditionally flips bits in a range if cond is true.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if(4, 7, false).0];
Source

pub const fn flip_range_if_checked( self, start: u32, end: u32, cond: bool, ) -> Result<Self, MismatchedBounds>

Conditionally flips bits in a range if cond is true.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if_checked(4, 7, false).unwrap().0];
Source§

impl Bitwise<u64>

§Reverse ops for u64.

Source

pub const fn reverse_range(self, start: u32, end: u32) -> Self

Reverses the order of the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range(1, 4).0];
Source

pub const fn reverse_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Reverses the order of the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<u64>

§Count ops for u64.

Source

pub const fn count_ones_range(self, start: u32, end: u32) -> u32

Counts the number of 1s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1011_0110);
assert_eq![3, b.count_ones_range(1, 4)];
Source

pub const fn count_ones_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 1s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1011_0110);
assert_eq![3, b.count_ones_range_checked(1, 4).unwrap()];
Source

pub const fn count_zeros_range(self, start: u32, end: u32) -> u32

Counts the number of 0s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1011_0110);
assert_eq![1, b.count_zeros_range(1, 4)];
Source

pub const fn count_zeros_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 0s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1011_0110);
assert_eq![1, b.count_zeros_range_checked(1, 4).unwrap()];
Source§

impl Bitwise<u64>

§Find ops for u64.

Source

pub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range(0, 7)];
Source

pub const fn find_first_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 0 in self from the [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range(0, 7)];
Source

pub const fn find_first_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 0 in self from the checked [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range(0, 7)];
Source

pub const fn find_last_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 0 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u64>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range(0, 7)];
Source

pub const fn find_last_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 0 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u64>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range_checked(0, 7).unwrap()];
Source§

impl Bitwise<u128>

§Implementations for u128.

§Constants and mask constructors for u128.

Source

pub const BITS: u32 = u128::BITS

The size in bits.

Source

pub const fn mask_range(start: u32, end: u32) -> Self

Returns a bitmask of ones from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<u128>::mask_range(1, 4).0]
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    2.8 ns   163.0 ns
  64    2.0 ns    21.6 ns
  32    2.4 ns    12.1 ns
  16    2.6 ns     7.6 ns
   8    2.9 ns     8.4 ns
Source

pub const fn mask_range_checked( start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Returns a bitmask of ones from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<u128>::mask_range_checked(1, 4).unwrap().0];
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    3.7 ns   145.0 ns
  64    3.2 ns    38.2 ns
  32    3.2 ns     9.0 ns
  16    3.2 ns     7.0 ns
   8    3.1 ns     6.7 ns
Source

pub const fn is_set_mask(self, mask: u128) -> bool

Returns true if any bit selected by mask is 1 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert![b.is_set_mask(0b1000_0000)];
Source

pub const fn set_mask(self, mask: u128) -> Self

Sets all bits selected by mask to 1.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert_eq![0b1010_1111, b.set_mask(0b0000_1111).0];
Source

pub const fn is_unset_mask(self, mask: u128) -> bool

Returns true if all bits selected by mask are 0 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert![b.is_unset_mask(0b0100_0000)];
Source

pub const fn unset_mask(self, mask: u128) -> Self

Sets all bits selected by mask to 0.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_mask(0b1000_0000).0];
Source§

impl Bitwise<u128>

§Get methods for u128.

Source

pub const fn get_range(self, start: u32, end: u32) -> Self

Gets the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range(1, 4).0];
Source

pub const fn get_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range_checked(1, 4).unwrap().0];
Source

pub const fn get_value_range(self, start: u32, end: u32) -> Self

Gets the value of the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1011_0110);
assert_eq![0b1011, b.get_value_range(1, 4).0];
Source

pub const fn get_value_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the value of the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1011_0110);
assert_eq![0b1011, b.get_value_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<u128>

§Set ops for u128.

Source

pub const fn is_set(self, nth: u32) -> bool

Returns true if the nth bit is 1.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<u128>(0b1000_0000);
assert![b.is_set(7)];
Source

pub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is 1.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u128>(0b1000_0000);
assert![b.is_set_checked(7).unwrap()];
Source

pub const fn set(self, nth: u32) -> Self

Sets the nth bit.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<u128>(0);
assert_eq![0b0000_1000, b.set(3).0];
Source

pub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Sets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u128>(0);
assert_eq![0b0000_1000, b.set_checked(3).unwrap().0];
Source

pub const fn is_set_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 1.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b0011_1000);
assert![b.is_set_range(3, 5)];
Source

pub const fn is_set_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 1.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b0011_1000);
assert![b.is_set_range_checked(3, 5).unwrap()];
Source

pub const fn set_range(self, start: u32, end: u32) -> Self

Sets the bits in self to 1, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range(1, 4).0];
Source

pub const fn set_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the bits in self to 1, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range_checked(1, 4).unwrap().0];
Source

pub const fn set_all(self) -> Self

Sets all the bits to 1.

§Examples
let b = Bitwise::<u128>(0);
assert_eq![!0 as u128, b.set_all().0];
Source

pub const fn set_value_range(self, value: u128, start: u32, end: u32) -> Self

Sets the given value into the bits from the [start..=end] range.

Leaves the rest of the bits unchanged.

The value is first masked to fit the size of the range, and then it is inserted into the specified bit range of self, replacing the existing bits in that range. The rest of the bits in self remain unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range(0b101, 1, 3).0];
Source

pub const fn set_value_range_checked( self, value: u128, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range_checked(0b101, 1, 3).unwrap().0];
Source

pub const fn set_value_range_checked_strict( self, value: u128, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given checked value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

A value fits when it can be represented by end - start + 1 bits.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS, MismatchedIndices if start > end, and MismatchedCapacity if value does not fit within the specified bit range.

§Examples
let b = Bitwise::<u128>(0);
assert![b.set_value_range_checked_strict(0b100, 0, 1).is_err()];
Source§

impl Bitwise<u128>

§Unset ops for u128.

Source

pub const fn is_unset(self, nth: u32) -> bool

Returns true if the nth bit is 0.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert![b.is_unset(6)];
Source

pub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is unset to 0.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert![b.is_unset_checked(6).unwrap()];
Source

pub const fn unset(self, nth: u32) -> Self

Unsets the nth bit.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert_eq![0b0010_0000, b.unset(7).0];
Source

pub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Unsets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_checked(7).unwrap().0];
Source

pub const fn is_unset_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 0.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1000_0001);
assert![b.is_unset_range(1, 6)];
Source

pub const fn is_unset_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 0.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1000_0001);
assert![b.is_unset_range_checked(1, 6).unwrap()];
Source

pub const fn unset_range(self, start: u32, end: u32) -> Self

Unsets the bits in self to 0, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range(1, 4).0];
Source

pub const fn unset_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Unsets the bits in self to 0, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range_checked(1, 4).unwrap().0];
Source

pub const fn unset_all(self) -> Self

Unsets all the bits to 0.

§Examples
let b = Bitwise::<u128>(0b1111_1111);
assert_eq![0, b.unset_all().0];
Source§

impl Bitwise<u128>

§Flip ops for u128.

Source

pub const fn flip(self, nth: u32) -> Self

Flips the nth bit.

§Panics

Panics in debug mode if `nth >= BITS.

§Examples
let b = Bitwise::<u128>(0b0000_0001);
assert_eq![0, b.flip(0).0];
Source

pub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Flips the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<u128>(0b0000_0001);
assert_eq![0, b.flip_checked(0).unwrap().0];
Source

pub const fn flip_range(self, start: u32, end: u32) -> Self

Flips the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range(4, 7).0];
Source

pub const fn flip_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Flips the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range_checked(4, 7).unwrap().0];
Source

pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self

Conditionally flips bits in a range if cond is true.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if(4, 7, false).0];
Source

pub const fn flip_range_if_checked( self, start: u32, end: u32, cond: bool, ) -> Result<Self, MismatchedBounds>

Conditionally flips bits in a range if cond is true.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if_checked(4, 7, false).unwrap().0];
Source§

impl Bitwise<u128>

§Reverse ops for u128.

Source

pub const fn reverse_range(self, start: u32, end: u32) -> Self

Reverses the order of the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range(1, 4).0];
Source

pub const fn reverse_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Reverses the order of the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<u128>

§Count ops for u128.

Source

pub const fn count_ones_range(self, start: u32, end: u32) -> u32

Counts the number of 1s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1011_0110);
assert_eq![3, b.count_ones_range(1, 4)];
Source

pub const fn count_ones_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 1s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1011_0110);
assert_eq![3, b.count_ones_range_checked(1, 4).unwrap()];
Source

pub const fn count_zeros_range(self, start: u32, end: u32) -> u32

Counts the number of 0s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1011_0110);
assert_eq![1, b.count_zeros_range(1, 4)];
Source

pub const fn count_zeros_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 0s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1011_0110);
assert_eq![1, b.count_zeros_range_checked(1, 4).unwrap()];
Source§

impl Bitwise<u128>

§Find ops for u128.

Source

pub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range(0, 7)];
Source

pub const fn find_first_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 0 in self from the [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range(0, 7)];
Source

pub const fn find_first_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 0 in self from the checked [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range(0, 7)];
Source

pub const fn find_last_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 0 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<u128>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range(0, 7)];
Source

pub const fn find_last_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 0 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<u128>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range_checked(0, 7).unwrap()];
Source§

impl Bitwise<usize>

§Implementations for usize.

§Constants and mask constructors for usize.

Source

pub const BITS: u32 = usize::BITS

The size in bits.

Source

pub const fn mask_range(start: u32, end: u32) -> Self

Returns a bitmask of ones from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<usize>::mask_range(1, 4).0]
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    2.8 ns   163.0 ns
  64    2.0 ns    21.6 ns
  32    2.4 ns    12.1 ns
  16    2.6 ns     7.6 ns
   8    2.9 ns     8.4 ns
Source

pub const fn mask_range_checked( start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Returns a bitmask of ones from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
assert_eq![0b0001_1110, Bitwise::<usize>::mask_range_checked(1, 4).unwrap().0];
§Benchmarks

The current algorithm used can be more than 1 order of magnitude more efficient than a naive loop that sets the individual bits, as well as much more consistant across the spectrum of bitsizes.

The following table shows the compared benchmark for start=0 and end=BITS-1 measured on an i5-8350U CPU @ 1.70 Ghz:

bits   current    naive
----   -------   --------
 128    3.7 ns   145.0 ns
  64    3.2 ns    38.2 ns
  32    3.2 ns     9.0 ns
  16    3.2 ns     7.0 ns
   8    3.1 ns     6.7 ns
Source

pub const fn is_set_mask(self, mask: usize) -> bool

Returns true if any bit selected by mask is 1 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert![b.is_set_mask(0b1000_0000)];
Source

pub const fn set_mask(self, mask: usize) -> Self

Sets all bits selected by mask to 1.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert_eq![0b1010_1111, b.set_mask(0b0000_1111).0];
Source

pub const fn is_unset_mask(self, mask: usize) -> bool

Returns true if all bits selected by mask are 0 in self.

Bits not included in mask are ignored.

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert![b.is_unset_mask(0b0100_0000)];
Source

pub const fn unset_mask(self, mask: usize) -> Self

Sets all bits selected by mask to 0.

Bits not included in mask are left unchanged.

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_mask(0b1000_0000).0];
Source§

impl Bitwise<usize>

§Get methods for usize.

Source

pub const fn get_range(self, start: u32, end: u32) -> Self

Gets the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range(1, 4).0];
Source

pub const fn get_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1011_0110);
assert_eq![0b0001_0110, b.get_range_checked(1, 4).unwrap().0];
Source

pub const fn get_value_range(self, start: u32, end: u32) -> Self

Gets the value of the bits in self from the [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1011_0110);
assert_eq![0b1011, b.get_value_range(1, 4).0];
Source

pub const fn get_value_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Gets the value of the bits in self from the checked [start..=end] range.

Sets the rest of the bits to 0.

The bits in the specified range are shifted rightwards so that the least significant bit (LSB) aligns with the units place, forming the integer value.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1011_0110);
assert_eq![0b1011, b.get_value_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<usize>

§Set ops for usize.

Source

pub const fn is_set(self, nth: u32) -> bool

Returns true if the nth bit is 1.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<usize>(0b1000_0000);
assert![b.is_set(7)];
Source

pub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is 1.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<usize>(0b1000_0000);
assert![b.is_set_checked(7).unwrap()];
Source

pub const fn set(self, nth: u32) -> Self

Sets the nth bit.

§Panics

Panics in debug mode if nth >= BITS.

§Examples
let b = Bitwise::<usize>(0);
assert_eq![0b0000_1000, b.set(3).0];
Source

pub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Sets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<usize>(0);
assert_eq![0b0000_1000, b.set_checked(3).unwrap().0];
Source

pub const fn is_set_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 1.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b0011_1000);
assert![b.is_set_range(3, 5)];
Source

pub const fn is_set_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 1.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b0011_1000);
assert![b.is_set_range_checked(3, 5).unwrap()];
Source

pub const fn set_range(self, start: u32, end: u32) -> Self

Sets the bits in self to 1, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range(1, 4).0];
Source

pub const fn set_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the bits in self to 1, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1000_0001);
assert_eq![0b1001_1111, b.set_range_checked(1, 4).unwrap().0];
Source

pub const fn set_all(self) -> Self

Sets all the bits to 1.

§Examples
let b = Bitwise::<usize>(0);
assert_eq![!0 as usize, b.set_all().0];
Source

pub const fn set_value_range(self, value: usize, start: u32, end: u32) -> Self

Sets the given value into the bits from the [start..=end] range.

Leaves the rest of the bits unchanged.

The value is first masked to fit the size of the range, and then it is inserted into the specified bit range of self, replacing the existing bits in that range. The rest of the bits in self remain unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range(0b101, 1, 3).0];
Source

pub const fn set_value_range_checked( self, value: usize, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1000_0001);
assert_eq![0b1000_1011, b.set_value_range_checked(0b101, 1, 3).unwrap().0];
Source

pub const fn set_value_range_checked_strict( self, value: usize, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Sets the given checked value into the bits from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

A value fits when it can be represented by end - start + 1 bits.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS, MismatchedIndices if start > end, and MismatchedCapacity if value does not fit within the specified bit range.

§Examples
let b = Bitwise::<usize>(0);
assert![b.set_value_range_checked_strict(0b100, 0, 1).is_err()];
Source§

impl Bitwise<usize>

§Unset ops for usize.

Source

pub const fn is_unset(self, nth: u32) -> bool

Returns true if the nth bit is 0.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert![b.is_unset(6)];
Source

pub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds>

Returns true if the nth bit is unset to 0.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert![b.is_unset_checked(6).unwrap()];
Source

pub const fn unset(self, nth: u32) -> Self

Unsets the nth bit.

§Panics

Panics in debug mode if `nth >= BITS

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert_eq![0b0010_0000, b.unset(7).0];
Source

pub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Unsets the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert_eq![0b0010_0000, b.unset_checked(7).unwrap().0];
Source

pub const fn is_unset_range(self, start: u32, end: u32) -> bool

Returns true if all bits in the [start..=end] range are 0.

Bits outside the range are ignored.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1000_0001);
assert![b.is_unset_range(1, 6)];
Source

pub const fn is_unset_range_checked( self, start: u32, end: u32, ) -> Result<bool, MismatchedBounds>

Returns true if all bits in the checked [start..=end] range are 0.

Bits outside the range are ignored.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1000_0001);
assert![b.is_unset_range_checked(1, 6).unwrap()];
Source

pub const fn unset_range(self, start: u32, end: u32) -> Self

Unsets the bits in self to 0, from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range(1, 4).0];
Source

pub const fn unset_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Unsets the bits in self to 0, from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1111_1111);
assert_eq![0b1110_0001, b.unset_range_checked(1, 4).unwrap().0];
Source

pub const fn unset_all(self) -> Self

Unsets all the bits to 0.

§Examples
let b = Bitwise::<usize>(0b1111_1111);
assert_eq![0, b.unset_all().0];
Source§

impl Bitwise<usize>

§Flip ops for usize.

Source

pub const fn flip(self, nth: u32) -> Self

Flips the nth bit.

§Panics

Panics in debug mode if `nth >= BITS.

§Examples
let b = Bitwise::<usize>(0b0000_0001);
assert_eq![0, b.flip(0).0];
Source

pub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds>

Flips the nth bit.

§Errors

Returns IndexOutOfBounds if nth >= BITS.

§Examples
let b = Bitwise::<usize>(0b0000_0001);
assert_eq![0, b.flip_checked(0).unwrap().0];
Source

pub const fn flip_range(self, start: u32, end: u32) -> Self

Flips the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range(4, 7).0];
Source

pub const fn flip_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Flips the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert_eq![0b0101_0000, b.flip_range_checked(4, 7).unwrap().0];
Source

pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self

Conditionally flips bits in a range if cond is true.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if(4, 7, false).0];
Source

pub const fn flip_range_if_checked( self, start: u32, end: u32, cond: bool, ) -> Result<Self, MismatchedBounds>

Conditionally flips bits in a range if cond is true.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1010_0000);
assert_eq![0b1010_0000, b.flip_range_if_checked(4, 7, false).unwrap().0];
Source§

impl Bitwise<usize>

§Reverse ops for usize.

Source

pub const fn reverse_range(self, start: u32, end: u32) -> Self

Reverses the order of the bits in self from the [start..=end] range.

Leaves the rest of the bits unchanged.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range(1, 4).0];
Source

pub const fn reverse_range_checked( self, start: u32, end: u32, ) -> Result<Self, MismatchedBounds>

Reverses the order of the bits in self from the checked [start..=end] range.

Leaves the rest of the bits unchanged.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b0001_0110);
assert_eq![0b0001_1010, b.reverse_range_checked(1, 4).unwrap().0];
Source§

impl Bitwise<usize>

§Count ops for usize.

Source

pub const fn count_ones_range(self, start: u32, end: u32) -> u32

Counts the number of 1s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1011_0110);
assert_eq![3, b.count_ones_range(1, 4)];
Source

pub const fn count_ones_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 1s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1011_0110);
assert_eq![3, b.count_ones_range_checked(1, 4).unwrap()];
Source

pub const fn count_zeros_range(self, start: u32, end: u32) -> u32

Counts the number of 0s in self from the [start..=end] range.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1011_0110);
assert_eq![1, b.count_zeros_range(1, 4)];
Source

pub const fn count_zeros_range_checked( self, start: u32, end: u32, ) -> Result<u32, MismatchedBounds>

Counts the number of 0s in self from the checked [start..=end] range.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1011_0110);
assert_eq![1, b.count_zeros_range_checked(1, 4).unwrap()];
Source§

impl Bitwise<usize>

§Find ops for usize.

Source

pub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range(0, 7)];
Source

pub const fn find_first_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1011_0000);
assert_eq![Some(4), b.find_first_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the first 0 in self from the [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range(0, 7)];
Source

pub const fn find_first_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the first 0 in self from the checked [start..=end] range.

Returns None if there are no bits unset.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1111_1011);
assert_eq![Some(2), b.find_first_zero_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 1 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range(0, 7)];
Source

pub const fn find_last_one_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 1 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b0001_0110);
assert_eq![Some(4), b.find_last_one_range_checked(0, 7).unwrap()];
Source

pub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32>

Finds the index of the last 0 in self from the [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Panics

Panics in debug mode if start >= BITS || end >= BITS || start > end.

§Examples
let b = Bitwise::<usize>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range(0, 7)];
Source

pub const fn find_last_zero_range_checked( self, start: u32, end: u32, ) -> Result<Option<u32>, MismatchedBounds>

Finds the index of the last 0 in self from the checked [start..=end] range.

Returns None if there are no bits set.

The index is relative to the entire sequence of self, not to the given start.

§Errors

Returns IndexOutOfBounds if start >= BITS || end >= BITS and MismatchedIndices if start > end.

§Examples
let b = Bitwise::<usize>(0b1110_1111);
assert_eq![Some(4), b.find_last_zero_range_checked(0, 7).unwrap()];

Trait Implementations§

Source§

impl<T> Binary for Bitwise<T>
where T: Binary,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Clone> Clone for Bitwise<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Copy> Copy for Bitwise<T>

Source§

impl<T> Debug for Bitwise<T>
where T: Debug,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Display for Bitwise<T>
where T: Display,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Eq> Eq for Bitwise<T>

Source§

impl<T: Hash> Hash for Bitwise<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Hasher> Hasher for Bitwise<T>

Source§

fn finish(&self) -> u64

Returns the hash value for the values written so far. Read more
Source§

fn write(&mut self, bytes: &[u8])

Writes some data into this Hasher. Read more
1.3.0 · Source§

fn write_u8(&mut self, i: u8)

Writes a single u8 into this hasher.
1.3.0 · Source§

fn write_u16(&mut self, i: u16)

Writes a single u16 into this hasher.
1.3.0 · Source§

fn write_u32(&mut self, i: u32)

Writes a single u32 into this hasher.
1.3.0 · Source§

fn write_u64(&mut self, i: u64)

Writes a single u64 into this hasher.
1.26.0 · Source§

fn write_u128(&mut self, i: u128)

Writes a single u128 into this hasher.
1.3.0 · Source§

fn write_usize(&mut self, i: usize)

Writes a single usize into this hasher.
1.3.0 · Source§

fn write_i8(&mut self, i: i8)

Writes a single i8 into this hasher.
1.3.0 · Source§

fn write_i16(&mut self, i: i16)

Writes a single i16 into this hasher.
1.3.0 · Source§

fn write_i32(&mut self, i: i32)

Writes a single i32 into this hasher.
1.3.0 · Source§

fn write_i64(&mut self, i: i64)

Writes a single i64 into this hasher.
1.26.0 · Source§

fn write_i128(&mut self, i: i128)

Writes a single i128 into this hasher.
1.3.0 · Source§

fn write_isize(&mut self, i: isize)

Writes a single isize into this hasher.
Source§

fn write_length_prefix(&mut self, len: usize)

🔬This is a nightly-only experimental API. (hasher_prefixfree_extras)
Writes a length prefix into this hasher, as part of being prefix-free. Read more
Source§

fn write_str(&mut self, s: &str)

🔬This is a nightly-only experimental API. (hasher_prefixfree_extras)
Writes a single str into this hasher. Read more
Source§

impl<T> LowerExp for Bitwise<T>
where T: LowerExp,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> LowerHex for Bitwise<T>
where T: LowerHex,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Octal for Bitwise<T>
where T: Octal,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Ord> Ord for Bitwise<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for Bitwise<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for Bitwise<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> UpperExp for Bitwise<T>
where T: UpperExp,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> UpperHex for Bitwise<T>
where T: UpperHex,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Bitwise<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Bitwise<T>
where T: RefUnwindSafe,

§

impl<T> Send for Bitwise<T>
where T: Send,

§

impl<T> Sync for Bitwise<T>
where T: Sync,

§

impl<T> Unpin for Bitwise<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Bitwise<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Bitwise<T>
where T: UnwindSafe,

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> AnyExt for T
where T: Any + ?Sized,

Source§

fn type_id() -> TypeId

Returns the TypeId of Self. Read more
Source§

fn type_of(&self) -> TypeId

Returns the TypeId of self. Read more
Source§

fn type_name(&self) -> &'static str

Returns the type name of self. Read more
Source§

fn type_is<T: 'static>(&self) -> bool

Returns true if Self is of type T. Read more
Source§

fn type_hash(&self) -> u64

Returns a deterministic hash of the TypeId of Self.
Source§

fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64

Returns a deterministic hash of the TypeId of Self using a custom hasher.
Source§

fn as_any_ref(&self) -> &dyn Any
where Self: Sized,

Upcasts &self as &dyn Any. Read more
Source§

fn as_any_mut(&mut self) -> &mut dyn Any
where Self: Sized,

Upcasts &mut self as &mut dyn Any. Read more
Source§

fn as_any_box(self: Box<Self>) -> Box<dyn Any>
where Self: Sized,

Available on crate feature alloc only.
Upcasts Box<self> as Box<dyn Any>. Read more
Source§

fn downcast_ref<T: 'static>(&self) -> Option<&T>

Available on crate feature unsafe_layout and non-crate feature safe_code only.
Returns some shared reference to the inner value if it is of type T. Read more
Source§

fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T>

Available on crate feature unsafe_layout and non-crate feature safe_code only.
Returns some exclusive reference to the inner value if it is of type T. 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> ByteSized for T

Source§

const BYTE_ALIGN: usize = _

The alignment of this type in bytes.
Source§

const BYTE_SIZE: usize = _

The size of this type in bytes.
Source§

fn byte_align(&self) -> usize

Returns the alignment of this type in bytes.
Source§

fn byte_size(&self) -> usize

Returns the size of this type in bytes. Read more
Source§

fn ptr_size_ratio(&self) -> [usize; 2]

Returns the size ratio between Ptr::BYTES and BYTE_SIZE. 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Hook for T

Source§

fn hook<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Hooks a mutation step into the value and returns it. Read more
Source§

fn tap<F>(self, f: F) -> Self
where F: FnOnce(&Self),

Taps into the value for observation and returns it unchanged. Read more
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> MemExt for T
where T: ?Sized,

Source§

const NEEDS_DROP: bool = _

Know whether dropping values of this type matters, in compile-time.
Source§

fn mem_align_of<T>() -> usize

Returns the minimum alignment of the type in bytes. Read more
Source§

fn mem_align_of_val(&self) -> usize

Returns the alignment of the pointed-to value in bytes. Read more
Source§

fn mem_size_of<T>() -> usize

Returns the size of a type in bytes. Read more
Source§

fn mem_size_of_val(&self) -> usize

Returns the size of the pointed-to value in bytes. Read more
Source§

fn mem_copy(&self) -> Self
where Self: Copy,

Bitwise-copies a value. Read more
Source§

fn mem_needs_drop(&self) -> bool

Returns true if dropping values of this type matters. Read more
Source§

fn mem_drop(self)
where Self: Sized,

Drops self by running its destructor. Read more
Source§

fn mem_forget(self)
where Self: Sized,

Forgets about self without running its destructor. Read more
Source§

fn mem_replace(&mut self, other: Self) -> Self
where Self: Sized,

Replaces self with other, returning the previous value of self. Read more
Source§

fn mem_take(&mut self) -> Self
where Self: Default,

Replaces self with its default value, returning the previous value of self. Read more
Source§

fn mem_swap(&mut self, other: &mut Self)
where Self: Sized,

Swaps the value of self and other without deinitializing either one. Read more
Source§

unsafe fn mem_zeroed<T>() -> T

Available on crate feature unsafe_layout only.
Returns the value of type T represented by the all-zero byte-pattern. Read more
Source§

unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst

Available on crate feature unsafe_layout only.
Returns the value of type T represented by the all-zero byte-pattern. Read more
Source§

fn mem_as_bytes(&self) -> &[u8]
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &[u8]. Read more
Source§

fn mem_as_bytes_mut(&mut self) -> &mut [u8]
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &mut [u8]. Read more
Source§

impl<T, R> Morph<R> for T
where T: ?Sized,

Source§

fn morph<F>(self, f: F) -> R
where F: FnOnce(Self) -> R, Self: Sized,

Morphs the value into a new one and returns it. Read more
Source§

fn morph_ref<F>(&self, f: F) -> R
where F: FnOnce(&Self) -> R,

Morphs the value by shared reference and returns the result. Read more
Source§

fn morph_mut<F>(&mut self, f: F) -> R
where F: FnOnce(&mut Self) -> R,

Morphs the value by exclusive reference and returns the result. Read more
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.