How can I find the specific combination of values at a particular index in a truth table?

38 Views Asked by At

I apologize if I'm not using the proper terminology.

But, if I have a code of length y with x possible values, how do I find the values at a particular row if I wrote out a truth table?

For example, if I have a code that is 3 digits and can only be 0 or 1, then I can write a table:

    A B C
    _ _ _
0 | 0 0 0
1 | 0 0 1
2 | 0 1 0
3 | 0 1 1
4 | 1 0 0
5 | 1 0 1
6 | 1 1 0
7 | 1 1 1

How can I determine what A, B, and C are at some arbitrary row without writing all the values out? As, in, at row 6, how can i determine that A=1, B=1, and C=0?

1

There are 1 best solutions below

1
On BEST ANSWER

Look at the rows in your truth table, you'll see that the number on the left is written in base $10$ and the columns $A,B,C$ represent the development of this number in base $2$.

For instance $5=\overline{101}^2=4+1=(1\times 2^2) + (0\times 2^1) + (1\times 2^0)$

So when you have $n$ variables, you will get a decimal number between $0$ and $2^n-1$.

You just need to convert it to a binary number. Instead of $A,B,C,...$ let's call them $A_{n-1},\cdots,A_1,A_0$.

Using C syntax (using shift operations and logical operators) this would be:

Loop x from 0 to 1<<n excluded
  Loop i from 0 to n excluded
    A[i] = (x>>i) & 1

Or in mathematical language $A_i=\lfloor\dfrac x{2^i}\rfloor\mod 2$ for $0\le x<2^n-1$ and $0\le i<n$.