# A Max Value of int in JAVA is 2147483647,but the test case include the value of 2147483648,is the system Wrong?

• I Try to solve this problem using java and not in a bit view. I do like this:
int num=0;
int m;
while(n!=0){
m=n%2;
n=n/2;
if(m==1){
num++;
}
}
return num;

In actually,It is a mathematic way.it goes right in most situation,but the test case includes 2147483648,it is out of range in java.so the answer is wrong.
I want to know is there any way to solve this problem without using bit-operation (like &)?

• actually, I don't think it matters.

int(2147483647) = (01111111111111111111111111111111)binary

and

int(2147483648) = (10000000000000000000000000000000)binary = -2147483648(this is what
computer understands)

It has sth to do with the int storage in java. An int is stored in a 4-byte(32bit) unit.
Any binary form of highest bit starting with 0 means positive number, and 1 means negative number.
for example

(2)decimal = (00000...00010)binary    (30 0s followed by '10')

(-1)decimal = (111111.....1111)binary  (32 1s)

Therefore if you read a 2147483648 into an int unit, the computer will regard it as a negative number.
As for this problem, don't use +-/ to manipulate the bit related problem. You have to think like a computer
(note when dealing with bit, computer knows nothing about +-
/, it only knows <<, >>, &, |, ^)

• but I cannot pass 2147483647 into the method.
call hammingWeight(2147483647);

it pops error, "The literal 2147483647 of type int is out of range".

public static int hammingWeight(int n){}

• 2147483647 should not be out of range. But 2147483648 will
be a problem if you are using int. the answer above makes it clear that
the int in this problem should be treated as unsigned int. do not treat it as signed int
as the default in java

• you should call hammingWeight(Integer.parseUnsigedInt("2147483647"));

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.