Implemented trailing_zeros and leading_zeros

This commit is contained in:
arkpar 2017-09-06 14:41:40 +02:00
parent 65ec18da4d
commit 1268bc2363
No known key found for this signature in database
GPG Key ID: CF9ADE6B115EB4DD
1 changed files with 46 additions and 0 deletions

View File

@ -622,6 +622,36 @@ macro_rules! construct_uint {
arr[index / 64] & (1 << (index % 64)) != 0
}
/// Returns the number of leading zeros in the binary representation of self.
pub fn leading_zeros(&self) -> u32 {
let mut r = 0;
for i in 0..$n_words {
let w = self.0[$n_words - i - 1];
if w == 0 {
r += 64;
} else {
r += w.leading_zeros();
break;
}
}
r
}
/// Returns the number of leading zeros in the binary representation of self.
pub fn trailing_zeros(&self) -> u32 {
let mut r = 0;
for i in 0..$n_words {
let w = self.0[i];
if w == 0 {
r += 64;
} else {
r += w.trailing_zeros();
break;
}
}
r
}
/// Return specific byte.
///
/// # Panics
@ -2484,4 +2514,20 @@ mod tests {
assert_eq!(U256::from(1), number);
}
#[test]
fn leading_zeros() {
assert_eq!(U256::from("000000000000000000000001adbdd6bd6ff027485484b97f8a6a4c7129756dd1").leading_zeros(), 95);
assert_eq!(U256::from("f00000000000000000000001adbdd6bd6ff027485484b97f8a6a4c7129756dd1").leading_zeros(), 0);
assert_eq!(U256::from("0000000000000000000000000000000000000000000000000000000000000001").leading_zeros(), 255);
assert_eq!(U256::from("0000000000000000000000000000000000000000000000000000000000000000").leading_zeros(), 256);
}
#[test]
fn trailing_zeros() {
assert_eq!(U256::from("1adbdd6bd6ff027485484b97f8a6a4c7129756dd100000000000000000000000").trailing_zeros(), 92);
assert_eq!(U256::from("1adbdd6bd6ff027485484b97f8a6a4c7129756dd10000000000000000000000f").trailing_zeros(), 0);
assert_eq!(U256::from("8000000000000000000000000000000000000000000000000000000000000000").trailing_zeros(), 255);
assert_eq!(U256::from("0000000000000000000000000000000000000000000000000000000000000000").trailing_zeros(), 256);
}
}