This is not a good way to implement clz.  There are nice branch-free methods for these:<p><pre><code>    &#x2F;* Count no. trailing zeros *&#x2F;

    int ntz(unsigned x)
    {
        &#x2F;* Set all trailing zeros to ones *&#x2F;
        &#x2F;* And clear leading zeros *&#x2F;
        x = ~x &amp; (x - 1);
        &#x2F;* Now get population count *&#x2F;
        x = x - ((x &gt;&gt; 1) &amp; 0x55555555);
        x = (x &amp; 0x33333333) + ((x &gt;&gt; 2) &amp; 0x33333333);
        x = (x + (x &gt;&gt; 4)) &amp; 0x0F0F0F0F;
        x = x + (x &gt;&gt; 8);
        x = x + (x &gt;&gt; 16);
        return x &amp; 0x3F;
    }

    &#x2F;* Count leading zeros *&#x2F;
    &#x2F;* log2(x) is 31-nlz(x) *&#x2F;

    int nlz(unsigned x)
    {
        &#x2F;* Make all bits to the right of the first 1 a 1 *&#x2F;
        x |= (x &gt;&gt; 1);
        x |= (x &gt;&gt; 2);
        x |= (x &gt;&gt; 4);
        x |= (x &gt;&gt; 8);
        x |= (x &gt;&gt; 16);
        &#x2F;* Make all bits to the left 1s *&#x2F;
        x = ~x;
        &#x2F;* Population count *&#x2F;
        x = x - ((x &gt;&gt; 1) &amp; 0x55555555);
        x = (x &amp; 0x33333333) + ((x &gt;&gt; 2) &amp; 0x33333333);
        x = (x + (x &gt;&gt; 4)) &amp; 0x0F0F0F0F;
        x = x + (x &gt;&gt; 8);
        x = x + (x &gt;&gt; 16);
        return x &amp; 0x3F;
    }
</code></pre>
(these are from Hacker&#x27;s Delight)