Deena
Deena

Reputation: 335

Specify number of bits in a String represetation of a binary number

I am trying to write a algorithm that will print a powerset of a given set of numbers. I did that with a loop that goes from zero to 2^length of my set. I convert the index i to binary, and whenever there is a one, I print that number. However, since the string does not have any preceding zeros, I am not getting the right output.

For example, if I have a set of three numbers: {2, 3, 4}, when i is 3, I want the string to be "011", but instead it is "11" and I'm getting an output of 2, 3 instead of 3, 4.

Here is my code:

public static void powerset (int[] A){
        double powerSetLength = Math.pow(2, A.length);
        for (int i=0; i<powerSetLength; i++){
            String bin = Integer.toBinaryString(i);
            System.out.println ("\nbin: " + bin);
            for (int j=0; j<bin.length(); j++){
                if (bin.charAt(j)=='1')
                    System.out.print(A[j] + " ");
            }
        }
        System.out.println();
    }

Here is the output that I am getting:

9 7 2 

bin: 0

bin: 1
9 
bin: 10
9 
bin: 11
9 7 
bin: 100
9 
bin: 101
9 2 
bin: 110
9 7 
bin: 111
9 7 2 

Here is an example of the output that I would like to get:

9 7 2
bin 001
2

I would like to know if there is a way to convert an integer to binary with a specified number of bits so that I can get this output.

Upvotes: 2

Views: 1751

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726609

One easy way to deal with this problem is assuming that if a digit is missing in the representation, then its value is zero. You can do it like this:

// The number of digits you want is A.length
for (int j=0; j < A.length ; j++) {
    // If j is above length, it's the same as if bin[j] were zero
    if (j < b.length() && bin.charAt(j)=='1')
        System.out.print(A[j] + " ");
    }
}

Of course if you can assume that A.length < 64 (which you should be able to assume if you want your program to finish printing in under a year) you could use long to represent your number, and bit operations to check if a bit is set or not:

int len = A.length;
for (long mask = 0 ; mask != (1L << len) ; mask++) {
    for (int i = 0 ; i != len ; i++) {
        if ((mask & (1L << i)) != 0) {
            System.out.print(A[j] + " ");
        }
    }
    System.out.print();
}

Upvotes: 1

James
James

Reputation: 8586

 String padded = String.format("%03d", somenumber);

or

 System.out.printf("%03d", somenumber);

Would each pad to three digits (the 3 in the format specifier). You could additionally build the specifier programatically based on length you need:

 String.format("%0" + n + "d", somenumber)

But this is unnecessary if you just need to know if bit N is set. You could just as easily do this:

if ((value & (1L << n)) != 0) { } 

Where value is the number, and n is the ordinal of the bit you want. This logically ands the bit in question with the value - if it's set, the result is non-zero, and the if is true. If it is not set, the result is zero, and the if is false.

Upvotes: 0

Related Questions