Could someone please explain why these two pieces of Java codes are behaving differently? First one correctly counts number of bits but the second one just displays 1 or 0 for non-zero numbers. I don't understand whats happening.
public static void printNumUnitBits(int n){
int num=0;
for(int i=0;i<32;i++){
int x=n&1;
num=num+x;
n=n>>>1;
}
System.out.println("Number of one bits:"+num);
}
public static void printNumUnitBits(int n){
int num=0;
for(int i=0;i<32;i++){
num=num+n&1;
n=n>>>1;
}
System.out.println("Number of one bits:"+num);
}