XNOR of two numbers | CPP program to find XNOR of two numbers . ; Please refer below post for details of this function https : www . geeksforgeeks . org / toggle - bits - significant - bit / ; Make a copy of n as we are going to change it . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Returns XNOR of num1 and num2 ; if num2 is greater then we swap this number in num1 ; Driver code
#include <iostream> NEW_LINE 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 ; }