XOR and XNOR logic gate

Logic Gate

In this tutorial we will learn about XOR and XNOR logic gates.

Logic Gate: XOR

The logic gate XOR (Exclusive OR gate) takes two or more input and works as per the following truth table.

i.e., if there are even number of 1 or TRUE as input then the output is 1 or TRUE else it is 0 or FALSE.

Where, 0 means OFF or FALSE and 1 means ON or TRUE.

XOR in code

We use the caret character ^ for XOR in programming language like C, C++, Java, JavaScript, Php etc.

So, if we are given an if-statement then the code inside the if block will be executed if both the variables (expression) are different i.e, TRUE-FALSE.


//javascript
var A = true;
var B = false;

if(A ^ B) {
    console.log("Hello");
} else {
    console.log("Bye");
}

The given code will print Hello as both A and B are different.
i.e., A ^ B
= true ^ false
= true

Logic Gate: XNOR

The logic gate XNOR (Exclusive NOR gate) is reverse of the XOR gate and takes two or more input and works as per the following truth table.

i.e., if there are even number of 1 or TRUE as input then the output is 1 or TRUE else it is 0 or FALSE.

XNOR in code

We use the caret character ^ and ! for XNOR in programming language like C, C++, Java, JavaScript, Php etc.

So, if we are given an if-statement then the code inside the if block will be executed if both the variables (expression) are same i.e., TRUE-TRUE or FALSE-FALSE.


//javascript
var A = true;
var B = true;

if(!(A ^ B)) {
    console.log("Hello");
} else {
    console.log("Bye");
}

the given code will print Hello as both A and B are same.
i.e., A ^ B
= true ^ true
= false
and !false = true

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT