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§
Sourcefn bit_len(&self) -> usize
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.