Skip to content

Commit a21c6fb

Browse files
TheAnyKeyyouknowone
authored andcommitted
implementation of Py310 int.bit_count
1 parent 6bcd1da commit a21c6fb

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

vm/src/obj/objint.rs

+12
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,18 @@ impl PyInt {
653653
fn denominator(&self) -> usize {
654654
1
655655
}
656+
657+
#[pymethod]
658+
/// Returns the number of ones 1 an int. When the number is < 0,
659+
/// then it returns the number of ones of the absolute value.
660+
fn bit_count(&self) -> u32 {
661+
self.value
662+
.to_u32_digits()
663+
.1
664+
.iter()
665+
.map(|n| n.count_ones())
666+
.sum()
667+
}
656668
}
657669

658670
#[derive(FromArgs)]

0 commit comments

Comments
 (0)