title: Bit shifting in internet checksums
body: <p>This is almost certainly a very silly question, but for some reason I'm having trouble with internet checksum calculations.  All of the algorithms basically look something like this:</p>
  
  <pre><code>WORD chksm(WORD *startpos, WORD checklen){
  ulong sum = 0;
  WORD answer = 0;
  
  while (checklen &gt; 1)
  {
      sum += *startpos++;
      checklen -= 2;
  }
  
  if (checklen == 1)
  {
      *(BYTE *)(&amp;answer) = *(BYTE *)startpos;
      sum += answer;
  }
  
  sum = (sum &gt;&gt; 16) + (sum &amp; 0xffff);
  sum += (sum &gt;&gt; 16);
  answer = ~sum;
  
  return answer;}
  </code></pre>
  
  <p>I'm clear on everything except for the line:</p>
  
  <pre><code>sum += (sum &gt;&gt; 16);
  </code></pre>
  
  <p>It looks like the line immediately before it adds the top 16 bits to the bottom 16 bits, leaving all zeroes in the top 16 bits.  If that's the case, then wouldn't sum >> 16 now be equal to zero? And if so, why is that line there?</p>
  
  <p>Or am I (likely) just having a complete mental failure today? </p>