Minimum number of basic logic gates required to realize given Boolean expression`


Logic gates are the basic building block of a digital circuit. They take in one or two binary inputs and return a binary output. Since, using the term binary, the output and input can either be 0 or 1 or it can be said as “false” and “true” or “low” and “high”.

There are 3 basic logic gates −

AND Gate

AND gate has two or more inputs and one output. It produces a high output if all inputs are high. The truth table for a two-input AND gate is given below −

Input 1

Input 2

Output

1

1

1

1

0

0

0

1

0

0

0

0

OR Gate

OR gate has two or more inputs and one output. It produces a high output if any one input is high. The truth table for a two-input OR gate is given below −

Input 1

Input 2

Output

1

1

1

1

0

1

0

1

1

0

0

0

Not Gate

NOT gate has one input and one output. It produces a high output if the input is low and low output if the input is high. The truth table for NOT gate is given below −

Input

Output

1

0

0

1

These three basic logic gates can be combined to create more complex circuits, such as the NAND gate, the NOR gate, and the XOR gate.

Boolean Expression − It is a mathematical expression that contains one or more variables, logic operators and constants. The value for variables can be either 0 or 1 and are represented by a, b, c etc. The logical operators used in Boolean expressions are typically AND, OR, and NOT. These operators combine variables and constants in such a way that it defines a relationship between the variables or constants.

For example, the boolean expression A AND B would be true only if both variables A and B are true. The expression A OR B would be true if either variable A or variable B, or both, are true. The expression NOT A would be true only if variable A is false.

Boolean expressions are often used in programming, circuit design, and digital electronics to represent logical operations, conditions and logical relationships between the variables used.

Problem Statement

Given a string str defining the boolean expression. Find the minimum number of basic logic gates used to realize the given expression.

Input −

str = “!A.B+C”

Output −

3

Explanation

The expression uses 1 NOT gate represented by ‘!’, 1 AND gate represented by ‘.’ and 1 OR gate represented by ‘+’. Thus, a total of 3 basic logic gates.

Example 2

Input −

str = “!(a+b)+c.d”

Output −

4

Explanation

The expression uses 1 NOT gate represented by ‘!’, 2 AND gates represented by ‘.’ and 1 OR gate represented by ‘+’. Thus, a total of 4 basic logic gates.

Solution Approach

In order to realize the total number of logic gates for a boolean expression, we count the total number of gates occurring in the string and recognize them from their symbolic representation.

  • AND gate is represented as ‘.’

  • OR gate is represented as ‘+’

  • AND gate is represented as ‘!’

Pseudocode

procedure numberOfGates (s)
n = length(s)
   count = 0
   for i = 1 to n
      if s[i] equals '.' or s[i] equals '+' or s[i] equals '!'
         count = count + 1
      end if
   end for
   output count
end procedure

Example: C++ Implementation,

In the following program, we traverse the boolean expression and count the number of gates based on their occurrence.

#include <bits/stdc++.h>
using namespace std;
// Function to find the number of basic logic gates used in a boolean expression
void numberOfGates(string s){

   // Calculating the size of the string
   int n = s.size();
   
   // Initialising count variable
   int count = 0;
   
   // Traversing the string to find the gate's symbols
   for (int i = 0; i < n; i++) {
   
      // Incrementing counter on the occurrence of AND, OR or NOT gate
      if (s[i] == '.' || s[i] == '+' || s[i] == '!') {
         count++;
      }
   }
   cout << count;
}
int main(){
   string str = "!a+b";
   cout << "Boolean Exoression: " << str << endl;
   cout << "Minimum number of basic logic gate required: " ;
   numberOfGates(str);
}

Output

Boolean Exoression: !a+b
Minimum number of basic logic gate required: 2

Conclusion

In conclusion, the solution to find the minimum number of basic logic gates required to realize a given boolean expression we can effectively use numberOfGates() function. This function takes a string defining the boolean expression as input and outputs the minimum number of gates required to realize the expression. Minimising logic gates is important in order to reduce the cost and complexity of the circuit.

Overall, the code is a simple and effective way to calculate the number of basic logic gates needed to realize a Boolean expression, and it can be easily adapted for use in more complex digital circuits.

Updated on: 25-Jul-2023

304 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements