title: Sum of 32 bits after inverting bits, final result going from binary to decimal
body: <p>If my input is 1. Then 1 as 32 bits in binary is 00000000000000000000000000000001. If I invert the bits, its 11111111111111111111111111111110. And if I convert this inverted bit number from binary to decimal, I should get 4294967294. I wrote the following program to do this, but my final sum is wrong despite me being able to invert the bits correctly. I'm getting -3. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class FlippingBits {
  
  public static void main(String[] args) {
      FlippingBits fpb = new FlippingBits();
      int i = 1;
      int index = 0;
      int[] bitArray = new int[32];
      fpb.convertToBin(i, bitArray, index);
  }
  
  private void convertToBin(int decimalInput, int[] unsigned32, int index) {
      if (decimalInput &lt;= 1) {
          unsigned32[index++] = flipBit(decimalInput);
          for (int i = index; i &lt; unsigned32.length; i++) {
              unsigned32[i] = 1;
          }
          printArray(unsigned32);
          System.out.println();
          sumBit(unsigned32);
          return;
      }
      int remainder = decimalInput % 2;
      unsigned32[index] = flipBit(remainder);
      index++;
      convertToBin(decimalInput &gt;&gt; 1, unsigned32, index);
  }
  
  private void sumBit(int[] unsigned32) {
      int sum = 0;
      for (int i = 0; i &lt; unsigned32.length; i++) {
          sum += unsigned32[i] * (int) Math.pow(2, i);
      }
      System.out.println(sum);
  }
  
  private int flipBit(int remainder) {
      if (remainder == 1) {
          return 0;
      } else {
          return 1;
      }
  }
  
  private void printArray(int[] unsigned32) {
      for (int i = 0; i &lt; unsigned32.length; i++) {
          System.out.print(unsigned32[i]);
      }
  }
  
  }
  </code></pre>
  
  <p>I'm not sure what's happening with my sumBit(int[]) method. I'm pretty sure I haven't forgotten how to convert from binary to decimal.</p>