×

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR.

Truth table:

XYXNOR
001
010
100
111

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:

XNOR operator in Java

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:

XNOR operator in Java

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:

XNOR operator in Java

Time Complexity: O(1)

Auxiliary Space: O(1)


Related Topics

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

Median Of Stream Of Running Integers in Java

An integer array is given to us. Compute the median of the elements traversed so far in the input array. For the sake of simplicity, assume that there are no...

10 minutes read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.