title: writing an 8 bit checksum in C
body: <p>I am having trouble writing an algorithm for a 1byte / 8 bit checksum.
  Obviously with 8bits over a decimal value of 255 the Most significant bits have to wrap around. I think I am doing it correctly.</p>
  
  <p>Here is the code...</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  
  int main(void)
  {
      int check_sum = 0;              //checksum
      int lcheck_sum = 0;             //left checksum bits
      int rcheck_sum = 0;             //right checksum bits
      short int mask = 0x00FF;            // 16 bit mask
  
      //Create the frame - sequence number (S) and checksum 1 byte
  
      int c;
      //calculate the checksum
      for (c = 0; c &lt; length; c++)
      {
          check_sum = (int)buf[c] + check_sum;
          printf("\n Check Sum %d ", check_sum);      //debug
      }
  
      printf("\nfinal Check Sum %d", check_sum);      //debug
  
      //Take checksum and make it a 8 bit checksum
  
      if (check_sum &gt; 255)            //if greater than 8 bits then encode bits
      {
  
          lcheck_sum = check_sum;
          lcheck_sum &gt;&gt; 8;    //shift 8 bits to the right
  
          rcheck_sum = check_sum &amp; mask;
          check_sum = lcheck_sum + rcheck_sum;
      }
  
  
      //Take the complement
      check_sum = ~check_sum;
  
  
      //Truncate - to get rid of the 8 bits to the right and keep the 8 LSB's
      check_sum = check_sum &amp; mask;
  
      printf("\nTruncated and complemented final Check Sum %d\n",check_sum);
      return 0;
  }
  </code></pre>