Trait BitTest

Source
pub trait BitTest {
    // Required methods
    fn bit_len(&self) -> usize;
    fn bit(&self, n: usize) -> bool;
}
Expand description

Bit query for integers

§Examples

// query a bit of the number
assert_eq!(0b10010.bit(1), true);
assert_eq!(0b10010.bit(3), false);
assert_eq!(0b10010.bit(100), false);
assert_eq!((-0b10010).bit(1), true);
assert_eq!((-0b10010).bit(3), true);
assert_eq!((-0b10010).bit(100), true);

// query the bit length of the number
assert_eq!(0.bit_len(), 0);
assert_eq!(17.bit_len(), 5);
assert_eq!((-17).bit_len(), 5);
assert_eq!(0b101000000.bit_len(), 9);

Required Methods§

Source

fn bit_len(&self) -> usize

Effective bit length of the binary representation.

For 0, the length is 0.

For positive numbers it is:

  • number of digits in base 2
  • the index of the top 1 bit plus one
  • the floored base-2 logarithm of the number plus one.

For negative numbers it is:

  • number of digits in base 2 without the sign
  • the index of the top 0 bit plus one
  • the floored base-2 logarithm of the absolute value of the number plus one.
Source

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

Returns true if the n-th bit is set in its two’s complement binary representation, n starts from 0.

Implementations on Foreign Types§

Source§

impl BitTest for i8

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for i16

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for i32

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for i64

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for i128

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for isize

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for u8

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for u16

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for u32

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for u64

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for u128

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Source§

impl BitTest for usize

Source§

fn bit_len(&self) -> usize

Source§

fn bit(&self, position: usize) -> bool

Implementors§