Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a truth table in excel

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

Example

sample 4-bit truth table

like image 601
Jonathan Camilleri Avatar asked Dec 16 '15 14:12

Jonathan Camilleri


2 Answers

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.

like image 112
Jonathan Camilleri Avatar answered Sep 25 '22 13:09

Jonathan Camilleri


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.

like image 44
DavisDude Avatar answered Sep 23 '22 13:09

DavisDude