XNOR operator in Java
The opposite of the binary equivalent of XOR is given by XNOR.
Truth table:
X | Y | XNOR |
0 | 0 | 1 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
If the bits are the same, it returns 1, else it returns 0.
Examples:
Input: 10 20
Output : 1
A Binary of 20 is 10100
A Binary of 10 is 1010
So the XNOR is 00001
So the output is 1
Input : 10 10
Output : 15
A Binary of 10 is 1010
A Binary of 10 is 1010
So the XNOR is 1111
So the output is 15
Method 1:- (O(logn))
In this solution, each bit is checked separately. If two bits match, we output 1; otherwise, we output 0.
Let's explain it using the following code.
XNORExpl1.java
// Java program to calculate
// XNOR of two numbers
import java.util.*;
import java.lang.*;
public class XNORExpl1 {
public static int xnor(int x, int y)
{
// x should be larger
if (x < y) {
// swapping x and y;
int temp = x;
x = y;
y = temp;
}
if (x == 0 && y == 0)
return 1;
// for last bit of x
int x_rem = 0;
// for the last bit of y
int y_rem = 0;
// counter for c bit
// and set bit in xnorn
int c = 0;
// to make new xnor number
int xnorn = 0;
// for set bits in new xnor number
while (true) {
// get last bit of x
x_rem = x & 1;
// get the last bit of y
y_rem = y & 1;
// Checking if the current two bits are the same
if (x_rem == y_rem)
xnorn |= (1 << c);
// counter for c bit
c++;
x = x >> 1;
y = y >> 1;
if (x < 1)
break;
}
return xnorn;
}
// Driver code
public static void main(String args[])
{
int x = 10, y = 50;
System.out.println(xnor(x, y));
}
}
Output:
Second Method:- O(1)
1) Determine the maximum of the two provided numbers.
2) Flip the highest two numbers' bits.
3) Return the XOR of the bigger original number and the smaller changed number.
XNORExpl2.java
// Java program to calculate XNOR
// of two numbers
import java.io.*;
class XNORExpl2 {
static int togglebit(int m)
{
if (m == 0)
return 1;
// copying m as we are
// going to change it.
int j = m;
// Below steps set bits after
// MSB (including MSB)
m |= m >> 1;
// This makes sure 4 bits
// (From MSB and including MSB)
// are set. It does following
// 110011001 | 001100110 = 111111111
m |= m >> 2;
m |= m >> 4;
m |= m >> 8;
m |= m >> 16;
return j ^ m;
}
// Returns XNOR of n1 and n2
static int xnor(int n1, int n2)
{
// if n2 is greater then
// swap this number in n1
if (n1 < n2)
{
int t = n1;
n1 = n2;
n2 = t;
}
n1 = togglebit(n1);
return n1 ^ n2;
}
/* Driver code to test above method */
public static void main(String args[])
{
int x = 10, y = 20;
System.out.println(xnor(x, y));
}
}
Output:
Third Method: Using XOR
Simply said, A XOR B is the opposite of XNOR of A and B. A bit mask must be created in order to only retrieve the real bits from an inverted binary number because directly inverting any binary number also flips the leading zeroes.
Below is an example of this implementation:
XNORExpl3.java
// Java program to find XNOR of two numbers.
class XNORExpl3 {
// Returns XNOR of n1 and n2
static int XNOR(int x, int y)
{
// getting the number of bits from
// max of x and y to construct the bit mask
int nOB = (int)(Math.log(Math.max(x, y)) / Math.log(2));
// constructing the bit m
int m = (1 << nOB) - 1;
// x = inverted xor
int x1 = ~(x ^ y);
// getting only the required bits
// using the m
return x1 & m;
}
// Main method
public static void main(String args[])
{
int n1 = 7, n2 = 19;
// calling the function
System.out.print(XNOR(n1, n2));
}
}
Output:
Time Complexity: O(1)
Auxiliary Space: O(1)