Home > Enterprise >  Count the amount of 0's at the start of an inverted binary number
Count the amount of 0's at the start of an inverted binary number

Time:10-04

As an exercise, I must convert any regular number to a binary number and then revert the binary number. I did that but now I have to make the program count the amount of 0's at the start of the inverted binary number. I tried to count all the 0's but since I only have to count the amount of 0's BEFORE the first 1, I am stuck. This is what I have so far:

import java.util.*;


class Main {
  public static void main(String[] args) {

    int woord = 20;
    
   String bin = Integer.toBinaryString(woord);
   String test = new StringBuilder(bin).reverse().toString();
  
    System.out.println(test);
    
  }
}

How do I count the 0's at the start of the inverted binary number?

CodePudding user response:

Just do a split on "1" and return the length of the first element.

import java.util.*;

class Main {
  public static void main(String[] args) {

    int woord = 20;

   String bin = Integer.toBinaryString(woord);
   String test = new StringBuilder(bin).reverse().toString();

    System.out.println(test);
    String[] parsed = test.split("1");
    System.out.println(parsed.length > 0 ? parsed[0].length() : "0");
  }
}

CodePudding user response:

Actually, if this is the exercise:

  • Take as input a number as an int
  • Return the amount of zeroes that are at the end of it (after the last '1' bit), when looking at that number in bits

Then you're barking up the wrong tree.

return Integer.numberOfTrailingZeroes(input)

is the one-liner you need. After all, 'count zeroes at the start for the inverted number' is the same as 'count the zeroes at the end for the original'.

Looking at how that method is implemented:

        // HD, Figure 5-14
        int y;
        if (i == 0) return 32;
        int n = 31;
        y = i <<16; if (y != 0) { n = n -16; i = y; }
        y = i << 8; if (y != 0) { n = n - 8; i = y; }
        y = i << 4; if (y != 0) { n = n - 4; i = y; }
        y = i << 2; if (y != 0) { n = n - 2; i = y; }
        return n - ((i << 1) >>> 31);

Straight from the OpenJDK source docs, except it's also a hotspot intrinsic candidate. This code is roughly speaking a few thousand times faster than your attempt, as it doesn't involve any strings.

The real question is, what is this question attempting to test:

  • If it is knowledge of libraries and/or ability to read documentation, then the right answer was Integer.numberOfTrailingZeroes.
  • If it is knowledge of what bits are and how to manipulate them (test your knowledge of bitwise AND, OR, XOR, and left/right shift operations), it's more or less you writing the above implementation.
  • If it's just 'lets see if you can figure it out', then I'd wager that this exercise is mostly pointless.
  • There's a small chance whomever wrote it was really thinking you'd actually flip your number into a binary string, flip the characters, and write a counting loop. However, then it's a bad exercise, in the sense that you're being asked to do something in an incredibly obtuse and inefficient way. The notion of 'work with the bits' sort of screaming at you to use bitwise operations. If this is the explanation, whomever wrote the exercise probably should be focussing a lot more on learning and a lot less on teaching.
  •  Tags:  
  • java
  • Related