Trait Bitwise

Source
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§

Source

fn bit_shl(&mut self, n: usize) -> bool

Bitwise operator << Shift bits to the left

§Parameters
  • data: The data to be shifted
  • n: The number of bits to shift
§Returns
  • true if the leftmost 1 bits 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]);
Source

fn bit_shr(&mut self, n: usize) -> bool

Bitwise operator >> Shift bits to the right

§Parameters
  • data: The data to be shifted
  • n: The number of bits to shift
§Returns
  • true if the rightmost 1 bits 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

Source

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]);
Source

fn bit_not(&mut self) -> &mut Self

Bitwise operator !

§Examples
assert_eq!([0b0000_1111, 0b0000_0011].bit_not(), [0b1111_0000, 0b1111_1100]);
Source

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]);
Source

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]);
Source

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]);
Source

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]);
Source

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]);
Source

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]);
Source

fn bit_all_zero(&self) -> bool

Check if all bits are zero

Source

fn bit_all_one(&self) -> bool

Check if all bits are one

Source

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);
Source

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.

Implementations on Foreign Types§

Source§

impl Bitwise for [u8]

Source§

fn bit_shl(&mut self, n: usize) -> bool

Source§

fn bit_shr(&mut self, n: usize) -> bool

Source§

fn bit_reverse(&mut self) -> &mut Self

Source§

fn bit_not(&mut self) -> &mut Self

Source§

fn bit_be_and(&mut self, other: &Self) -> &mut Self

Source§

fn bit_be_or(&mut self, other: &Self) -> &mut Self

Source§

fn bit_be_xor(&mut self, other: &Self) -> &mut Self

Source§

fn bit_le_and(&mut self, other: &Self) -> &mut Self

Source§

fn bit_le_or(&mut self, other: &Self) -> &mut Self

Source§

fn bit_le_xor(&mut self, other: &Self) -> &mut Self

Source§

fn bit_all_zero(&self) -> bool

Source§

fn bit_all_one(&self) -> bool

Source§

fn bit_leading_zeros(&self) -> usize

Source§

fn bit_trailing_zeros(&self) -> usize

Implementors§