The Hamming distance between two non-negative integers is the number of bit positions at which the corresponding bits differ. Compute it by combining the two numbers with a bitwise XOR and counting the resulting set bits. Return 0 when either input is null.

Examples:

Input: x = 1, y = 4 Output: 2 Explanation: 1 is 001 and 4 is 100, they differ in two positions Input: x = 3, y = 1 Output: 1 Explanation: 3 is 011 and 1 is 001, they differ in one position Input: x = 7, y = 0 Output: 3 Explanation: 7 is 111 and 0 is 000, they differ in three positions Input: x = 5, y = 5 Output: 0 Explanation: Identical numbers have no differing bits Input: x = null, y = 3 Output: 0 Explanation: Null input returns 0
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay October 2026 (Oct 16–31)Sponsored by FLXBL

Contest runs October 16 - 31. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 16 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In