# Logic Gates | SIMULINK Tutorial

#### Introduction

In electronics, a logic gate is an idealized or physical device implementing a Boolean function; that is, it performs a logical operation on one or more binary inputs and produces a single binary output. Depending on the context, the term may refer to an ideal logic gate, one that has for instance zero rise time and unlimited fan-out, or it may refer to a non-ideal physical device.

A logic gate is an elementary building block of a digital circuit. Most logic gates have two inputs and one output. At any given moment, every terminal is in one of the two binary conditions low (0) or high (1), represented by different voltage levels. The logic state of a terminal can, and generally does, change often, as the circuit processes data. In most logic gates, the low state is approximately zero volts (0 V), while the high state is approximately five volts positive (+5 V).

Logic gates are primarily implemented using diodes or transistors acting as electronic switches, but can also be constructed using vacuum tubes, electromagnetic relays (relay logic), fluidic logic, pneumatic logic, optics, molecules, or even mechanical elements.

There are seven basic logic gates: AND, OR, XOR, NOT, NAND, NOR, and XNOR.

AND Gate: The AND gate is so named because, if 0 is called “false” and

1 is called “true,” the gate acts in the same way as the logical “and” operator. The following illustration and table show the circuit symbol and logic combinations for an AND gate. (In the symbol, the input terminals are at left and the output terminal is at right.) The output is

“True” when both inputs are “true.” Otherwise, the output is “false.”

OR Gate: The OR gate gets its name from the fact that it behaves after the fashion of the logical inclusive “or.” The output is “true” if either or both of the inputs are “true.” If both inputs are “false,” then the output is “false.

XOR Gate: The XOR (exclusive-OR) gate acts in the same way as the logical “either/or.” The output is “true” if either, but not both, of the inputs are “true.” The output is “false” if both inputs are “false” or if both inputs are “true.” Another way of looking at this circuit is to observe that the output is 1 if the inputs are different, but 0 if the inputs are the same.

NOT Gate: A logical inverter, sometimes called a NOT gate to differentiate it from other types of electronic inverter devices, has only one input. It reverses the logic state.

NAND Gate: The NAND gate operates as an AND gate followed by a NOT gate. It acts in the manner of the logical operation “and” followed by negation. The output is “false” if both inputs are “true.” Otherwise, the output is “true.”

NOR Gate: The NOR gate is a combination OR gate followed by an inverter. Its output is “true” if both inputs are “false.” Otherwise, the output is “false.”

X-NOR Gate: The XNOR (exclusive-NOR) gate is a combination XOR gate followed by an inverter. Its output is “true” if the inputs are the same, and “false” if the inputs are different.

Using combinations of logic gates, complex operations can be performed. In theory, there is no limit to the number of gates that can be arrayed together in a single device.

To implement circuits consisting logic gates in Simulink, Logical Operator block is used. Logical Operator block can be located at:

Simulink>> Logic and Bit operations >> Logical operator

The Logical Operator block performs the specified logical operation on its inputs. An input value is TRUE (1) if it is nonzero and FALSE (0) if it is zero.

You select the Boolean operation connecting the inputs with the Operator parameter list. If you select rectangular as the Icon shape property, the block updates to display the name of the selected operator.

The Logical Operator block performs any of these logical operations on its inputs: AND, OR, NAND, NOR, XOR, and NOT. The output depends on the number of inputs, their dimensionality, and the selected operator. The output is 1 if TRUE and 0 if FALSE. The block icon shows the selected operator. The following rules apply to the inputs and outputs of the block:

• If the block has more than one input, any nonscalar inputs must have the same dimensions. For example, if any input is a 2-by-2 array, all other nonscalar inputs must also be 2-by-2 arrays.
• Scalar inputs are expanded to have the same dimensions as the nonscalar inputs.
• If the block has more than one input, the output has the same dimensions as the inputs (after scalar expansion) and each output element is the result of applying the specified logical operation to the corresponding input elements. For example, if the specified operation is AND and the inputs are 2-by-2 arrays, the output is a 2-by-2 array whose top, left element is the result of applying AND to the top, left elements of the inputs, etc.
• If the block has a single input and the specified operator is not the NOT operator, the input must be vector-like, i.e. a scalar, a 1-D array, or a one-row or one-column 2-D array. The output is a scalar value equal to the result of applying the operation to the elements of the input.
• If the specified operation is NOT, the block accepts only one input. The output has the same dimensions as the input and contains the logical complements of the elements of the input.

Parameters:  A logical operator block has the following parameters:

Operator (Type of GATE to be implemented.)

Number of Input ports

Icon shape (rectangular or distinctive)

Data Type:

Require all inputs and the output to have the same data type. (Unchecked if data types don’t have to match)

Output data type (Specifies output data type, Boolean by default)

Data type Assistant (Clicking the Show data type assistant button enables this parameter):

Mode (Select the category of data to specify: Inherit, Built in, fixed point or expression)

According to Mode selected, different settings show up to be filled according to system requirement.

Example:

The 1 bit comparator is just XOR gate implementation. When the two input bits are same then the output is logic 0 and when they are different then the output is logic 1. The two binary sequence are a = 0101 and b = 0011 that are compared with XOR gate in Simulink. Shown below is the simulink logic gate simulation example:

The digital input bits sequence are implemented in Simulink with the Signal from Workspace block which is available in the DSP Source library.

Once the Signal from Workspace is placed, double click it to open its properties and enter [0 1 0 1] in the signal field.

Similarly place another such block for signal b or copy and paste the earlier block and enter [0 0 1 1] in the signal field.

The XOR logical gate is under Simulink > Logic and Bits operation library as shown:

Once the gate is placed double click on the block and change from AND to XOR in the operator field.

Lastly place a scope from the sink library and connect the output of the XOR gate to the scope.

Change the simulation setting as follows, enter 5 as the stop time and in the solver options select Fixed-Step as the type and discrete(no continuous states) in the Solver field.

Run the simulation to verify whether the model implements the XOR gate. The output picture is shown below.

The above figure shows that from 0 to 1 the output is 0, from 1 to 2 the output is 1, from 2 to 3 the output is also 1 and from 3 to 4 the output is 0 as required by XOR output logic.

##### Anushi Maheshwari

Anushi is pursuing graduation degree in Electronics & Communication from HBTI, Kanpur (Now HBTU). She has a keen interest towards Competitive Programming & loves to solve coding Challenges.

### Recommended Posts

##### Test Post

17 Feb 2018 - Tutorial

##### Counters using conditionally executed subsystems | Simulink Tutorial

16 Feb 2018 - Simulink, Tutorial

##### Regulating drum boiler pressure

16 Feb 2018 - Simulink, Tutorial