Here’s the recursive way to get 3 bit set out of 12
final boolean T = true;
final boolean F = false;
int result_count;
boolean is3bits(int no)
{
int ix;
int tot = 0;
for (ix = 0; ix < 12; ix++)
{
if ((no & 1) == 1){tot++;}
no = no >> 1;
}
return tot == 3;
}
void recurs(int num0, int depth)
{
int abit; // this level's bit
int num1; // solution or pass to next recurs
if (depth < 12)
{
// my bit zero and 1
for (abit = 0; abit < 2; abit++)
{
// 'or' this bit into the number
num1 = num0 | (abit << depth);
if (is3bits(num1))
{
// print the solution
println(String.format("%4d %s %4d ", num1, binary(num1, 12), ++result_count));
}
else
{
// continue looking at more bits
recurs(num1, depth + 1);
}
}
}
}
void setup()
{
int num;
int count3 = 0;
if (F) // counting
{
for (num = 0; num < 4096 ; num++)
{
if (is3bits(num))
{
count3++;
println(String.format("%4d %s %4d", num, binary(num, 12), count3));
}
}
}
if (T) // recursing
{
result_count = 0;
recurs(0, 0);
}
exit();
}
void draw(){}
Output
Dec. Bin. Count
3584 111000000000 1
3328 110100000000 2
2816 101100000000 3
1792 011100000000 4
3200 110010000000 5
...
35 000000100011 217
19 000000010011 218
11 000000001011 219
7 000000000111 220