title: two's complement of numbers in python
body: <p>I am writing code that will have negative and positive numbers all 16 bits long with the MSB being the sign aka two's complement. This means the smallest number I can have is <strong>-32768</strong> which is <code>1000 0000 0000 0000</code> in two's complement form. The largest number I can have is <strong>32767</strong> which is <code>0111 1111 1111 1111</code>.</p>
  
  <p>The issue I am having is python is representing the negative numbers with the same binary notation as positive numbers just putting a minus sign out the front i.e. <strong>-16384</strong> is displayed as <code>-0100 0000 0000 0000</code> what I want to be displayed for a number like <strong>-16384</strong> is <code>1100 0000 0000 0000</code>.</p>
  
  <p>I am not quite sure how this can be coded. This is the code i have. Essentially if the number is between 180 and 359 its going to be negative. I need to display this as a twos compliment value. I dont have any code on how to display it because i really have no idea how to do it.</p>
  
  <pre><code>def calculatebearingActive(i):
  
      numTracks = trackQty_Active
      bearing = (((i)*360.0)/numTracks)
      if 0&lt; bearing &lt;=179:
          FC = (bearing/360.0)
          FC_scaled = FC/(2**(-16))
          return int(FC_scaled)
  
      elif 180&lt;= bearing &lt;=359:
          FC = -1*(360-bearing)/(360.0)
          FC_scaled = FC/(2**(-16))
          return int(FC_scaled)
  
      elif bearing ==360:
          FC = 0
          return FC
  </code></pre>