implementation of Py310 int.bit_count

This commit is contained in:
TheAnyKey authored and Jeong YunWon committed 2020-06-17 18:42:21 +09:00
1 parent 6bcd1da76b
commit a21c6fb8ee
1 file changed
+12
+12
View File
@@ -653,6 +653,18 @@ impl PyInt {
fn denominator(&self) -> usize {
1
}
#[pymethod]
/// Returns the number of ones 1 an int. When the number is < 0,
/// then it returns the number of ones of the absolute value.
fn bit_count(&self) -> u32 {
self.value
.to_u32_digits()
.1
.iter()
.map(|n| n.count_ones())
.sum()
}
}
#[derive(FromArgs)]