Skip to content

Commit

Permalink
define two methods with conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
GiacomoPope committed Jul 25, 2024
1 parent 8e5d4db commit e2f86e6
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/kyber_py/utilities/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
def bit_count(x):
"""
Count the number of bits in x
import sys

Method to support old python as `x.bit_count()`
was released in Python 3.10 and we currently
support Python 3.9
"""
try:
# int.bit_count() was only made available in 3.10
if sys.version_info >= (3, 10):

def bit_count(x: int) -> int:
"""
Count the number of bits in x
"""
return x.bit_count()
except AttributeError:

else:

def bit_count(x: int) -> int:
"""
Count the number of bits in x
"""
return bin(x).count("1")


Expand Down

0 comments on commit e2f86e6

Please sign in to comment.