title: Extracting rightmost N bits of an integer
body: <p>In the yester Code Jam Qualification round <a href="http://code.google.com/codejam/contest/dashboard?c=433101#s=a&amp;a=0" rel="noreferrer">http://code.google.com/codejam/contest/dashboard?c=433101#s=a&amp;a=0</a> , there was a problem called Snapper Chain. From the contest analysis I came to know the problem requires bit twiddling stuff like extracting the rightmost N bits of an integer and checking if they all are 1. I saw a contestant's(Eireksten) code which performed the said operation like below:</p>
  
  <pre><code>(((K&amp;(1&lt;&lt;N)-1))==(1&lt;&lt;N)-1)
  </code></pre>
  
  <p>I couldn't understand how this works. What is the use of -1 there in the comparison?. If somebody can explain this, it would be very much useful for us rookies. Also, Any tips on identifying this sort of problems would be much appreciated. I used a naive algorithm to solve this problem and ended up solving only the smaller data set.(It took heck of a time to compile the larger data set which is required to be submitted within 8 minutes.). Thanks in advance.</p>