solution.js 318 B

12345678910111213141516171819202122232425
  1. /**
  2. * @param {number} x
  3. * @param {number} y
  4. * @return {number}
  5. */
  6. function countOnesBits(x) {
  7. var result = 0;
  8. while (x > 0) {
  9. if (x % 2 === 1) {
  10. result++;
  11. }
  12. x = x >> 1;
  13. }
  14. return result;
  15. }
  16. function hammingDistance(x, y) {
  17. return countOnesBits(x ^ y);
  18. }