 <p><code>b &amp; 1</code> tests if b is odd. If it is, <code>result</code> is set to <code>tmp * power</code>.</p> <p><code>b &gt;&gt;= 1</code> divides b by 2. If the result is non-zero, <code>tmp</code> is set to <code>power</code>, and <code>power</code> is set to <code>tmp * tmp</code>.</p> <p>Eventually, <code>b</code> is divided by 2 so much that it reaches zero, which ends the <code>while</code> loop.</p> <p>The algorithm is a generalization of so-called "<a href="http://mathforum.org/dr.math/faq/faq.peasant.html" rel="nofollow">Russian Peasant multiplication</a>" called "<a href="http://www.wikiwand.com/en/Exponentiation_by_squaring" rel="nofollow">Exponentiation by squaring</a>". The same basic process can be used for exponentiation instead of multiplication, by squaring the intermediate result (which we see in the second <code>if</code> test) rather than doubling.</p> <p>Time complexity proportional to the highest set bit in <code>b</code>; if the highest set bit in <code>b</code> is bit <em>K</em> then the loop will iterate <em>K</em> times. That is, time complexity is proportional to the base 2 logarithm of b.</p>