Hamming Distance Solution


  • 0
    G

    namespace HammingDistance
    {
    using System;

    class HammingDistance
    {
        static void Main(string[] args)
        {
            HammingDistance bitcalculator = new HammingDistance();
            Console.WriteLine("Hamming Distance for 2 and 4 is {0}",bitcalculator.HamDist());
            Console.ReadKey();
        }
    
        public int HamDist()
        {
            int count = 0 ;
            int x = 2;
            int y = 4;
            int final = x ^ y;
    
            while (final != 0)
            {
                count++;
                final &= (final - 1);                
            }
            return count;
        }
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.