Computing Truth Tables in Org
15 Feb 2024 Charles Choi
Truth tables are great way to design and verify combinational logic. Useful as truth tables are, they however can be onerous to write: as the number of inputs grow, so does the size of table in exponential fashion (row size = 2^{number of inputs}). Writing this out is not fun.
Thankfully, enumeration of said input values in Emacs can be automated. This post goes over an approach to generating truth table inputs with which you can use to drive combinational logic in Org.
Inserting the Truth Table Input
Before getting into implementation details, let’s go over inserting truth table input into your current Org buffer.
Invoke the command Mx
cc/inserttruthtableinput
. In the minibuffer you will be prompted for the number of inputs to enumerate for the truth table input values. Entering the value 2
will insert the following table into your buffer. Note that the second and third columns map to 2^{1} and 2^{0} respectively. Reading across a row, the first column shows the decimal value of the input, whereas the remaining columns shows the binary representation of that input value.
1 2 3 4 5 6 

Combinational Logic in Org
If the truth table input is in an Org buffer, we can use its spreadsheet capabilities to compute combinational logic with said values. For example, with the table above we can compute the xor
of each input row by entering the following formula for all cells in column $4
. Refer to Emacs Lisp forms as formulas for more general documentation on how Org formulas work.
1 2 3 4 5 6 7 

Note that the formula must include the N
flag so that referenced cells are parsed as numbers.
To support combinational logic in Org tables, I’ve created a library to support digital boolean operations, where the only legal input values are 0
and 1
. The following boolean logic operations are supported:
 multiinput and (
digitaland
)  multiinput or (
digitalor
)  multiinput nand (
digitalnand
)  multiinput nor (
digitalnor
)  2input xor (
digitalxor
)
Implementation
Implementation of the behavior described above is done in the following two files:
 cctruthtable.el which holds the functions required to implement
cc/inserttruthtableinput
.  ccdigitallogic.el which holds all the functions required to support digital combinational logic with tables created by the above.
All code verified with Emacs 29.1.
Installation
Install the above files into your loadpath
. Then include the following lines in your Emacs initialization file.
1 2 

As this work is relatively new, I’ve yet to try to make this into a MELPA package for public distribution. If enough interest is there, I can be motivated to do this.
On the need to write a digital logic library
At the start I tried to get away with using the base Elisp functions for boolean operations. I quickly found out that such functions were really designed presuming the input values are t
or nil.
Using the digital convention of using 0
and 1
as input values to the base Elisp boolean operations can lead to surprising results. For example, evaluating (not 0)
will return nil
. Ultimately this led to writing a library of boolean functions expressly designed to use as input the digital values of 0
and 1
.
Closing Thoughts
Programmers deal with combinational logic all the time. As much as I love truth tables, they are a PITA to make because so much of the work is mechanical. Automating this work with Elisp and Org has freed me to focus on just solving the logic problem. Engaged readers might find similar results.
Thanks for reading! Feel free to provide feedback about this post to me on Mastodon.