Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

Count Consecutive Leading 1's in Python int Binary Representation

$
0
0

How can Python bit operations be applied to efficiently count up the number of consecutive leading 1's in an integer's binary representation.

For instance,

inegerbinary representation# consecutive leading 1's
00b00
10b11
20b101
30b112
40b1001
50b1011
60b1102
70b1113

This problem can be solved through string manipulation as lambda x: f"{x:b}0".index("0"), but I am looking for a bitwise operation without string conversion.


Viewing all articles
Browse latest Browse all 13891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>