title: Compute the checksum for a binary file in Python
body: <p>I have a binary file(2 bytes), whose content in Hex Format are as follows :</p>
  
  <pre><code>00 00 00 DD 11 22 33 44
  </code></pre>
  
  <p>In binary the content looks as follows(little endian format) :</p>
  
  <pre><code>11011101 00000000 00000000 00000000 01000100 00110011 00100010 00010001 
  </code></pre>
  
  <p>I want to manually compute the checksum of the file and append it at the end of the file. For computing I am considering 4 bytes chunk at a time.</p>
  
  <p>Then the manual steps should look as follows :</p>
  
  <pre><code>   11011101 00000000 00000000 00000000
  + 
     01000100 00110011 00100010 00010001
    --------------------------------------
  =  00100001 00110011 00100010 00010001
  
     11011110 11001100 11011101 11101110  (1's complement)
  +
     00000000 00000000 00000000 00000001  (add 1) &lt;br/&gt;
  -----------------------------------------
  =  11011110 11001100 11011101 11101111  (2's complement) = CheckSum
  </code></pre>
  
  <p>Please correct me, if I have computed the checksum wrongly.
  Code I have written to compute the checksum in the system doesn't works fine.</p>
  
  <pre><code>add = 0
  with open("temp.bin", "rb") as f:
      ## Read first 4 bytes of data
      byte = f.read(4)
      while byte != "":
          add += int.from_bytes(byte, byteorder="little")
      print(b"%02X" % (~add &amp; 0xFFFFFFFF))
  </code></pre>
  
  <p>Please help me with the above problem and code.</p>