BitOps

Trait BitOps 

Source
pub trait BitOps: Copy + Sized {
Show 16 methods // Required methods fn set_bit(self, bit: Self) -> Self; fn set_bit_exact(self, bit: Self, value: bool) -> Self; fn clear_bit(self, bit: Self) -> Self; fn is_set(self, bit: Self) -> bool; fn get_bit(self, bit: Self) -> Self; fn toggle_bit(self, bit: Self) -> Self; fn toggle_bits(self, bits: Self, shift: Self) -> Self; fn set_bits(self, value: Self, value_bits: Self, value_shift: Self) -> Self; fn set_bits_n(self, ops: &[(Self, Self, Self)]) -> Self; fn set_bits_exact( self, value: Self, value_bits: Self, value_shift: Self, ) -> Self; fn set_bits_exact_n(self, ops: &[(Self, Self, Self)]) -> Self; fn clear_bits(self, clear_mask: Self) -> Self; fn highest_bit(self) -> Option<Self>; fn lowest_bit(self) -> Option<Self>; fn get_bits(self, value_bits: Self, value_shift: Self) -> Self; fn create_mask(bits: Self) -> Self;
}
Expand description

Common bitwise operations to manipulate the bits in raw integers.

Required Methods§

Source

fn set_bit(self, bit: Self) -> Self

Sets the given bit to 1.

The bit position starts at 0.

§Parameters
  • bit: Bit to set, starting at position 0.
Source

fn set_bit_exact(self, bit: Self, value: bool) -> Self

Sets the given bit to the given value.

The bit position starts at 0.

§Parameters
  • bit: Bit to set, starting at position 0.
  • value: Value to set.
Source

fn clear_bit(self, bit: Self) -> Self

Clears the given bit by setting it to 0.

The bit position starts at 0.

§Parameters
  • bit: Bit to clear, starting at position 0.
Source

fn is_set(self, bit: Self) -> bool

Returns whether the given bit is set.

The bit position starts at 0.

§Parameters
  • bit: Bit to check, starting at position 0.
Source

fn get_bit(self, bit: Self) -> Self

Returns the integer value of the given bit (0 or 1).

The bit position starts at 0.

§Parameters
  • bit: Bit to get, starting at position 0.
Source

fn toggle_bit(self, bit: Self) -> Self

Toggles (flips) the given bit.

The bit position starts at 0.

§Parameters
  • bit: Bit to toggle, starting at position 0.
Source

fn toggle_bits(self, bits: Self, shift: Self) -> Self

Toggles (flips) the specified contiguous bits.

§Parameters
  • bits: Amount of bits of value that are relevant.
  • shift: Relevant position of bits inside value, starting from the right/LSB (0).
Source

fn set_bits(self, value: Self, value_bits: Self, value_shift: Self) -> Self

Sets the bits of value in self without clearing already set bits.

§Parameters
  • value: New value/bits to be set in base.
  • value_bits: Amount of bits of value that are relevant.
  • value_shift: Position of value inside self, starting from the right/LSB (0).
§Panics

This function panics for overflowing shifts and bit positions that are outside the range of the underlying type.

Source

fn set_bits_n(self, ops: &[(Self, Self, Self)]) -> Self

Version of Self::set_bits that applies a list of multiple values to the base.

§Parameters
  • base: Base value to alter.
  • ops: Tuple of (value, value_bits, value_shift) where each tuple member corresponds to the parameter in [set_bits].
§Panics

This function panics for overflowing shifts and bit positions that are outside the range of the underlying type.

Source

fn set_bits_exact( self, value: Self, value_bits: Self, value_shift: Self, ) -> Self

Like Self::set_bits but calls Self::clear_bits beforehand for the relevant bits.

§Parameters
  • value: New value/bits to be set in base.
  • value_bits: Amount of bits of value that are relevant.
  • value_shift: Position of value inside self, starting from the right/LSB (0).
§Panics

This function panics for overflowing shifts and bit positions that are outside the range of the underlying type.

Source

fn set_bits_exact_n(self, ops: &[(Self, Self, Self)]) -> Self

Combination of Self::set_bits_exact and Self::set_bits_n.

§Parameters
  • base: Base value to alter.
  • ops: Tuple of (value, value_bits, value_shift) where each tuple member corresponds to the parameter in [set_bits].
§Panics

This function panics for overflowing shifts and bit positions that are outside the range of the underlying type.

Source

fn clear_bits(self, clear_mask: Self) -> Self

Clears all bits specified in the mask by setting them to 0.

§Parameters
  • clear_mask: Bitmask with bits to clear.
Source

fn highest_bit(self) -> Option<Self>

Returns the highest bit that is set, if any.

The bit position starts at 0.

Source

fn lowest_bit(self) -> Option<Self>

Returns the lowest bit that is set, if any.

The bit position starts at 0.

Source

fn get_bits(self, value_bits: Self, value_shift: Self) -> Self

Returns the requested contiguous bits as new integer.

§Parameters
  • value_bits: Amount of bits of value that are relevant.
  • value_shift: Position of value inside self, starting from the right/LSB (0).
Source

fn create_mask(bits: Self) -> Self

Creates a bitmask (1s) with the given amount of contiguous bits.

§Parameters
  • bits: Amount of contiguous bits.

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 BitOps for u8

Source§

fn set_bit(self, bit: Self) -> Self

Wrapper around bitops_u8::set_bit, but as associated function (method) on u8.

Source§

fn set_bit_exact(self, bit: Self, value: bool) -> Self

Wrapper around bitops_u8::set_bit_exact, but as associated function (method) on u8.

Source§

fn clear_bit(self, bit: Self) -> Self

Wrapper around bitops_u8::clear_bit, but as associated function (method) on u8.

Source§

fn is_set(self, bit: Self) -> bool

Wrapper around bitops_u8::is_set, but as associated function (method) on u8.

Source§

fn get_bit(self, bit: Self) -> Self

Wrapper around bitops_u8::get_bit, but as associated function (method) on u8.

Source§

fn toggle_bit(self, bit: Self) -> Self

Wrapper around bitops_u8::toggle_bit, but as associated function (method) on u8.

Source§

fn toggle_bits(self, bits: Self, shift: Self) -> Self

Wrapper around bitops_u8::toggle_bits, but as associated function (method) on u8.

Source§

fn set_bits(self, value: Self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_u8::set_bits, but as associated function (method) on u8.

Source§

fn set_bits_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_u8::set_bits_n, but as associated function (method) on u8.

Source§

fn set_bits_exact( self, value: Self, value_bits: Self, value_shift: Self, ) -> Self

Wrapper around bitops_u8::set_bits_exact, but as associated function (method) on u8.

Source§

fn set_bits_exact_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_u8::set_bits_exact_n, but as associated function (method) on u8.

Source§

fn clear_bits(self, clear_mask: Self) -> Self

Wrapper around bitops_u8::clear_bits, but as associated function (method) on u8.

Source§

fn highest_bit(self) -> Option<Self>

Wrapper around bitops_u8::highest_bit, but as associated function (method) on u8.

Source§

fn lowest_bit(self) -> Option<Self>

Wrapper around bitops_u8::lowest_bit, but as associated function (method) on u8.

Source§

fn get_bits(self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_u8::get_bits, but as associated function (method) on u8.

Source§

fn create_mask(bits: Self) -> Self

Wrapper around bitops_u8::create_mask, but as associated function (method) on u8.

Source§

impl BitOps for u16

Source§

fn set_bit(self, bit: Self) -> Self

Wrapper around bitops_u16::set_bit, but as associated function (method) on u16.

Source§

fn set_bit_exact(self, bit: Self, value: bool) -> Self

Wrapper around bitops_u16::set_bit_exact, but as associated function (method) on u16.

Source§

fn clear_bit(self, bit: Self) -> Self

Wrapper around bitops_u16::clear_bit, but as associated function (method) on u16.

Source§

fn is_set(self, bit: Self) -> bool

Wrapper around bitops_u16::is_set, but as associated function (method) on u16.

Source§

fn get_bit(self, bit: Self) -> Self

Wrapper around bitops_u16::get_bit, but as associated function (method) on u16.

Source§

fn toggle_bit(self, bit: Self) -> Self

Wrapper around bitops_u16::toggle_bit, but as associated function (method) on u16.

Source§

fn toggle_bits(self, bits: Self, shift: Self) -> Self

Wrapper around bitops_u16::toggle_bits, but as associated function (method) on u16.

Source§

fn set_bits(self, value: Self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_u16::set_bits, but as associated function (method) on u16.

Source§

fn set_bits_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_u16::set_bits_n, but as associated function (method) on u16.

Source§

fn set_bits_exact( self, value: Self, value_bits: Self, value_shift: Self, ) -> Self

Wrapper around bitops_u16::set_bits_exact, but as associated function (method) on u16.

Source§

fn set_bits_exact_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_u16::set_bits_exact_n, but as associated function (method) on u16.

Source§

fn clear_bits(self, clear_mask: Self) -> Self

Wrapper around bitops_u16::clear_bits, but as associated function (method) on u16.

Source§

fn highest_bit(self) -> Option<Self>

Wrapper around bitops_u16::highest_bit, but as associated function (method) on u16.

Source§

fn lowest_bit(self) -> Option<Self>

Wrapper around bitops_u16::lowest_bit, but as associated function (method) on u16.

Source§

fn get_bits(self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_u16::get_bits, but as associated function (method) on u16.

Source§

fn create_mask(bits: Self) -> Self

Wrapper around bitops_u16::create_mask, but as associated function (method) on u16.

Source§

impl BitOps for u32

Source§

fn set_bit(self, bit: Self) -> Self

Wrapper around bitops_u32::set_bit, but as associated function (method) on u32.

Source§

fn set_bit_exact(self, bit: Self, value: bool) -> Self

Wrapper around bitops_u32::set_bit_exact, but as associated function (method) on u32.

Source§

fn clear_bit(self, bit: Self) -> Self

Wrapper around bitops_u32::clear_bit, but as associated function (method) on u32.

Source§

fn is_set(self, bit: Self) -> bool

Wrapper around bitops_u32::is_set, but as associated function (method) on u32.

Source§

fn get_bit(self, bit: Self) -> Self

Wrapper around bitops_u32::get_bit, but as associated function (method) on u32.

Source§

fn toggle_bit(self, bit: Self) -> Self

Wrapper around bitops_u32::toggle_bit, but as associated function (method) on u32.

Source§

fn toggle_bits(self, bits: Self, shift: Self) -> Self

Wrapper around bitops_u32::toggle_bits, but as associated function (method) on u32.

Source§

fn set_bits(self, value: Self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_u32::set_bits, but as associated function (method) on u32.

Source§

fn set_bits_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_u32::set_bits_n, but as associated function (method) on u32.

Source§

fn set_bits_exact( self, value: Self, value_bits: Self, value_shift: Self, ) -> Self

Wrapper around bitops_u32::set_bits_exact, but as associated function (method) on u32.

Source§

fn set_bits_exact_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_u32::set_bits_exact_n, but as associated function (method) on u32.

Source§

fn clear_bits(self, clear_mask: Self) -> Self

Wrapper around bitops_u32::clear_bits, but as associated function (method) on u32.

Source§

fn highest_bit(self) -> Option<Self>

Wrapper around bitops_u32::highest_bit, but as associated function (method) on u32.

Source§

fn lowest_bit(self) -> Option<Self>

Wrapper around bitops_u32::lowest_bit, but as associated function (method) on u32.

Source§

fn get_bits(self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_u32::get_bits, but as associated function (method) on u32.

Source§

fn create_mask(bits: Self) -> Self

Wrapper around bitops_u32::create_mask, but as associated function (method) on u32.

Source§

impl BitOps for u64

Source§

fn set_bit(self, bit: Self) -> Self

Wrapper around bitops_u64::set_bit, but as associated function (method) on u64.

Source§

fn set_bit_exact(self, bit: Self, value: bool) -> Self

Wrapper around bitops_u64::set_bit_exact, but as associated function (method) on u64.

Source§

fn clear_bit(self, bit: Self) -> Self

Wrapper around bitops_u64::clear_bit, but as associated function (method) on u64.

Source§

fn is_set(self, bit: Self) -> bool

Wrapper around bitops_u64::is_set, but as associated function (method) on u64.

Source§

fn get_bit(self, bit: Self) -> Self

Wrapper around bitops_u64::get_bit, but as associated function (method) on u64.

Source§

fn toggle_bit(self, bit: Self) -> Self

Wrapper around bitops_u64::toggle_bit, but as associated function (method) on u64.

Source§

fn toggle_bits(self, bits: Self, shift: Self) -> Self

Wrapper around bitops_u64::toggle_bits, but as associated function (method) on u64.

Source§

fn set_bits(self, value: Self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_u64::set_bits, but as associated function (method) on u64.

Source§

fn set_bits_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_u64::set_bits_n, but as associated function (method) on u64.

Source§

fn set_bits_exact( self, value: Self, value_bits: Self, value_shift: Self, ) -> Self

Wrapper around bitops_u64::set_bits_exact, but as associated function (method) on u64.

Source§

fn set_bits_exact_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_u64::set_bits_exact_n, but as associated function (method) on u64.

Source§

fn clear_bits(self, clear_mask: Self) -> Self

Wrapper around bitops_u64::clear_bits, but as associated function (method) on u64.

Source§

fn highest_bit(self) -> Option<Self>

Wrapper around bitops_u64::highest_bit, but as associated function (method) on u64.

Source§

fn lowest_bit(self) -> Option<Self>

Wrapper around bitops_u64::lowest_bit, but as associated function (method) on u64.

Source§

fn get_bits(self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_u64::get_bits, but as associated function (method) on u64.

Source§

fn create_mask(bits: Self) -> Self

Wrapper around bitops_u64::create_mask, but as associated function (method) on u64.

Source§

impl BitOps for u128

Source§

fn set_bit(self, bit: Self) -> Self

Wrapper around bitops_u128::set_bit, but as associated function (method) on u128.

Source§

fn set_bit_exact(self, bit: Self, value: bool) -> Self

Wrapper around bitops_u128::set_bit_exact, but as associated function (method) on u128.

Source§

fn clear_bit(self, bit: Self) -> Self

Wrapper around bitops_u128::clear_bit, but as associated function (method) on u128.

Source§

fn is_set(self, bit: Self) -> bool

Wrapper around bitops_u128::is_set, but as associated function (method) on u128.

Source§

fn get_bit(self, bit: Self) -> Self

Wrapper around bitops_u128::get_bit, but as associated function (method) on u128.

Source§

fn toggle_bit(self, bit: Self) -> Self

Wrapper around bitops_u128::toggle_bit, but as associated function (method) on u128.

Source§

fn toggle_bits(self, bits: Self, shift: Self) -> Self

Wrapper around bitops_u128::toggle_bits, but as associated function (method) on u128.

Source§

fn set_bits(self, value: Self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_u128::set_bits, but as associated function (method) on u128.

Source§

fn set_bits_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_u128::set_bits_n, but as associated function (method) on u128.

Source§

fn set_bits_exact( self, value: Self, value_bits: Self, value_shift: Self, ) -> Self

Wrapper around bitops_u128::set_bits_exact, but as associated function (method) on u128.

Source§

fn set_bits_exact_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_u128::set_bits_exact_n, but as associated function (method) on u128.

Source§

fn clear_bits(self, clear_mask: Self) -> Self

Wrapper around bitops_u128::clear_bits, but as associated function (method) on u128.

Source§

fn highest_bit(self) -> Option<Self>

Wrapper around bitops_u128::highest_bit, but as associated function (method) on u128.

Source§

fn lowest_bit(self) -> Option<Self>

Wrapper around bitops_u128::lowest_bit, but as associated function (method) on u128.

Source§

fn get_bits(self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_u128::get_bits, but as associated function (method) on u128.

Source§

fn create_mask(bits: Self) -> Self

Wrapper around bitops_u128::create_mask, but as associated function (method) on u128.

Source§

impl BitOps for usize

Source§

fn set_bit(self, bit: Self) -> Self

Wrapper around bitops_usize::set_bit, but as associated function (method) on usize.

Source§

fn set_bit_exact(self, bit: Self, value: bool) -> Self

Wrapper around bitops_usize::set_bit_exact, but as associated function (method) on usize.

Source§

fn clear_bit(self, bit: Self) -> Self

Wrapper around bitops_usize::clear_bit, but as associated function (method) on usize.

Source§

fn is_set(self, bit: Self) -> bool

Wrapper around bitops_usize::is_set, but as associated function (method) on usize.

Source§

fn get_bit(self, bit: Self) -> Self

Wrapper around bitops_usize::get_bit, but as associated function (method) on usize.

Source§

fn toggle_bit(self, bit: Self) -> Self

Wrapper around bitops_usize::toggle_bit, but as associated function (method) on usize.

Source§

fn toggle_bits(self, bits: Self, shift: Self) -> Self

Wrapper around bitops_usize::toggle_bits, but as associated function (method) on usize.

Source§

fn set_bits(self, value: Self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_usize::set_bits, but as associated function (method) on usize.

Source§

fn set_bits_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_usize::set_bits_n, but as associated function (method) on usize.

Source§

fn set_bits_exact( self, value: Self, value_bits: Self, value_shift: Self, ) -> Self

Wrapper around bitops_usize::set_bits_exact, but as associated function (method) on usize.

Source§

fn set_bits_exact_n(self, ops: &[(Self, Self, Self)]) -> Self

Wrapper around bitops_usize::set_bits_exact_n, but as associated function (method) on usize.

Source§

fn clear_bits(self, clear_mask: Self) -> Self

Wrapper around bitops_usize::clear_bits, but as associated function (method) on usize.

Source§

fn highest_bit(self) -> Option<Self>

Wrapper around bitops_usize::highest_bit, but as associated function (method) on usize.

Source§

fn lowest_bit(self) -> Option<Self>

Wrapper around bitops_usize::lowest_bit, but as associated function (method) on usize.

Source§

fn get_bits(self, value_bits: Self, value_shift: Self) -> Self

Wrapper around bitops_usize::get_bits, but as associated function (method) on usize.

Source§

fn create_mask(bits: Self) -> Self

Wrapper around bitops_usize::create_mask, but as associated function (method) on usize.

Implementors§