/// Returns the number of bits necessary to represent this value. Note that zero
/// is considered to need 0 bits.
pub fn bit_length(&self) -> usize {
- // Skip over the most significant digits which are zero.
+ let digitbits = <$ty>::BITS as usize;
let digits = self.digits();
- let zeros = digits.iter().rev().take_while(|&&x| x == 0).count();
- let end = digits.len() - zeros;
- if end == 0 {
+ // Find the most significant non-zero digit.
+ let msd = digits.iter().rposition(|&x| x != 0);
+ match msd {
+ Some(msd) => msd * digitbits + digits[msd].log2() as usize + 1,
// There are no non-zero digits, i.e., the number is zero.
- return 0;
+ _ => 0,
}
- let digitbits = <$ty>::BITS as usize;
- let end_leading_zeros = digits[end - 1].leading_zeros() as usize;
- end * digitbits - end_leading_zeros
}
/// Adds `other` to itself and returns its own mutable reference.