Generate a truth table in excel
Asked Answered
B

5

10

I need to make a formula that gives you the truth table for a variable number of columns.

Example

sample 4-bit truth table

Bellaude answered 16/12, 2015 at 14:33 Comment(1)
I think this might be on target for a VBA approach.Tacye
B
5

Replace the FirstCell with a static reference to the cell that contains the first 2^1 value e.g. $D$1 for a 4-bit table (16 values) and autofill to the rest of the grid (in the example A1:D16)

=IF(MOD(ROW()-ROW(FirstCell),POWER(2, ((COLUMN() - COLUMN(FirstCell)) * -1) + 1)) >= (POWER(2, ((COLUMN() - COLUMN(FirstCell)) * -1) + 1) / 2),1,0)

The logic behind this is:

If the current row modulus 2 power current column (* -1 as the first value is in the last column and + 1 because it starts from 0) is greater or equal to half of 2 power current column, put the value as 1, else put the value as 0.

Bellaude answered 16/12, 2015 at 14:33 Comment(2)
Heads-up for those using a regional setting with , as decimal separator => replace occurrences of , with ;Steinway
Really helpful! For everyone else, note that "FirstCell" is the top Right Cell of the table.Bradfordbradlee
B
7

The current recommended answer did not work for me. For a simpler method, I'd recommend the following formula:

=IF(MOD(FLOOR((ROW()-ROW(TopRight))/(2^(COLUMN(TopRight)-COLUMN())), 1),2)=0,0,1)

Where TopRight is the top right cell of the truth table.

For instance, if you're creating a truth table with 8 entries that starts in A3, replace TopRight with $H$3, then drag the formula across and down.


A basic explanation of what's going on: In truth tables, the rows alternate 1 or 0 every 2 ^ n number of rows, where n is the the number of columns that the given column is away from the rightmost column.

Brachycephalic answered 1/3, 2019 at 19:5 Comment(0)
B
5

Replace the FirstCell with a static reference to the cell that contains the first 2^1 value e.g. $D$1 for a 4-bit table (16 values) and autofill to the rest of the grid (in the example A1:D16)

=IF(MOD(ROW()-ROW(FirstCell),POWER(2, ((COLUMN() - COLUMN(FirstCell)) * -1) + 1)) >= (POWER(2, ((COLUMN() - COLUMN(FirstCell)) * -1) + 1) / 2),1,0)

The logic behind this is:

If the current row modulus 2 power current column (* -1 as the first value is in the last column and + 1 because it starts from 0) is greater or equal to half of 2 power current column, put the value as 1, else put the value as 0.

Bellaude answered 16/12, 2015 at 14:33 Comment(2)
Heads-up for those using a regional setting with , as decimal separator => replace occurrences of , with ;Steinway
Really helpful! For everyone else, note that "FirstCell" is the top Right Cell of the table.Bradfordbradlee
M
3

The other answers might make Boole sad. This one aims to be more boolean.

You need to populate the first row (2) with 0's

For the LSB column (D) - Invert:

  • =NOT(D2)*1 (formula for cell D3, copied to D4:D17)
  • That will invert the value from the row above. The *1 numification is necessary to avoid seeing TRUE or FALSE

enter image description here

For all other columns - Add:

  • =XOR(AND(D2:$D2),C2)*1 (formula for cell C3, copied to all cells A3:C17)
  • For an ADD function, you want to XOR the value above in the column with the result of ANDing all the bits in all the columns to the right of it. (In other words: if all the bits to the right of the bit above are 1, then you should flip the value from the bit above. This ADD formula works for any number of columns.)
  • The AND range is referenced to one row up and one col right, to the $D LSB column, also one row up. So the $D anchor for the LSB column allows copying to any other column
  • Again, *1 is used for numification of the resulting TRUE/FALSE

enter image description here

Manara answered 26/11, 2020 at 3:44 Comment(0)
T
1

Here's a Microsoft 365 one-liner:

=TRANSPOSE(LET(n,5,m,2^n,x,SEQUENCE(n,m,0),y,FLOOR(x/m,1),z,FLOOR((x-y*m)/2^(n-1-y),1),MOD(z,2)))

n is the number of columns needed, m then stores the length of each column.

The formula finds the row of x and stores the value in y, and then successively halves each row into the correct format, and outputs the result mod 2 to produce the truth table set of inputs.

TRANSPOSE is used because SEQUENCE places the numbers left-right, top-bottom.

Timi answered 2/7, 2021 at 5:47 Comment(1)
My version for a T/F table is =LET(n,3,m,2^n,x,SEQUENCE(n,m,0),y,FLOOR(x/m,1),z,FLOOR((x-y*m)/2^(n-1-y),1),TRANSPOSE(SUBSTITUTE(SUBSTITUTE(MOD(z,2),"1","F"),"0","T")))Thrips
D
-1
0 0 0 0  
0 0 0 1   
0 0 1 0   
0 0 1 1   
0 1 0 0    
.....   

remember the numbers are only 0 or 1.

for column D: D2=1-D1
for column C: C2=IF(D1=1,1-D1,D1)
for column B: B2=IF((C1=1)*(D1=1),1-B1, B1)
.....

After did this, copy the numbers without formulas for your truth table to avoid Excel calculation.

Dustcloth answered 24/6, 2018 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.