/**

  • 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
  • /
    public class Text11
    {
    public int NumberOf1(int n)
    {
      int count = 0;
      while (n != 0) {
          ++count;
          n = (n - 1) & n;
      }
      return count;
    }
    public int NumberOf1V2(int n)
    {
      char[] c=Integer.toBinaryString(n).toCharArray();
      int count=0;
      for (int i = 0; i < c.length; i++) {
          if (c[i]=='1') {
              count++;
          }
      }
      return count;
    }
    public int NumberOf1V3(int n)
    {
      String s=Integer.toBinaryString(n);
      int count=0;
      for (int i = 0; i < s.length(); i++) {
          if (s.charAt(i)=='1') {
              count++;
          }
      }
      return count;
    }
    }