I'm looking for the mathematically equation/algorithm to create the variable columns in a truth table. I really want to understand how this is done, so any directions will be a plus.
Ok, so imagine we have a truth table with n variables. The number of rows in the table will be 2^n. Now we want to create the rows and columns in our array and populate them with their truth values.
For example if I have n = 2. I'll have 2^2 = 4 rows. So I'll create an array like this:
- xy
- 01
- 00
- 11
- 10
That was easy, but now imagine I have n = x variables. Where x can be any whole number greater than zero.
For example, x = 9. I'll have 2^9 = 512 rows and 9 columns. Here are a few of the rows (I don't know if its right):
- 111111111
- 111111110
- 111111101
- 111111000
- 111110011
- 111100010
- ...
I'm thinking the equation/algorithm is:
1) start from the right and alternate true and false
2) move one to the left, increase alternation group count, alternate
3) repeat step 2 until all the variables are touched.
It this right? Am I missing a theoretical maximum number of variables size? What about very small tables where the first variable has half of its values true and half false (it's not the case when you have 9 variables and 512 rows is it?)
Thanks so much!
On your top row, you start with all 1's, and flip them to make the 0's. What if we start with all 0's instead, and do it like this:
Notice the pattern?