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.
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.
Time Complexity: O(1).
- 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 whether bitwise AND of N numbers is Even or Odd
- Check if two numbers are bit rotations of each other or not
- Check whether product of 'n' numbers is even or odd
- Check whether bitwise OR of N numbers is Even or Odd
- Check if one of the numbers is one's complement of the other
- 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 comparison operators
- Check if a number can be expressed as a sum of consecutive numbers
- Check if product of first N natural numbers is divisible by their sum
- Check if binary representations of two numbers are anagram
- Check whether factorial of N is divisible by sum of first N natural numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.