Skip to content

Latest commit

 

History

History
52 lines (35 loc) · 1.06 KB

File metadata and controls

52 lines (35 loc) · 1.06 KB

METHOD 1

int check(int a, int i)
    {
        if(a& ( 1<<i) )
            return 1;
        
        return 0;
    }
    int countBitsFlip(int a, int b){
        
        // Your logic here
        
        int count=0;
        int n = max((int)log2(a), (int)log2(b)) + 1; // Fixed the typo and added a missing semicolon

        for (int i = 0; i < n; i++)  // Changed <= to <
            if (check(a, i) != check(b, i))
                count++;
                
        return count;
        
    }

METHOD 2

 // Function to find number of bits needed to be flipped to convert A to B
    int countBitsFlip(int a, int b){
        
        // Your logic here
        
        int s=a^b;    //common bits become 0 and diff bit become 1
        
        int count=0;
        
        //count the no. of set bits
        while(s)
        {
            s= (s & (s-1));
            count++;
        }
        
        return count;
    }