Home > Back-end >  Given x gates, write loop to go through all possible gate combinations?
Given x gates, write loop to go through all possible gate combinations?

Time:05-30

I am trying to print out the full truth table with a variable dependent number of inputs, but I cant figure out any algorithm to achieve that. I want my function to look like this:

int report(CallBack myfunctions, int gates)

CallBack is a pointer to a function and gates is an integer reffering to the number of inputs. For example typing report(and, 3) would produce this output

0000
0010
0100
0110
1000
1010
1100
1111

First 3 columns represent all possible combinations from the 3 inputs, 4th column contains the 'AND' result of each row. My problem it that i cant find a way to go from one line to the next. I've tried utilising the property of each number to be written as the sum of powers of 2 (eg. 14 = 2^3 2^2 2^1) but I cant find a way to make it work for a large number of inputs because the number of columns increases accordingly. NOTE: number of gates doesnt have to be infinite, up to 20 is enough

I should also point out that the AND function calculates the result through a linked list (as its only parameter) and each node of the linked list contains an input (eg. 0->1->1->0->0).

For the creation of the linked list i've made 2 functions (and heres the struct as well):

typedef struct data
{
    int value;
    struct data * next;
} Data;
typedef Data * DataList;

Data * createData(int value)
{
    Data * dataptr;
    dataptr = malloc(sizeof (Data));
    dataptr->value = value;
    dataptr->next = NULL;
    return dataptr;
}
void appendData(DataList *lstptr, Data *newptr)
{
    if (*lstptr==NULL)
    {
        *lstptr = newptr;
        return;
    }
    appendData( &((*lstptr)->next), newptr);
    return;
}
 

CodePudding user response:

If you have n inputs, then you can achieve what you want as follows:

  1. For i = 0 to 2^n-1,
    1. For j = 0 to n-1,
      1. Set inputs[j] to ( i >> k ) & 1.
    2. Do something with inputs.

A more complex approach that possibly does less work.[1]

  1. Set overflow to 0.
  2. For j = 0 to n-1,
    1. Set inputs[j] to 0.
  3. Loop,
    1. Do something with inputs.
    2. Set j to 0.
    3. Loop,
      1. If inputs[j] is 0,
        1. Set inputs[j] to 1.
        2. Break out of loop.
      2. Else,
        1. Set inputs[j] to 0.
        2. Increment j.
        3. If j is equal to n,
          1. Set overflow to 1.
          2. Break out of loop.
    4. If overflow,
      1. Break out of loop.

Finally, another approach is to leave the bits in their "packed" form and pass them to the callback as an integer instead of an array.

  1. For input = 0 to 2^n-1,
    1. Do something with input.

The callback for a 3-bit AND would look like this:

int and3( uint64_t input ) {
   return input == 7 ? 1 : 0;
}

Footnotes.

  1. It can also handle a larger number of inputs, but the first approach can already handle 64 inputs, which can't be completed by a modern computer.
  •  Tags:  
  • c
  • Related