Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0443953

Browse files
committed
issue23591: optimize _high_bit()
1 parent 1a05d6c commit 0443953

1 file changed

Lines changed: 2 additions & 7 deletions

File tree

Lib/enum.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -784,13 +784,8 @@ def __invert__(self):
784784

785785

786786
def _high_bit(value):
787-
"""return the highest bit set in value"""
788-
bit = 0
789-
while 'looking for the highest bit':
790-
limit = 2 ** bit
791-
if limit > value:
792-
return bit - 1
793-
bit += 1
787+
"""returns index of highest bit, or -1 if value is zero or negative"""
788+
return value.bit_length() - 1 if value > 0 else -1
794789

795790
def unique(enumeration):
796791
"""Class decorator for enumerations ensuring unique member values."""

0 commit comments

Comments
 (0)