void getBitCombinations(BitSet bs, int start, int current, int target)
{
    if (current == target) {
		// success, so store a copy of the BitSet in a Vector
		v.add(bs.clone());
		return;
    }
    if (start == a.length)
        return;					// failure

    bs.set(start);
    current++;
    getBitCombinations(bs, start + 1, current, target);

    bs.clear(start);
    current--;
    getBitCombinations(bs, start + 1, current, target);
}

Example 1: A simple recursive solution to the problem of enumerating all combinations. In this and other listings, a is the underlying set; I use its length here and pass into the initial call of getBitCombinations() a BitSet of this length. v is a vector in the class's scope.

Back to Article