Given two integers, the task is to find the hamming distance between two integers. Hamming Distance between two integers is the number of bits which are different at same position in both numbers.
Input: n1 = 9, n2 = 14 Output: 3 9 = 1001, 14 = 1110 No. of Different bits = 3 Input: n1 = 4, n2 = 8 Output: 2
- Calculate the XOR of two numbers.
- Count the number of set bits.
Below is the implementation of above approach:
Note: No. of set bits can be count using __builtin_popcount() function.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of Hamming difference of consecutive numbers from 0 to N | Set 2
- Distance between two nodes of binary tree with node values from 1 to N
- Maximum distance between two 1's in Binary representation of N
- Maximize distance between any two consecutive 1's after flipping M 0's
- Compute the minimum or maximum of two integers without branching
- Detect if two integers have opposite signs
- How to sum two integers without using arithmetic operators in C/C++?
- Compare two integers without using any Comparison operator
- Divide two integers without using multiplication, division and mod operator
- Bitwise recursive addition of two integers
- Number of mismatching bits in the binary representation of two integers
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Count number of steps to cover a distance if steps can be taken in powers of 2
- Smallest of three integers without comparison operators
- Find maximum XOR of given integer in a stream of integers
- Check if given four integers (or sides) make rectangle
- Number of integers with odd number of set bits
- Find the missing element in an array of integers represented in binary format
- Find a number which give minimum sum when XOR with every number of array of integers
- Queries to find the count of integers in a range that contain the given pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.