pub trait Bitwise {
Show 14 methods
// Required methods
fn bit_shl(&mut self, n: usize) -> bool;
fn bit_shr(&mut self, n: usize) -> bool;
fn bit_reverse(&mut self) -> &mut Self;
fn bit_not(&mut self) -> &mut Self;
fn bit_be_and(&mut self, other: &Self) -> &mut Self;
fn bit_be_or(&mut self, other: &Self) -> &mut Self;
fn bit_be_xor(&mut self, other: &Self) -> &mut Self;
fn bit_le_and(&mut self, other: &Self) -> &mut Self;
fn bit_le_or(&mut self, other: &Self) -> &mut Self;
fn bit_le_xor(&mut self, other: &Self) -> &mut Self;
fn bit_all_zero(&self) -> bool;
fn bit_all_one(&self) -> bool;
fn bit_leading_zeros(&self) -> usize;
fn bit_trailing_zeros(&self) -> usize;
}Expand description
Bitwise operations implementation for [u8]
Required Methods§
Sourcefn bit_shl(&mut self, n: usize) -> bool
fn bit_shl(&mut self, n: usize) -> bool
Bitwise operator <<
Shift bits to the left
§Parameters
data: The data to be shiftedn: The number of bits to shift
§Returns
trueif the leftmost1bits are overflowed
§Examples
let mut data: [u8; 2] = [0b1111_1111, 0b0000_0000];
assert_eq!(data.bit_shl(4), true);
assert_eq!(data, [0b1111_0000, 0b0000_0000]);Sourcefn bit_shr(&mut self, n: usize) -> bool
fn bit_shr(&mut self, n: usize) -> bool
Bitwise operator >>
Shift bits to the right
§Parameters
data: The data to be shiftedn: The number of bits to shift
§Returns
trueif the rightmost1bits are overflowed
§Examples
let mut data = [0b1111_1111, 0b0000_0000];
assert_eq!(data.bit_shr(4), false);
assert_eq!(data, [0b0000_1111, 0b1111_0000]);§Note
The right shift will fill the leftmost bits with 0 and the rightmost bits with the original value
Sourcefn bit_reverse(&mut self) -> &mut Self
fn bit_reverse(&mut self) -> &mut Self
Reverse the bits of the data
§Examples
assert_eq!([0b0000_1111, 0b0000_0011].bit_reverse(), [0b1100_0000, 0b1111_0000]);Sourcefn bit_not(&mut self) -> &mut Self
fn bit_not(&mut self) -> &mut Self
Bitwise operator !
§Examples
assert_eq!([0b0000_1111, 0b0000_0011].bit_not(), [0b1111_0000, 0b1111_1100]);Sourcefn bit_be_and(&mut self, other: &Self) -> &mut Self
fn bit_be_and(&mut self, other: &Self) -> &mut Self
Bitwise operator & for big-endian
§Examples
assert_eq!([0b0011_0011, 0b0011_0011].bit_be_and(&[0b1111_1111]), [0b0000_0000, 0b0011_0011]);Sourcefn bit_be_or(&mut self, other: &Self) -> &mut Self
fn bit_be_or(&mut self, other: &Self) -> &mut Self
Bitwise operator | for big-endian
§Examples
assert_eq!([0b0011_0011, 0b0011_0011].bit_be_or(&[0b1111_1111]), [0b0011_0011, 0b1111_1111]);Sourcefn bit_be_xor(&mut self, other: &Self) -> &mut Self
fn bit_be_xor(&mut self, other: &Self) -> &mut Self
Bitwise operator ^ for big-endian
§Examples
assert_eq!([0b0011_0011, 0b0011_0011].bit_be_xor(&[0b1111_1111]), [0b0011_0011, 0b1100_1100]);Sourcefn bit_le_and(&mut self, other: &Self) -> &mut Self
fn bit_le_and(&mut self, other: &Self) -> &mut Self
Bitwise operator & for little-endian
§Examples
assert_eq!([0b0011_0011, 0b0011_0011].bit_le_and(&[0b1111_1111]), [0b0011_0011, 0b0000_0000]);Sourcefn bit_le_or(&mut self, other: &Self) -> &mut Self
fn bit_le_or(&mut self, other: &Self) -> &mut Self
Bitwise operator | for little-endian
§Examples
assert_eq!([0b0011_0011, 0b0011_0011].bit_le_or(&[0b1111_1111]), [0b1111_1111, 0b0011_0011]);Sourcefn bit_le_xor(&mut self, other: &Self) -> &mut Self
fn bit_le_xor(&mut self, other: &Self) -> &mut Self
Bitwise operator ^ for little-endian
§Examples
assert_eq!([0b0011_0011, 0b0011_0011].bit_le_xor(&[0b1111_1111]), [0b1100_1100, 0b0011_0011]);Sourcefn bit_all_zero(&self) -> bool
fn bit_all_zero(&self) -> bool
Check if all bits are zero
Sourcefn bit_all_one(&self) -> bool
fn bit_all_one(&self) -> bool
Check if all bits are one
Sourcefn bit_leading_zeros(&self) -> usize
fn bit_leading_zeros(&self) -> usize
Count the number of trailing zeros
§Examples
assert_eq!([0b0001_0000, 0b1000_0011].bit_leading_zeros(), 3);
assert_eq!([0b0000_0000, 0b1000_0011].bit_leading_zeros(), 8);
assert_eq!([0b0000_0000, 0b0001_0011].bit_leading_zeros(), 11);
assert_eq!([0b0000_0000, 0b0000_0000].bit_leading_zeros(), 16);Sourcefn bit_trailing_zeros(&self) -> usize
fn bit_trailing_zeros(&self) -> usize
Count the number of leading zeros
§Examples
assert_eq!([0b0000_1111, 0b0000_1000].bit_trailing_zeros(), 3);
assert_eq!([0b0000_1111, 0b0000_0000].bit_trailing_zeros(), 8);
assert_eq!([0b1111_1000, 0b0000_0000].bit_trailing_zeros(), 11);
assert_eq!([0b0000_0000, 0b0000_0000].bit_trailing_zeros(), 16);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.