 GeeksforGeeks App
Open App Browser
Continue

XNOR gives the reverse of XOR if binary bit.

```First bit | Second bit | XNOR
0             0           1
0             1           0
1             0           0
1             1           1
It gives 1 if bits are same else if
bits are different it gives 0.```

It is reverse of XOR but we can’t implement it directly so this is the program for implement XNOR
Examples:

```Input  : 10 20
Output : 1
Binary of 20 is 10100
Binary of 10 is  1010
So the XNOR is  00001
So output is 1

Input  : 10 10
Output : 15
Binary of 10 is  1010
Binary of 10 is  1010
So the XNOR is   1111
So output is 15```

First Method:- (O(logn)) In this solution we check one bit at a time. If two bits are same, we put 1 in result, else we put 0.
Let’s understand it with below code

## C++

 `// CPP program to find``// XNOR of two numbers``#include ``using` `namespace` `std;` `// log(n) solution``int` `xnor(``int` `a, ``int` `b)``{``    ``// Make sure a is larger``    ``if` `(a < b)``        ``swap(a, b);` `    ``if` `(a == 0 && b == 0)``        ``return` `1;` `    ``int` `a_rem = 0; ``// for last bit of a``    ``int` `b_rem = 0; ``// for last bit of b` `    ``// counter for count bit``    ``// and set bit  in xnornum``    ``int` `count = 0;` `    ``// to make new xnor number``    ``int` `xnornum = 0;` `    ``// for set bits in new xnor number``    ``while` `(a)``    ``{``        ``// get last bit of a``        ``a_rem = a & 1;` `        ``// get last bit of b``        ``b_rem = b & 1;` `        ``// Check if current two``        ``// bits are same``        ``if` `(a_rem == b_rem)       ``            ``xnornum |= (1 << count);``        ` `        ``// counter for count bit``        ``count++;``        ``a = a >> 1;``        ``b = b >> 1;``    ``}``    ``return` `xnornum;``}` `// Driver code``int` `main()``{``    ``int` `a = 10, b = 50;``    ``cout << xnor(a, b);``    ``return` `0;``}`

## Java

 `// Java program to find``// XNOR of two numbers``import` `java.util.*;``import` `java.lang.*;` `public` `class` `GfG {` `    ``public` `static` `int` `xnor(``int` `a, ``int` `b)``    ``{``        ``// Make sure a is larger``        ``if` `(a < b) {``            ``// swapping a and b;``            ``int` `t = a;``            ``a = b;``            ``b = t;``        ``}` `        ``if` `(a == ``0` `&& b == ``0``)``            ``return` `1``;` `        ``// for last bit of a``        ``int` `a_rem = ``0``;` `        ``// for last bit of b``        ``int` `b_rem = ``0``;` `        ``// counter for count bit``        ``// and set bit in xnornum``        ``int` `count = ``0``;` `        ``// to make new xnor number``        ``int` `xnornum = ``0``;` `        ``// for set bits in new xnor number``        ``while` `(``true``) {``            ``// get last bit of a``            ``a_rem = a & ``1``;` `            ``// get last bit of b``            ``b_rem = b & ``1``;` `            ``// Check if current two bits are same``            ``if` `(a_rem == b_rem)``                ``xnornum |= (``1` `<< count);` `            ``// counter for count bit``            ``count++;``            ``a = a >> ``1``;``            ``b = b >> ``1``;``            ``if` `(a < ``1``)``                ``break``;``        ``}``        ``return` `xnornum;``    ``}` `    ``// Driver function``    ``public` `static` `void` `main(String argc[])``    ``{``        ``int` `a = ``10``, b = ``50``;``        ``System.out.println(xnor(a, b));``    ``}``}`

## Python3

 `# Python3 program to find XNOR``# of two numbers``import` `math` `def` `swap(a,b):` `    ``temp``=``a``    ``a``=``b``    ``b``=``temp` `# log(n) solution``def` `xnor(a, b):``    ` `    ``# Make sure a is larger``    ``if` `(a < b):``        ``swap(a, b)` `    ``if` `(a ``=``=` `0` `and` `b ``=``=` `0``) :``        ``return` `1``;``    ` `    ``# for last bit of a``    ``a_rem ``=` `0``    ` `    ``# for last bit of b``    ``b_rem ``=` `0` `    ``# counter for count bit and``    ``#  set bit in xnor num``    ``count ``=` `0``    ` `    ``# for make new xnor number``    ``xnornum ``=` `0` `    ``# for set bits in new xnor``    ``# number``    ``while` `(a!``=``0``) :``    ` `        ``# get last bit of a``        ``a_rem ``=` `a & ``1``        ` `        ``# get last bit of b``        ``b_rem ``=` `b & ``1` `        ``# Check if current two``        ``# bits are same``        ``if` `(a_rem ``=``=` `b_rem):    ``            ``xnornum |``=` `(``1` `<< count)``        ` `        ``# counter for count bit``        ``count``=``count``+``1``        ` `        ``a ``=` `a >> ``1``        ``b ``=` `b >> ``1``    ` `    ``return` `xnornum;``    ` `# Driver method``a ``=` `10``b ``=` `50``print``(xnor(a, b))` `# This code is contributed by Gitanjali`

## C#

 `// C# program to find``// XNOR of two numbers``using` `System;` `public` `class` `GfG {` `    ``public` `static` `int` `xnor(``int` `a, ``int` `b)``    ``{``        ``// Make sure a is larger``        ``if` `(a < b) {``            ``// swapping a and b;``            ``int` `t = a;``            ``a = b;``            ``b = t;``        ``}` `        ``if` `(a == 0 && b == 0)``            ``return` `1;` `        ``// for last bit of a``        ``int` `a_rem = 0;` `        ``// for last bit of b``        ``int` `b_rem = 0;` `        ``// counter for count bit``        ``// and set bit in xnornum``        ``int` `count = 0;` `        ``// to make new xnor number``        ``int` `xnornum = 0;` `        ``// for set bits in new xnor number``        ``while` `(``true``) {``            ``// get last bit of a``            ``a_rem = a & 1;` `            ``// get last bit of b``            ``b_rem = b & 1;` `            ``// Check if current two bits are same``            ``if` `(a_rem == b_rem)``                ``xnornum |= (1 << count);` `            ``// counter for count bit``            ``count++;``            ``a = a >> 1;``            ``b = b >> 1;``            ``if` `(a < 1)``                ``break``;``        ``}``        ``return` `xnornum;``    ``}` `    ``// Driver function``    ``public` `static` `void` `Main()``    ``{``        ``int` `a = 10, b = 50;``        ``Console.WriteLine(xnor(a, b));``    ``}``}` `// This code is contributed by vt_m`

## PHP

 `> 1;``        ``\$b` `= ``\$b` `>> 1;``    ``}``    ``return` `\$xnornum``;``}` `    ``// Driver code``    ``\$a` `= 10;``    ``\$b` `= 50;``    ``echo` `xnor(``\$a``, ``\$b``);` `// This code is contributed by mits.    ``?>`

## Javascript

 ``

Output

`7`

Second Method:- O(1)
1) Find maximum of two given numbers.
2) Toggle all bits in higher of two numbers.
3) Return XOR of original smaller number and modified larger number.

## C++

 `// CPP program to find XNOR of two numbers.``#include ``using` `namespace` `std;` `// Please refer below post for details of this``// function``// https://www.geeksforgeeks.org/toggle-bits-significant-bit/``int` `togglebit(``int` `n)``{``    ``if` `(n == 0)``        ``return` `1;` `    ``// Make a copy of n as we are``    ``// going to change it.``    ``int` `i = n;` `    ``// Below steps set bits after``    ``// MSB (including MSB)` `    ``// Suppose n is 273 (binary``    ``// is 100010001). It does following``    ``// 100010001 | 010001000 = 110011001``    ``n |= n >> 1;` `    ``// This makes sure 4 bits``    ``// (From MSB and including MSB)``    ``// are set. It does following``    ``// 110011001 | 001100110 = 111111111``    ``n |= n >> 2;``    ``n |= n >> 4;``    ``n |= n >> 8;``    ``n |= n >> 16;` `    ``return` `i ^ n;``}` `// Returns XNOR of num1 and num2``int` `XNOR(``int` `num1, ``int` `num2)``{``    ``// if num2 is greater then``    ``// we swap this number in num1``    ``if` `(num1 < num2)``        ``swap(num1, num2);``    ``num1 = togglebit(num1);``  ` `    ``return` `num1 ^ num2;``}` `// Driver code``int` `main()``{``    ``int` `num1 = 10, num2 = 20;``    ``cout << XNOR(num1, num2);``    ``return` `0;``}`

## Java

 `// Java program to find XNOR``// of two numbers``import` `java.io.*;` `class` `GFG {``    ` `    ``// Please refer below post for``    ``// details of this function``    ``// https://www.geeksforgeeks.org/toggle-bits-significant-bit/``    ``static` `int` `togglebit(``int` `n)``    ``{``        ``if` `(n == ``0``)``            ``return` `1``;` `        ``// Make a copy of n as we are``        ``// going to change it.``        ``int` `i = n;` `        ``// Below steps set bits after``        ``// MSB (including MSB)` `        ``// Suppose n is 273 (binary``        ``// is 100010001). It does following``        ``// 100010001 | 010001000 = 110011001``        ``n |= n >> ``1``;` `        ``// This makes sure 4 bits``        ``// (From MSB and including MSB)``        ``// are set. It does following``        ``// 110011001 | 001100110 = 111111111``        ``n |= n >> ``2``;``        ``n |= n >> ``4``;``        ``n |= n >> ``8``;``        ``n |= n >> ``16``;` `        ``return` `i ^ n;``    ``}` `    ``// Returns XNOR of num1 and num2``    ``static` `int` `xnor(``int` `num1, ``int` `num2)``    ``{``        ``// if num2 is greater then``        ``// we swap this number in num1 ``        ``if` `(num1 < num2)``        ``{``            ``int` `temp = num1;``            ``num1 = num2;``            ``num2 = temp;``        ``}``    ` `        ``num1 = togglebit(num1);``    ` `        ``return` `num1 ^ num2;``    ``}` `    ``/* Driver program to test above function */``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `a = ``10``, b = ``20``;    ``        ``System.out.println(xnor(a, b));``    ``}``}` `// This code is contributed by Gitanjali`

## Python

 `# python program to find XNOR of two numbers``import` `math` `# Please refer below post for details of this function``# https://www.geeksforgeeks.org/toggle-bits-significant-bit/``def` `togglebit( n):` `    ``if` `(n ``=``=` `0``):``        ``return` `1` `    ``# Make a copy of n as we are``    ``# going to change it.``    ``i ``=` `n` `    ``# Below steps set bits after``    ``# MSB (including MSB)` `    ``# Suppose n is 273 (binary``    ``# is 100010001). It does following``    ``# 100010001 | 010001000 = 110011001``    ``n ``=` `n|(n >> ``1``)` `    ``# This makes sure 4 bits``    ``# (From MSB and including MSB)``    ``# are set. It does following``    ``# 110011001 | 001100110 = 111111111``    ``n |``=` `n >> ``2``    ``n |``=` `n >> ``4``    ``n |``=` `n >> ``8``    ``n |``=` `n >> ``16` `    ``return` `i ^ n``    ` `# Returns XNOR of num1 and num2``def` `xnor( num1, num2):``    ` `    ``# Make sure num1 is larger``    ``if` `(num1 < num2):``        ``temp ``=` `num1``        ``num1 ``=` `num2``        ``num2 ``=` `temp``    ``num1 ``=` `togglebit(num1)``    ` `    ``return` `num1 ^ num2``    ` `# Driver code``a ``=` `10``b ``=` `20``print` `(xnor(a, b))` `# This code is contributed by 'Gitanjali'.`

## C#

 `// C# program to find XNOR``// of two numbers``using` `System;` `class` `GFG {``    ` `    ``// Please refer below post for``    ``// details of this function``    ``// https://www.geeksforgeeks.org/toggle-bits-significant-bit/``    ``static` `int` `togglebit(``int` `n)``    ``{``        ``if` `(n == 0)``            ``return` `1;` `        ``// Make a copy of n as we are``        ``// going to change it.``        ``int` `i = n;` `        ``// Below steps set bits after``        ``// MSB (including MSB)` `        ``// Suppose n is 273 (binary``        ``// is 100010001). It does following``        ``// 100010001 | 010001000 = 110011001``        ``n |= n >> 1;` `        ``// This makes sure 4 bits``        ``// (From MSB and including MSB)``        ``// are set. It does following``        ``// 110011001 | 001100110 = 111111111``        ``n |= n >> 2;``        ``n |= n >> 4;``        ``n |= n >> 8;``        ``n |= n >> 16;` `        ``return` `i ^ n;``    ``}` `    ``// Returns XNOR of num1 and num2``    ``static` `int` `xnor(``int` `num1, ``int` `num2)``    ``{``        ``// if num2 is greater then``        ``// we swap this number in num1``        ``if` `(num1 < num2)``        ``{``            ``int` `temp = num1;``            ``num1 = num2;``            ``num2 = temp;``        ``}``    ` `        ``num1 = togglebit(num1);``    ` `        ``return` `num1 ^ num2;``    ``}` `    ``// Driver program``    ``public` `static` `void` `Main()``    ``{``        ``int` `a = 10, b = 20;``        ``Console.WriteLine(xnor(a, b));``    ``}``}` `// This code is contributed by vt_m`

## PHP

 `> 1;` `    ``// This makes sure 4 bits``    ``// (From MSB and including MSB)``    ``// are set. It does following``    ``// 110011001 | 001100110 = 111111111``    ``\$n` `|= ``\$n` `>> 2;``    ``\$n` `|= ``\$n` `>> 4;``    ``\$n` `|= ``\$n` `>> 8;``    ``\$n` `|= ``\$n` `>> 16;` `    ``return` `\$i` `^ ``\$n``;``}` `// Returns XNOR of num1 and num2``function` `XNOR(``\$num1``, ``\$num2``)``{``    ` `    ``// if num2 is greater then``    ``// we swap this number in num1``    ``if` `(``\$num1` `< ``\$num2``)``        ``list(``\$num1``, ``\$num2``)=``array``(``\$num2``, ``\$num1``);``    ``\$num1` `= togglebit(``\$num1``);` `    ``return` `\$num1` `^ ``\$num2``;``}` `// Driver code``\$num1` `= 10;``\$num2` `= 20;``echo` `XNOR(``\$num1``, ``\$num2``);` `// This code is contributed by Smitha.``?>`

## Javascript

 ``

Output

`1`

Third Method: Using XOR

XNOR of A and B is simply the inverse of A XOR B. The problem with directly inverting a binary number is that it causes leading zeroes to be inverted as well, therefore a bit mask needs to be constructed to only extract the actual bits from the inverted number.

This implementation is shown below:

## C++

 `// CPP program to find XNOR of two numbers.``#include ``using` `namespace` `std;` `// Returns XNOR of num1 and num2``unsigned ``int` `XNOR(``int` `a, ``int` `b)``{``    ``//getting the number of bits from``    ``//max of a and b to construct the bit mask``    ``int` `numOfBits = log2(max(a, b));``    ``//constructing the bit mask``    ``unsigned mask = (1 << numOfBits) - 1;``    ``//xnor = inverted xor``    ``unsigned xnor = ~(a ^ b);``    ``//getting only the required bits``    ``//using the mask``    ``return` `xnor & mask;``}`` ` `// Driver code``int` `main()``{``    ``int` `num1 = 7, num2 = 19;``      ``//function call``    ``cout << XNOR(num1, num2);``    ``return` `0;``}` `//this code is contributed by phasing17`

## Java

 `// Java program to find XNOR of two numbers.``class` `GFG {` `  ``// Returns XNOR of num1 and num2``  ``static` `int` `XNOR(``int` `a, ``int` `b)``  ``{``    ``// getting the number of bits from``    ``// max of a and b to construct the bit mask``    ``int` `numOfBits = (``int``)(Math.log(Math.max(a, b)) / Math.log(``2``));` `    ``// constructing the bit mask``    ``int` `mask = (``1` `<< numOfBits) - ``1``;` `    ``// xnor = inverted xor``    ``int` `xnor = ~(a ^ b);` `    ``// getting only the required bits``    ``// using the mask``    ``return` `xnor & mask;``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int` `num1 = ``7``, num2 = ``19``;` `    ``// function call``    ``System.out.print(XNOR(num1, num2));``  ``}``}` `// this code is contributed by phasing17`

## Python3

 `# Python3 program to find XNOR of two numbers.``import` `math` `# Returns XNOR of num1 and num2``def` `XNOR(a, b):` `    ``# getting the number of bits from``    ``# max of a and b to construct the bit mask``    ``numOfBits ``=` `int``(math.log(``max``(a, b), ``2``))` `    ``# constructing the bit mask``    ``mask ``=` `(``1` `<< numOfBits) ``-` `1` `    ``# xnor = inverted xor``    ``xnor ``=` `~(a ^ b)` `    ``# getting only the required bits``    ``# using the mask``    ``return` `xnor & mask`  `# Driver code``num1 ``=` `7``num2 ``=` `19` `# function call``print``(XNOR(num1, num2))` `# This code is contributed by phasing17`

## C#

 `// C# program to find XNOR of two numbers.` `using` `System;` `class` `GFG {` `  ``// Returns XNOR of num1 and num2``  ``static` `int` `XNOR(``int` `a, ``int` `b)``  ``{``    ``// getting the number of bits from``    ``// max of a and b to construct the bit mask``    ``int` `numOfBits = (``int``)(Math.Log(Math.Max(a, b)) / Math.Log(2));` `    ``// constructing the bit mask``    ``int` `mask = (1 << numOfBits) - 1;` `    ``// xnor = inverted xor``    ``int` `xnor = ~(a ^ b);` `    ``// getting only the required bits``    ``// using the mask``    ``return` `xnor & mask;``  ``}`  `  ``// Driver code``  ``public` `static` `void` `Main(``string``[] args)``  ``{``    ``int` `num1 = 7, num2 = 19;` `    ``// Function call``    ``Console.Write(XNOR(num1, num2));``  ``}``}` `// this code is contributed by phasing17`

## Javascript

 `// JavaScript program to find XNOR of two numbers.`  `// Returns XNOR of num1 and num2``function` `XNOR(a, b)``{``    ``//getting the number of bits from``    ``//max of a and b to construct the bit mask``    ``let numOfBits = Math.floor(Math.log2(Math.max(a, b)));``    ``//constructing the bit mask``    ``let mask = (1 << numOfBits) - 1;``    ``//xnor = inverted xor``    ``let xnor = ~(a ^ b);``    ``//getting only the required bits``    ``//using the mask``    ``return` `xnor & mask;``}`` ` `// Driver code``let num1 = 7, num2 = 19;` `//function call``console.log(XNOR(num1, num2));`  `//this code is contributed by phasing17`

Output

`11`

Time Complexity: O(1)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up