]> granicus.if.org Git - python/commitdiff
issue23591: optimize _high_bit()
authorEthan Furman <ethan@stoneleaf.us>
Fri, 2 Sep 2016 22:50:21 +0000 (15:50 -0700)
committerEthan Furman <ethan@stoneleaf.us>
Fri, 2 Sep 2016 22:50:21 +0000 (15:50 -0700)
Lib/enum.py

index 83696313a45eecc566775bfdf599768dbb424ea2..8d23933d3ed7d63bf639eba65924afda576c08af 100644 (file)
@@ -784,13 +784,8 @@ class IntFlag(int, Flag):
 
 
 def _high_bit(value):
-    """return the highest bit set in value"""
-    bit = 0
-    while 'looking for the highest bit':
-        limit = 2 ** bit
-        if limit > value:
-            return bit - 1
-        bit += 1
+    """returns index of highest bit, or -1 if value is zero or negative"""
+    return value.bit_length() - 1 if value > 0 else -1
 
 def unique(enumeration):
     """Class decorator for enumerations ensuring unique member values."""