# Check whether the two numbers differ at one bit position only

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)= (1_{10}101)_{2}(9)= (1_{10}001)_{2}Both the numbers differ at one bit position only, i.e, differ at the3rdbit 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.

## C++

`// 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 2 ` `bool` `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 only ` `bool` `differAtOneBitPos(unsigned ` `int` `a, ` ` ` `unsigned ` `int` `b) ` `{ ` ` ` `return` `isPowerOfTwo(a ^ b); ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `unsigned ` `int` `a = 13, b = 9; ` ` ` `if` `(differAtOneBitPos(a, b)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to check whether the two ` `// numbers differ at one bit position only ` `import` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to check whether the two ` `# numbers differ at one bit position only ` ` ` `# function to check if x is power of 2 ` `def` `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 only ` `def` `differAtOneBitPos( a , b ): ` ` ` `return` `isPowerOfTwo(a ^ b) ` ` ` `# Driver code to test above ` `a ` `=` `13` `b ` `=` `9` `if` `(differAtOneBitPos(a, b)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `( ` `"No"` `) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to check whether the two ` `// numbers differ at one bit position only ` `using` `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 ` |

*chevron_right*

*filter_none*

## PHP

`<?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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** O(1).

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Check whether the bit at given position is set or unset
- Position of rightmost common bit in two numbers
- Find position of left most dis-similar bit for two numbers
- Count all pairs of an array which differ in K bits
- Check if a given pair of Numbers are Betrothed numbers or not
- Check whether bitwise AND of N numbers is Even or Odd
- Check whether product of 'n' numbers is even or odd
- Check if two numbers are bit rotations of each other or not
- Check if one of the numbers is one's complement of the other
- Check whether bitwise OR of N numbers is Even or Odd
- Check the divisibility of Hexadecimal numbers
- Check whether factorial of N is divisible by sum of first N natural numbers
- Check if product of first N natural numbers is divisible by their sum
- Check if a number can be expressed as a sum of consecutive numbers
- Check if binary representations of two numbers are anagram
- Check if two numbers are equal without using comparison operators
- Check if a number can be written as a sum of 'k' prime numbers
- Check if a number can be represented as a sum of 2 triangular numbers
- Check if bits in range L to R of two numbers are complement of each other or not
- Check if two numbers are equal without using arithmetic and comparison operators

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.