```
public class Solution {
public int totalHammingDistance(int[] nums) {
int n = nums.length;
int sum = 0;
int[][] pair = new int[32][2];
for (int num : nums) {
int k = 0;
while (k < 32) {
int cur = (num & 1);
if (cur == 0) {
sum += pair[k][1];
} else {
sum += pair[k][0];
}
pair[k][cur]++;
k++;
num >>= 1;
}
}
return sum;
}
}
```