The idea of my algorithm is as following:
- Create an integer value with as many zeros at the right (least significant bits) as given to the function
permut()
.
- Start to move the left most zero bit by bit to the left.
- Treat all the bits to the right of the moved zero as the target value an apply the same algorithm to the shorten value.
The algorithm has two important characteristics:
- It is recursive
- It requires as many computations as values to calculate
Here the algorithm as Java code:
public static void main(String[] args) {
List<Integer> permutations = permut(7);
}
private static List<Integer> permut(int zeros) {
List<Integer> permutations = new ArrayList<>();
permut(zeros == 32 ? 0 : 0xFFFFFFFF << zeros, zeros, 31, permutations);
return permutations;
}
/*
* @param value
* for which to move the zero digit at bit position (zeros - 1)
* to the stopBit position
* @param zeros
* number of 0 digits available at the right most bit positions
* of value
* @param stopBit
* the left most bit position to move the zero digit at bit position
* (zeros - 1) to
* @param values
* to add the newly calculated integer values to
*/
private static void power(int value, int zeros, int stopBit, List<Integer> values) {
values.add(value);
if (zeros == 0) return;
int cleared = value | (1 << (zeros - 1));
for (int bit = zeros - 1; bit < stopBit;) {
power(cleared ^ (1 << ++bit), zeros - 1, bit - 1, values);
}
}
If you are curious if the algorithm behaves correctly, try the following checking methods with the modified main()
method:
public static void main(String[] args) {
int zeros = 7;
List<Integer> permutations = permut(zeros);
System.out.println("Expected number of values: " + combinations(zeros));
System.out.println("Returned number of values: " + permutations.size());
System.out.println("Returned values are unique: " + (new HashSet<>(permutations).size() == permutations.size()));
System.out.printf("All values contain %d zeros: %s\n", zeros, haveZeros(zeros, permutations));
}
private static long combinations(int zeros) {
long p = 1;
long q = 1;
for (int count = 0; count < zeros;) {
p *= (32 - count);
q *= (++count);
}
return p / q;
}
private static boolean haveZeros(int zeros, List<Integer> values) {
for (Integer value : values) {
int count = 0;
for (int bit = 1; bit != 0; bit = bit << 1) {
if ((value & bit) == 0) count++;
}
if (count != zeros) {
System.out.println(Integer.toBinaryString(value));
return false;
}
}
return true;
}