1. 程式人生 > >輸入一個整數,輸出該數二進制表示中1的個數。其中負數用補碼表示。

輸入一個整數,輸出該數二進制表示中1的個數。其中負數用補碼表示。

char bin obi int 表示 blog binary 補碼 charat

public class Solution {
public int NumberOf1(int n) {
int count=0;
String str=Integer.toBinaryString(n);
for(int x=0;x<str.length();x++)
{
if(str.charAt(x)==‘1‘)
count++;
}
return count;
}
}

輸入一個整數,輸出該數二進制表示中1的個數。其中負數用補碼表示。