title: Counting number of 1 bits in C++ negative number
body: <p>The following function:</p>
  
  <pre><code>int numOnesInBinary(int number) {
      int numOnes = 0;
      while (number != 0) {
          if ((number &amp; 1) == 1) {
              numOnes++;
          }
          number &gt;&gt;= 1;
      }
      return numOnes;
  }
  </code></pre>
  
  <p>will only work for positive numbers, because in the case of a negative number, it always add a 1 to the leftmost bit when doing the >> operation. In Java we can use >>> instead, but how can we do it in C++?
  I read in a book that we can use unsigned integers in C++, but I don't see how since unsigned integers cannot represent negative numbers.</p>