Truth Table Generator
num inputs
num outputs
0 1
How is this implemented?
The process is pretty straightforward. There is a loop that counts from 0 to where n
is the number of inputs. Each number is converted to binary and padded with 0s. For the output, we just tack some more zeros on the end. You should be able to use this concept to implement this in any language. There are only two things you need to figure out in different languages
- how to get the binary representation of a number
- how to add the spaces between all of the characters
const rows = [];
for (let i = 0; i < Math.pow(2, numInputs); i++) {
// convert to binary string and pad to the right length
const binaryString = i.toString(2).padStart(numInputs, "0");
// add output columns
const withOutputs = binaryString.padEnd(numInputs + numOutputs, "0");
// add spaces
rows.push(Array.from(withOutputs).join(" "));
}
const tableData = rows.join("\n");
Last updated 02/20/2023