Algorithm for generating all possible boolean functions of n variables
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In the study of computer science, boolean functions are integral to a range of applications from logic circuits to digital signal processing. A boolean function is a mathematical function that takes one or more binary inputs (0 or 1) and produces a binary output. This article delves into the algorithmic generation of all possible boolean functions of n
variables, highlighting key concepts, methodologies, and examples.
Understanding Boolean Functions
Basics
A boolean function of n
variables is represented as , where each can be either 0 or 1. Since each variable has two possible values, there are different combinations of input values. For each combination, the function can output either a 0 or 1. Consequently, the total number of distinct boolean functions of n
variables is . This exponential growth underscores the necessity for efficient algorithms to generate these functions systematically.
Example
Consider n = 2
variables, and . We can construct a truth table with $2^2 = 4$ rows, representing all input combinations:
| 0 | 0 | 0 or 1 (2 options) |
| 0 | 1 | 0 or 1 (2 options) |
| 1 | 0 | 0 or 1 (2 options) |
| 1 | 1 | 0 or 1 (2 options) |
For each row, the function can yield either 0 or 1. Therefore, there are distinct boolean functions for two variables.
Algorithm for Generation
The task of generating all boolean functions for n
variables is inherently combinatorial. Here is a step-by-step algorithm to achieve this:
- Generate Input Combinations: • Construct all combinations of the
nbinary variables. • This can be done using nested loops or recursive functions ifnis relatively small. - Create Output Sets: • For each input combination, generate a set of all possible outputs (0 or 1). This step produces outputs.
- Combine and Enumerate Functions: • Combine each input with all possible output sets to produce a distinct boolean function. • Enumerate these functions to list them comprehensively.
Example Algorithm in Pseudocode
Related reading
- Algorithm For Generating Unique Colors
- Algorithm for generating vibration patterns ranging in intensity in Android?
- Algorithm for Gift Card Codes
- Algorithm for grouping friends at the cinema
- Algorithm for Human Towering
- Algorithm for maximizing coverage of rectangular area with scaling tiles
- Algorithm for inserting points in a piecewise-cubic Bézier path
- Algorithm for intersection of 2 lines?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.