Check whether the two numbers differ at one bit position only - GeeksforGeeks

13 Apr, 2021
Given two non-negative integers a and b. The problem is to check whether the two numbers differ at one bit position only or not.Examples:  
Input : a = 13, b = 9
Output : Yes
(13)10 = (1101)2
(9)10 = (1001)2
Both the numbers differ at one bit position only, i.e,
differ at the 3rd bit from the right.

Input : a = 15, b = 8
Output : No
 
Approach: Following are the steps: 
Calculate num = a ^ b.Check whether num is a power of 2 or not. Refer this post.
Calculate num = a ^ b.
Check whether num is a power of 2 or not. Refer this post.
 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to check whether the two// numbers differ at one bit position only#include <bits/stdc++.h>using namespace std; // function to check if x is power of 2bool isPowerOfTwo(unsigned int x){    // First x in the below expression is    // for the case when x is 0    return x && (!(x & (x - 1)));} // function to check whether the two numbers// differ at one bit position onlybool differAtOneBitPos(unsigned int a,                       unsigned int b){    return isPowerOfTwo(a ^ b);} // Driver program to test aboveint main(){    unsigned int a = 13, b = 9;    if (differAtOneBitPos(a, b))        cout << "Yes";    else        cout << "No";    return 0;}
// Java implementation to check whether the two // numbers differ at one bit position onlyimport java.io.*;import java.util.*; class GFG {      // function to check if x is power of 2      static boolean isPowerOfTwo(int x)      {             // First x in the below expression is             // for the case when x is 0             return x!= 0 && ((x & (x - 1)) == 0);      }       // function to check whether the two numbers      // differ at one bit position only      static boolean differAtOneBitPos(int a, int b)      {             return isPowerOfTwo(a ^ b);      }      // Driver code      public static void main(String args[])      {             int a = 13, b = 9;             if (differAtOneBitPos(a, b) == true)                 System.out.println("Yes");             else                 System.out.println("No");      }} // This code is contributed by rachana soma
# Python3 implementation to check whether the two# numbers differ at one bit position only # function to check if x is power of 2def isPowerOfTwo( x ):     # First x in the below expression is    # for the case when x is 0    return x and (not(x & (x - 1))) # function to check whether the two numbers# differ at one bit position onlydef differAtOneBitPos( a , b ):    return isPowerOfTwo(a ^ b)     # Driver code to test abovea = 13b = 9if (differAtOneBitPos(a, b)):    print("Yes")else:    print( "No") # This code is contributed by "Sharad_Bhardwaj".
// C# implementation to check whether the two// numbers differ at one bit position onlyusing System;class GFG{         // function to check if x is power of 2    static bool isPowerOfTwo(int x)    {        // First x in the below expression is        // for the case when x is 0        return x != 0 && ((x & (x - 1)) == 0);    }     // function to check whether the two numbers    // differ at one bit position only    static bool differAtOneBitPos(int a, int b)    {        return isPowerOfTwo(a ^ b);    }         // Driver code    public static void Main()    {        int a = 13, b = 9;        if (differAtOneBitPos(a, b) == true)            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }} // This code is contributed by ihritik
<?php// PHP implementation to check// whether the two numbers differ// at one bit position only     // function to check if x is power of 2    function isPowerOfTwo($x)    {        $y = 0;                 // First x in the below expression is        // for the case when x is 0        if($x && (!($x & ($x - 1))))        $y = 1;        return $y;    }         // function to check whether    // the two numbers differ at    // one bit position only    function differAtOneBitPos($a,$b)    {        return isPowerOfTwo($a ^ $b);    }             // Driver Code        $a = 13;        $b = 9;        if (differAtOneBitPos($a, $b))            echo "Yes";        else            echo "No";     // This code is contributed by Sam007?>
<script> // JavaScript program to check whether the two // numbers differ at one bit position only       // function to check if x is power of 2      function isPowerOfTwo(x)      {                    // First x in the below expression is             // for the case when x is 0             return x!= 0 && ((x & (x - 1)) == 0);      }         // function to check whether the two numbers      // differ at one bit position only      function differAtOneBitPos(a, b)      {             return isPowerOfTwo(a ^ b);      }  // Driver code                 let a = 13, b = 9;             if (differAtOneBitPos(a, b) == true)                 document.write("Yes");             else                 document.write("No");               // This code is contributed by code_hunt.</script>
Output:  
Yes
Time Complexity: O(1). 
Sam007
rachana soma
ihritik
code_hunt
Numbers
Operators
Bit Magic
Bit Magic
Numbers
Operators
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Binary representation of a given number
Bits manipulation (Important tactics)
Bit Fields in C
Add two numbers without using arithmetic operators
Find the element that appears once
Set, Clear and Toggle a given bit of a number in C
Josephus problem | Set 1 (A O(n) Solution)
C++ bitset and its application