Merge pull request #30 from arkpar/bitscan
Implemented trailing_zeros and leading_zeros
This commit is contained in:
commit
1cc1734310
|
@ -4,7 +4,7 @@ homepage = "http://parity.io"
|
|||
repository = "https://github.com/ethcore/bigint"
|
||||
license = "MIT/Apache-2.0"
|
||||
name = "bigint"
|
||||
version = "4.1.2"
|
||||
version = "4.1.3"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
build = "build.rs"
|
||||
|
||||
|
|
46
src/uint.rs
46
src/uint.rs
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue