461. Hamming Distance

Input: x = 1, y = 4
Output: 2
Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
The above arrows point to positions where the corresponding bits are different.
Input: x = 3, y = 1
Output: 1
  • 0 <= x, y <= 231 - 1
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
total=0
while(x or y):
total+=(x&1) ^ (y&1)
x>>=1
y>>=1
return(total)
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
total=0
xor_=x^y
while(xor_):
xor_=(xor_) & (xor_-1)
total+=1
return(total)

--

--

--

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

High Availability with Heroku

How to generate a Normal Distribution dataset in Excel

Update Flutter App Remotely using Firebase Remote Config

Fun commands in Linux

Java Virtual Threads

Organising & Architecting for Sustained Digital Transformation

Magento 2.2 Features and Main Upgrades

Q&A on IPX Staking System

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

More from Medium

LINKED LIST-SINGLY LNKED LIST

141. Linked List Cycle

String to Integer (atoi) — LeetCode Solutions

266 Palindrome Permutation