Quantcast
Channel: What is the fastest/most efficient way to find the position of the highest set bit (msb) in an integer in C? - Stack Overflow
Viewing all articles
Browse latest Browse all 73

Answer by Vasil for What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?

$
0
0

Think bitwise operators.

I missunderstood the question the first time. You should produce an int with the leftmost bit set (the others zero). Assuming cmp is set to that value:

position = sizeof(int)*8while(!(n & cmp)){    n <<=1;   position--;}

Viewing all articles
Browse latest Browse all 73

Trending Articles



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