0

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);  

    }
  }

}

Leksus
  • 1
  • 6
    Why not step through it in the debugger to see what is happening. – Dean Feb 09 '22 at 14:29
  • 1
    `byte` is signed, that `mask` is very different from what you expect it to be, like it starts as `-128`. Even if you used `>>>`, the unsigned right shift operator, it wouldn't help because Java first extends the signed byte into a signed int, then unsigned shifts the 32-bit number to the right, and then converts back to `byte`. You would have to mask it manually before shifting, or just use any other type. See https://ideone.com/gIWWH9 – tevemadar Feb 09 '22 at 14:42
  • I recommend you read [why you shouldn't close the console scanner](https://stackoverflow.com/a/13042296/1552534) . – WJS Feb 09 '22 at 15:29
  • Why not use Integer.toBinaryString(n), and go from there? Also check this: https://stackoverflow.com/questions/2406432/converting-an-int-to-a-binary-string-representation-in-java – pringi Feb 09 '22 at 16:19

1 Answers1

0

You haven't specified how this is to be done. If bit manipulation only then you're on the right track.
However, an alternative would be to use a String approach with regular expressions.

  • set a capture pattern to look for one or more 0 bits.
  • iterate over the data, applying the regex and capture the group.
  • a special check is done for all ones. A regex is used rather than equals to allow for arbitrary
    length strings if desired.
Pattern pat = Pattern.compile("(0+)");
String[] data =
        { "10101111", "11001000", "10011011", "00100010" "00000000","11111111"};

String[] data = { "10101111", "11001000", "10011011",
        "00100010", "00000000", "11111111" };

for (String octet : data) {
    System.out.print(
            octet + " - " + (octet.matches("1+") ? "0" : ""));
    Matcher m = pat.matcher(octet);
    while (m.find()) {
        System.out.print(m.group(1).length() + " ");
    }
    System.out.println();
}

prints

10101111 - 1 1 
11001000 - 2 3 
10011011 - 2 1 
00100010 - 2 3 1 
00000000 - 8 
11111111 - 0

WJS
  • 36,363
  • 4
  • 24
  • 39
  • 1
    Hi! Thank you for your answer! This is supposed to be done only with bit manipulation and we are only allowed to enter a byte type number which is complicating things. I tried to make an array with the numbers binary code but I am kind of lost on how to do that. I tried but I am not good at this lmao XD – Leksus Feb 09 '22 at 17:41
  • Well, I can at least answer the array question. prefixing a value with `0b` expects it to be binary. So `int a = 0b111` gives the value of 7. But you can't do `byte a = 0b11110000;` because the right hand side defaults to an int. So initialize your array like so `byte[] test = {(byte)0b10000000, (byte)0b11011010};` – WJS Feb 09 '22 at 17:48