#[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.
§Related items
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:
-
- is_set (checked).
- set (checked).
- is_set_range (checked).
- set_range (checked).
- set_all.
- set_value_range (checked, checked_strict).
-
- is_unset (checked).
- unset (checked).
- is_unset_range (checked).
- unset_range (checked).
- unset_all.
-
- flip (checked).
- flip_range (checked).
- flip_range_if (checked).
Tuple Fields§
§0: TImplementations§
Source§impl Bitwise<u8>
impl Bitwise<u8>
Sourcepub const fn mask_range(start: u32, end: u32) -> Self
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 nsSourcepub const fn mask_range_checked(
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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 nsSourcepub const fn is_set_mask(self, mask: u8) -> bool
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)];Sourcepub const fn set_mask(self, mask: u8) -> Self
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];Sourcepub const fn is_unset_mask(self, mask: u8) -> bool
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)];Sourcepub const fn unset_mask(self, mask: u8) -> Self
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.
impl Bitwise<u8>
§Get methods for u8.
Sourcepub const fn get_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn get_value_range(self, start: u32, end: u32) -> Self
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];Sourcepub const fn get_value_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u8>
§Set ops for u8.
Sourcepub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_set_range(self, start: u32, end: u32) -> bool
pub const fn is_set_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_set_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_value_range(self, value: u8, start: u32, end: u32) -> Self
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];Sourcepub const fn set_value_range_checked(
self,
value: u8,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_value_range_checked_strict(
self,
value: u8,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u8>
§Unset ops for u8.
Sourcepub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_unset_range(self, start: u32, end: u32) -> bool
pub const fn is_unset_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_range(self, start: u32, end: u32) -> Self
pub const fn unset_range(self, start: u32, end: u32) -> Self
Sourcepub const fn unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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§impl Bitwise<u8>
§Flip ops for u8.
impl Bitwise<u8>
§Flip ops for u8.
Sourcepub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range(self, start: u32, end: u32) -> Self
pub const fn flip_range(self, start: u32, end: u32) -> Self
Sourcepub const fn flip_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
Sourcepub const fn flip_range_if_checked(
self,
start: u32,
end: u32,
cond: bool,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u8>
§Reverse ops for u8.
Sourcepub const fn reverse_range(self, start: u32, end: u32) -> Self
pub const fn reverse_range(self, start: u32, end: u32) -> Self
Sourcepub const fn reverse_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u8>
§Count ops for u8.
Sourcepub const fn count_ones_range(self, start: u32, end: u32) -> u32
pub const fn count_ones_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_ones_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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()];Sourcepub const fn count_zeros_range(self, start: u32, end: u32) -> u32
pub const fn count_zeros_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_zeros_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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.
impl Bitwise<u8>
§Find ops for u8.
Sourcepub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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>
impl Bitwise<u16>
Sourcepub const fn mask_range(start: u32, end: u32) -> Self
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 nsSourcepub const fn mask_range_checked(
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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 nsSourcepub const fn is_set_mask(self, mask: u16) -> bool
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)];Sourcepub const fn set_mask(self, mask: u16) -> Self
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];Sourcepub const fn is_unset_mask(self, mask: u16) -> bool
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)];Sourcepub const fn unset_mask(self, mask: u16) -> Self
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.
impl Bitwise<u16>
§Get methods for u16.
Sourcepub const fn get_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn get_value_range(self, start: u32, end: u32) -> Self
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];Sourcepub const fn get_value_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u16>
§Set ops for u16.
Sourcepub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_set_range(self, start: u32, end: u32) -> bool
pub const fn is_set_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_set_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_value_range(self, value: u16, start: u32, end: u32) -> Self
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];Sourcepub const fn set_value_range_checked(
self,
value: u16,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_value_range_checked_strict(
self,
value: u16,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u16>
§Unset ops for u16.
Sourcepub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_unset_range(self, start: u32, end: u32) -> bool
pub const fn is_unset_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_range(self, start: u32, end: u32) -> Self
pub const fn unset_range(self, start: u32, end: u32) -> Self
Sourcepub const fn unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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§impl Bitwise<u16>
§Flip ops for u16.
impl Bitwise<u16>
§Flip ops for u16.
Sourcepub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range(self, start: u32, end: u32) -> Self
pub const fn flip_range(self, start: u32, end: u32) -> Self
Sourcepub const fn flip_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
Sourcepub const fn flip_range_if_checked(
self,
start: u32,
end: u32,
cond: bool,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u16>
§Reverse ops for u16.
Sourcepub const fn reverse_range(self, start: u32, end: u32) -> Self
pub const fn reverse_range(self, start: u32, end: u32) -> Self
Sourcepub const fn reverse_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u16>
§Count ops for u16.
Sourcepub const fn count_ones_range(self, start: u32, end: u32) -> u32
pub const fn count_ones_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_ones_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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()];Sourcepub const fn count_zeros_range(self, start: u32, end: u32) -> u32
pub const fn count_zeros_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_zeros_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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.
impl Bitwise<u16>
§Find ops for u16.
Sourcepub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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>
impl Bitwise<u32>
Sourcepub const fn mask_range(start: u32, end: u32) -> Self
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 nsSourcepub const fn mask_range_checked(
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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 nsSourcepub const fn is_set_mask(self, mask: u32) -> bool
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)];Sourcepub const fn set_mask(self, mask: u32) -> Self
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];Sourcepub const fn is_unset_mask(self, mask: u32) -> bool
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)];Sourcepub const fn unset_mask(self, mask: u32) -> Self
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.
impl Bitwise<u32>
§Get methods for u32.
Sourcepub const fn get_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn get_value_range(self, start: u32, end: u32) -> Self
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];Sourcepub const fn get_value_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u32>
§Set ops for u32.
Sourcepub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_set_range(self, start: u32, end: u32) -> bool
pub const fn is_set_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_set_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_value_range(self, value: u32, start: u32, end: u32) -> Self
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];Sourcepub const fn set_value_range_checked(
self,
value: u32,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_value_range_checked_strict(
self,
value: u32,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u32>
§Unset ops for u32.
Sourcepub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_unset_range(self, start: u32, end: u32) -> bool
pub const fn is_unset_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_range(self, start: u32, end: u32) -> Self
pub const fn unset_range(self, start: u32, end: u32) -> Self
Sourcepub const fn unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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§impl Bitwise<u32>
§Flip ops for u32.
impl Bitwise<u32>
§Flip ops for u32.
Sourcepub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range(self, start: u32, end: u32) -> Self
pub const fn flip_range(self, start: u32, end: u32) -> Self
Sourcepub const fn flip_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
Sourcepub const fn flip_range_if_checked(
self,
start: u32,
end: u32,
cond: bool,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u32>
§Reverse ops for u32.
Sourcepub const fn reverse_range(self, start: u32, end: u32) -> Self
pub const fn reverse_range(self, start: u32, end: u32) -> Self
Sourcepub const fn reverse_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u32>
§Count ops for u32.
Sourcepub const fn count_ones_range(self, start: u32, end: u32) -> u32
pub const fn count_ones_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_ones_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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()];Sourcepub const fn count_zeros_range(self, start: u32, end: u32) -> u32
pub const fn count_zeros_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_zeros_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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.
impl Bitwise<u32>
§Find ops for u32.
Sourcepub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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>
impl Bitwise<u64>
Sourcepub const fn mask_range(start: u32, end: u32) -> Self
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 nsSourcepub const fn mask_range_checked(
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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 nsSourcepub const fn is_set_mask(self, mask: u64) -> bool
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)];Sourcepub const fn set_mask(self, mask: u64) -> Self
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];Sourcepub const fn is_unset_mask(self, mask: u64) -> bool
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)];Sourcepub const fn unset_mask(self, mask: u64) -> Self
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.
impl Bitwise<u64>
§Get methods for u64.
Sourcepub const fn get_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn get_value_range(self, start: u32, end: u32) -> Self
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];Sourcepub const fn get_value_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u64>
§Set ops for u64.
Sourcepub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_set_range(self, start: u32, end: u32) -> bool
pub const fn is_set_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_set_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_value_range(self, value: u64, start: u32, end: u32) -> Self
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];Sourcepub const fn set_value_range_checked(
self,
value: u64,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_value_range_checked_strict(
self,
value: u64,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u64>
§Unset ops for u64.
Sourcepub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_unset_range(self, start: u32, end: u32) -> bool
pub const fn is_unset_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_range(self, start: u32, end: u32) -> Self
pub const fn unset_range(self, start: u32, end: u32) -> Self
Sourcepub const fn unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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§impl Bitwise<u64>
§Flip ops for u64.
impl Bitwise<u64>
§Flip ops for u64.
Sourcepub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range(self, start: u32, end: u32) -> Self
pub const fn flip_range(self, start: u32, end: u32) -> Self
Sourcepub const fn flip_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
Sourcepub const fn flip_range_if_checked(
self,
start: u32,
end: u32,
cond: bool,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u64>
§Reverse ops for u64.
Sourcepub const fn reverse_range(self, start: u32, end: u32) -> Self
pub const fn reverse_range(self, start: u32, end: u32) -> Self
Sourcepub const fn reverse_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u64>
§Count ops for u64.
Sourcepub const fn count_ones_range(self, start: u32, end: u32) -> u32
pub const fn count_ones_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_ones_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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()];Sourcepub const fn count_zeros_range(self, start: u32, end: u32) -> u32
pub const fn count_zeros_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_zeros_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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.
impl Bitwise<u64>
§Find ops for u64.
Sourcepub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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>
impl Bitwise<u128>
Sourcepub const fn mask_range(start: u32, end: u32) -> Self
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 nsSourcepub const fn mask_range_checked(
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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 nsSourcepub const fn is_set_mask(self, mask: u128) -> bool
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)];Sourcepub const fn set_mask(self, mask: u128) -> Self
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];Sourcepub const fn is_unset_mask(self, mask: u128) -> bool
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)];Sourcepub const fn unset_mask(self, mask: u128) -> Self
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.
impl Bitwise<u128>
§Get methods for u128.
Sourcepub const fn get_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn get_value_range(self, start: u32, end: u32) -> Self
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];Sourcepub const fn get_value_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u128>
§Set ops for u128.
Sourcepub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_set_range(self, start: u32, end: u32) -> bool
pub const fn is_set_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_set_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_all(self) -> Self
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];Sourcepub const fn set_value_range(self, value: u128, start: u32, end: u32) -> Self
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];Sourcepub const fn set_value_range_checked(
self,
value: u128,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_value_range_checked_strict(
self,
value: u128,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u128>
§Unset ops for u128.
Sourcepub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_unset_range(self, start: u32, end: u32) -> bool
pub const fn is_unset_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_range(self, start: u32, end: u32) -> Self
pub const fn unset_range(self, start: u32, end: u32) -> Self
Sourcepub const fn unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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§impl Bitwise<u128>
§Flip ops for u128.
impl Bitwise<u128>
§Flip ops for u128.
Sourcepub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range(self, start: u32, end: u32) -> Self
pub const fn flip_range(self, start: u32, end: u32) -> Self
Sourcepub const fn flip_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
Sourcepub const fn flip_range_if_checked(
self,
start: u32,
end: u32,
cond: bool,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u128>
§Reverse ops for u128.
Sourcepub const fn reverse_range(self, start: u32, end: u32) -> Self
pub const fn reverse_range(self, start: u32, end: u32) -> Self
Sourcepub const fn reverse_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<u128>
§Count ops for u128.
Sourcepub const fn count_ones_range(self, start: u32, end: u32) -> u32
pub const fn count_ones_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_ones_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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()];Sourcepub const fn count_zeros_range(self, start: u32, end: u32) -> u32
pub const fn count_zeros_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_zeros_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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.
impl Bitwise<u128>
§Find ops for u128.
Sourcepub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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>
impl Bitwise<usize>
Sourcepub const fn mask_range(start: u32, end: u32) -> Self
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 nsSourcepub const fn mask_range_checked(
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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 nsSourcepub const fn is_set_mask(self, mask: usize) -> bool
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)];Sourcepub const fn set_mask(self, mask: usize) -> Self
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];Sourcepub const fn is_unset_mask(self, mask: usize) -> bool
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)];Sourcepub const fn unset_mask(self, mask: usize) -> Self
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.
impl Bitwise<usize>
§Get methods for usize.
Sourcepub const fn get_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn get_value_range(self, start: u32, end: u32) -> Self
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];Sourcepub const fn get_value_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<usize>
§Set ops for usize.
Sourcepub const fn is_set_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_set_range(self, start: u32, end: u32) -> bool
pub const fn is_set_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_set_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn set_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_all(self) -> Self
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];Sourcepub const fn set_value_range(self, value: usize, start: u32, end: u32) -> Self
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];Sourcepub const fn set_value_range_checked(
self,
value: usize,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn set_value_range_checked_strict(
self,
value: usize,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<usize>
§Unset ops for usize.
Sourcepub const fn is_unset_checked(self, nth: u32) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn is_unset_range(self, start: u32, end: u32) -> bool
pub const fn is_unset_range(self, start: u32, end: u32) -> bool
Sourcepub const fn is_unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<bool, MismatchedBounds> ⓘ
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()];Sourcepub const fn unset_range(self, start: u32, end: u32) -> Self
pub const fn unset_range(self, start: u32, end: u32) -> Self
Sourcepub const fn unset_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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§impl Bitwise<usize>
§Flip ops for usize.
impl Bitwise<usize>
§Flip ops for usize.
Sourcepub const fn flip_checked(self, nth: u32) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range(self, start: u32, end: u32) -> Self
pub const fn flip_range(self, start: u32, end: u32) -> Self
Sourcepub const fn flip_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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];Sourcepub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
pub const fn flip_range_if(self, start: u32, end: u32, cond: bool) -> Self
Sourcepub const fn flip_range_if_checked(
self,
start: u32,
end: u32,
cond: bool,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<usize>
§Reverse ops for usize.
Sourcepub const fn reverse_range(self, start: u32, end: u32) -> Self
pub const fn reverse_range(self, start: u32, end: u32) -> Self
Sourcepub const fn reverse_range_checked(
self,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds> ⓘ
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.
impl Bitwise<usize>
§Count ops for usize.
Sourcepub const fn count_ones_range(self, start: u32, end: u32) -> u32
pub const fn count_ones_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_ones_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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()];Sourcepub const fn count_zeros_range(self, start: u32, end: u32) -> u32
pub const fn count_zeros_range(self, start: u32, end: u32) -> u32
Sourcepub const fn count_zeros_range_checked(
self,
start: u32,
end: u32,
) -> Result<u32, MismatchedBounds> ⓘ
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.
impl Bitwise<usize>
§Find ops for usize.
Sourcepub const fn find_first_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_first_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_first_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_one_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_one_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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()];Sourcepub const fn find_last_zero_range(self, start: u32, end: u32) -> Option<u32> ⓘ
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)];Sourcepub const fn find_last_zero_range_checked(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds> ⓘ
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§
impl<T: Copy> Copy for Bitwise<T>
impl<T: Eq> Eq for Bitwise<T>
Source§impl<T: Hasher> Hasher for Bitwise<T>
impl<T: Hasher> Hasher for Bitwise<T>
1.26.0 · Source§fn write_u128(&mut self, i: u128)
fn write_u128(&mut self, i: u128)
u128 into this hasher.1.3.0 · Source§fn write_usize(&mut self, i: usize)
fn write_usize(&mut self, i: usize)
usize into this hasher.1.26.0 · Source§fn write_i128(&mut self, i: i128)
fn write_i128(&mut self, i: i128)
i128 into this hasher.1.3.0 · Source§fn write_isize(&mut self, i: isize)
fn write_isize(&mut self, i: isize)
isize into this hasher.Source§fn write_length_prefix(&mut self, len: usize)
fn write_length_prefix(&mut self, len: usize)
hasher_prefixfree_extras)Source§impl<T: Ord> Ord for Bitwise<T>
impl<T: Ord> Ord for Bitwise<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialOrd> PartialOrd for Bitwise<T>
impl<T: PartialOrd> PartialOrd for Bitwise<T>
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> AnyExt for T
impl<T> AnyExt for T
Source§fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId of Self using a custom hasher.Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§fn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
fn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
alloc only.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> MemExt for Twhere
T: ?Sized,
impl<T> MemExt for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Source§fn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Source§fn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Source§fn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice only.