# LeetCode - 0461. Hamming Distance ### 題目網址:https://leetcode.com/problems/hamming-distance/ ###### tags: `LeetCode` `Easy` `位元運算` ```cpp= /* -LeetCode format- Problem: 461. Hamming Distance Difficulty: Easy by Inversionpeter */ class Solution { int hammingWeight(uint32_t n) { n -= (n >> 1) & 0x55555555; n = (n & 0x33333333) + ((n >> 2) & 0x33333333); n = (n + (n >> 4)) & 0x0F0F0F0F; return (n * 0x01010101) >> 24; } public: int hammingDistance(int x, int y) { return hammingWeight(x ^ y); } }; ```