Trait BitOps

Source
pub trait BitOps:
    Copy
    + Integer
    + BitAnd<Output = Self>
    + Shl<Output = Self>
    + Shr<Output = Self>
    + From<u8> {
    // Provided methods
    fn is_flag(&self) -> bool { ... }
    fn is_bit_set(&self, bit: u8) -> bool { ... }
    fn is_flag_set(&self, flag: Self) -> bool { ... }
    fn bits_as_int(&self, bit: u8, count: u8) -> Self { ... }
}
Expand description

Miscellaneous bit operations for any Integer.

§Examples

use bitops::BitOps;

let x = 0b1010_1011_0000_1100; // 0xab0c
let flag = 0b1000;

assert!(flag.is_flag());
assert!(flag.is_bit_set(3));

assert!(x.is_flag_set(flag));
assert_eq!(x.bits_as_int(8, 4), 0xb);

Provided Methods§

Source

fn is_flag(&self) -> bool

Returns whether this number only has one bit set.

§Examples
use bitops::BitOps;

assert!(0b1000.is_flag());
assert!(!0b1001.is_flag());
Source

fn is_bit_set(&self, bit: u8) -> bool

Returns whether the given bit number is set.

§Panics

Panics if bit is greater than the number of bits in this Integer.

§Examples
use bitops::BitOps;

assert!(0b1000.is_bit_set(3));
Source

fn is_flag_set(&self, flag: Self) -> bool

Returns whether the given flag is set.

§Examples
use bitops::BitOps;

assert!(0b11010.is_flag_set(0b11000));
Source

fn bits_as_int(&self, bit: u8, count: u8) -> Self

Returns a number with bit 0 starting at the given bit and up to count left most bits. This is basically a right shift by bit and masked with (1 << count) - 1.

§Panics

Panics if bit is greater than the number of bits in this Integer.

§Examples
use bitops::BitOps;

assert_eq!(0xab000.bits_as_int(12, 8), 0xab);

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.

Implementors§

Source§

impl<N> BitOps for N
where N: Copy + Integer + BitAnd<Output = Self> + Shl<Output = Self> + Shr<Output = Self> + From<u8>,

Implements the BitOps trait for all types that meet the requirements.