Skip to content

Instantly share code, notes, and snippets.

@bschlenk
Created January 28, 2017 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bschlenk/f704ccccd2da25a33cbf75cd2520ec30 to your computer and use it in GitHub Desktop.
Save bschlenk/f704ccccd2da25a33cbf75cd2520ec30 to your computer and use it in GitHub Desktop.
Generate all possible truth table combinations for the given number of inputs. Wrote for students in logic class.
#!/usr/bin/env python
import sys
if len(sys.argv) != 2:
print "usage: %s <number of inputs>" % sys.argv[0]
sys.exit(1)
inputs = int(sys.argv[1])
rows = 2 ** inputs
for x in range(rows):
print bin(x)[2:].rjust(inputs, '0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment