notes from /dev/null

by Charles Choi 최민수

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 = 2number 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 M-x cc/insert-truth-table-input. In the mini-buffer 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 21 and 20 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.


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.

| i | 1 | 0 | output |
| 0 | 0 | 0 |      0 |
| 1 | 0 | 1 |      1 |
| 2 | 1 | 0 |      1 |
| 3 | 1 | 1 |      0 |
#+TBLFM: $4='(digital-xor $2 $3);N

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:

  • multi-input and (digital-and)
  • multi-input or (digital-or)
  • multi-input nand (digital-nand)
  • multi-input nor (digital-nor)
  • 2-input xor (digital-xor)


Implementation of the behavior described above is done in the following two files:

  • cc-truth-table.el which holds the functions required to implement cc/insert-truth-table-input.
  • cc-digital-logic.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.


Install the above files into your load-path. Then include the following lines in your Emacs initialization file.

(require 'cc-truth-table)
(require 'cc-digital-logic)

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.

emacs   org mode



Feeds & TagsGet Captee for macOS

Powered by Pelican