diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 7db7d69d1..615e8a2f6 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -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)]