The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x
and y
, calculate the Hamming distance.
Note:
0 ≤ x
, y
< 231.
public int HammingDistance(int x, int y) {
int dis=0;
List xx = new List();
List yy = new List();
while(x/2!=0||x%2!=0)
{
xx.Add(x%2);
x = x / 2;
}
while(y / 2 != 0 || y % 2 != 0)
{
yy.Add(y % 2);
y = y / 2;
}
int xCount = xx.Count;
int yCount = yy.Count;
int MinCount=xCount;
if (xCount > yCount)
{
while (xCount != yCount)
{
yy.Add(0);
yCount++;
}
}
else
{
while (xCount != yCount)
{
xx.Add(0);
xCount++;
}
}
for(int i=0;i