From a21c6fb8ee810133ed8b00a46835a9c82915b703 Mon Sep 17 00:00:00 2001 From: TheAnyKey Date: Sun, 31 May 2020 21:43:42 +0000 Subject: [PATCH] implementation of Py310 int.bit_count --- vm/src/obj/objint.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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)]