XNOR of two numbers
Last Updated :
06 Jul, 2022
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++
#include <iostream>
using namespace std;
int xnor( int a, int b)
{
if (a < b)
swap(a, b);
if (a == 0 && b == 0)
return 1;
int a_rem = 0;
int b_rem = 0;
int count = 0;
int xnornum = 0;
while (a)
{
a_rem = a & 1;
b_rem = b & 1;
if (a_rem == b_rem)
xnornum |= (1 << count);
count++;
a = a >> 1;
b = b >> 1;
}
return xnornum;
}
int main()
{
int a = 10, b = 50;
cout << xnor(a, b);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG {
public static int xnor( int a, int b)
{
if (a < b) {
int t = a;
a = b;
b = t;
}
if (a == 0 && b == 0 )
return 1 ;
int a_rem = 0 ;
int b_rem = 0 ;
int count = 0 ;
int xnornum = 0 ;
while ( true ) {
a_rem = a & 1 ;
b_rem = b & 1 ;
if (a_rem == b_rem)
xnornum |= ( 1 << count);
count++;
a = a >> 1 ;
b = b >> 1 ;
if (a < 1 )
break ;
}
return xnornum;
}
public static void main(String argc[])
{
int a = 10 , b = 50 ;
System.out.println(xnor(a, b));
}
}
|
Python3
import math
def swap(a,b):
temp = a
a = b
b = temp
def xnor(a, b):
if (a < b):
swap(a, b)
if (a = = 0 and b = = 0 ) :
return 1 ;
a_rem = 0
b_rem = 0
count = 0
xnornum = 0
while (a! = 0 ) :
a_rem = a & 1
b_rem = b & 1
if (a_rem = = b_rem):
xnornum | = ( 1 << count)
count = count + 1
a = a >> 1
b = b >> 1
return xnornum;
a = 10
b = 50
print (xnor(a, b))
|
C#
using System;
public class GfG {
public static int xnor( int a, int b)
{
if (a < b) {
int t = a;
a = b;
b = t;
}
if (a == 0 && b == 0)
return 1;
int a_rem = 0;
int b_rem = 0;
int count = 0;
int xnornum = 0;
while ( true ) {
a_rem = a & 1;
b_rem = b & 1;
if (a_rem == b_rem)
xnornum |= (1 << count);
count++;
a = a >> 1;
b = b >> 1;
if (a < 1)
break ;
}
return xnornum;
}
public static void Main()
{
int a = 10, b = 50;
Console.WriteLine(xnor(a, b));
}
}
|
PHP
<?php
function xnor( $a , $b )
{
if ( $a < $b )
list( $a , $b ) = array ( $b , $a );
if ( $a == 0 && $b == 0)
return 1;
$a_rem = 0;
$b_rem = 0;
$count = 0;
$xnornum = 0;
while ( $a )
{
$a_rem = $a & 1;
$b_rem = $b & 1;
if ( $a_rem == $b_rem )
$xnornum |= (1 << $count );
$count ++;
$a = $a >> 1;
$b = $b >> 1;
}
return $xnornum ;
}
$a = 10;
$b = 50;
echo xnor( $a , $b );
?>
|
Javascript
<script>
function xnor(a, b)
{
if (a < b)
{
let t = a;
a = b;
b = t;
}
if (a == 0 && b == 0)
return 1;
let a_rem = 0;
let b_rem = 0;
let count = 0;
let xnornum = 0;
while ( true ) {
a_rem = a & 1;
b_rem = b & 1;
if (a_rem == b_rem)
xnornum |= (1 << count);
count++;
a = a >> 1;
b = b >> 1;
if (a < 1)
break ;
}
return xnornum;
}
let a = 10, b = 50;
document.write(xnor(a, b));
</script>
|
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++
#include <iostream>
using namespace std;
int togglebit( int n)
{
if (n == 0)
return 1;
int i = n;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return i ^ n;
}
int XNOR( int num1, int num2)
{
if (num1 < num2)
swap(num1, num2);
num1 = togglebit(num1);
return num1 ^ num2;
}
int main()
{
int num1 = 10, num2 = 20;
cout << XNOR(num1, num2);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int togglebit( int n)
{
if (n == 0 )
return 1 ;
int i = n;
n |= n >> 1 ;
n |= n >> 2 ;
n |= n >> 4 ;
n |= n >> 8 ;
n |= n >> 16 ;
return i ^ n;
}
static int xnor( int num1, int num2)
{
if (num1 < num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
num1 = togglebit(num1);
return num1 ^ num2;
}
public static void main(String[] args)
{
int a = 10 , b = 20 ;
System.out.println(xnor(a, b));
}
}
|
Python
import math
def togglebit( n):
if (n = = 0 ):
return 1
i = n
n = n|(n >> 1 )
n | = n >> 2
n | = n >> 4
n | = n >> 8
n | = n >> 16
return i ^ n
def xnor( num1, num2):
if (num1 < num2):
temp = num1
num1 = num2
num2 = temp
num1 = togglebit(num1)
return num1 ^ num2
a = 10
b = 20
print (xnor(a, b))
|
C#
using System;
class GFG {
static int togglebit( int n)
{
if (n == 0)
return 1;
int i = n;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return i ^ n;
}
static int xnor( int num1, int num2)
{
if (num1 < num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
num1 = togglebit(num1);
return num1 ^ num2;
}
public static void Main()
{
int a = 10, b = 20;
Console.WriteLine(xnor(a, b));
}
}
|
PHP
<?php
function togglebit( $n )
{
if ( $n == 0)
return 1;
$i = $n ;
$n |= $n >> 1;
$n |= $n >> 2;
$n |= $n >> 4;
$n |= $n >> 8;
$n |= $n >> 16;
return $i ^ $n ;
}
function XNOR( $num1 , $num2 )
{
if ( $num1 < $num2 )
list( $num1 , $num2 )= array ( $num2 , $num1 );
$num1 = togglebit( $num1 );
return $num1 ^ $num2 ;
}
$num1 = 10;
$num2 = 20;
echo XNOR( $num1 , $num2 );
?>
|
Javascript
<script>
function togglebit(n)
{
if (n == 0)
return 1;
let i = n;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return i ^ n;
}
function XNOR(num1, num2)
{
if (num1 < num2) {
let temp = num1;
num1 = num2;
num2 = temp;
}
num1 = togglebit(num1);
return num1 ^ num2;
}
let num1 = 10, num2 = 20;
document.write(XNOR(num1, num2));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
unsigned int XNOR( int a, int b)
{
int numOfBits = log2(max(a, b));
unsigned mask = (1 << numOfBits) - 1;
unsigned xnor = ~(a ^ b);
return xnor & mask;
}
int main()
{
int num1 = 7, num2 = 19;
cout << XNOR(num1, num2);
return 0;
}
|
Java
class GFG {
static int XNOR( int a, int b)
{
int numOfBits = ( int )(Math.log(Math.max(a, b)) / Math.log( 2 ));
int mask = ( 1 << numOfBits) - 1 ;
int xnor = ~(a ^ b);
return xnor & mask;
}
public static void main(String[] args)
{
int num1 = 7 , num2 = 19 ;
System.out.print(XNOR(num1, num2));
}
}
|
Python3
import math
def XNOR(a, b):
numOfBits = int (math.log( max (a, b), 2 ))
mask = ( 1 << numOfBits) - 1
xnor = ~(a ^ b)
return xnor & mask
num1 = 7
num2 = 19
print (XNOR(num1, num2))
|
C#
using System;
class GFG {
static int XNOR( int a, int b)
{
int numOfBits = ( int )(Math.Log(Math.Max(a, b)) / Math.Log(2));
int mask = (1 << numOfBits) - 1;
int xnor = ~(a ^ b);
return xnor & mask;
}
public static void Main( string [] args)
{
int num1 = 7, num2 = 19;
Console.Write(XNOR(num1, num2));
}
}
|
Javascript
function XNOR(a, b)
{
let numOfBits = Math.floor(Math.log2(Math.max(a, b)));
let mask = (1 << numOfBits) - 1;
let xnor = ~(a ^ b);
return xnor & mask;
}
let num1 = 7, num2 = 19;
console.log(XNOR(num1, num2));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...