I have written code that is supposed to count sequential zeros. When you enter a number, for example 55 which in binary is 0011 0111, the Result is supposed to be 2 and 1, instead I only get 2 and the loop ends.
Can anyone help?
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.print("input number:");
Scanner sc = new Scanner(System.in);
byte n = sc.nextByte();
sc.close();
byte mask = (byte)(128); // 1000 0000
int result;
int zeros = 0;
System.out.print("result:");
System.out.println("");
for(int i=0; i<=8; i++) {
result =(byte) (n & mask);
if (result != 0) result = 1; else result = 0;
if (result == 0) {
zeros++;
}
if (result == 1) {
if (zeros != 0) {
System.out.print(zeros+" ");
}
zeros=0;
}
mask = (byte)(mask >> 1);
}
}
}