Monk and his Friend

QUESTION

Monk has a very good friend, Puchi. As weird as his name, are the games he plays. One fine day, they decided to play a game to test how diverse their choices are. \n\nBoth of them choose exactly one integer each. Monk chooses an integer M and Puchi chooses an integer P. \n\nThe diversity of their choices is defined as the number of bits whose status is different in the binary representation of M and P , i.e. , count of bits that are ,either set in M and unset in P or set in P and unset in M.\n\nFind the answer to their game.\n\nInput:\nFirst line contains T. T test cases follow.\nEach test case consists of 2 space-separated integers P and M.\n\nOutput:\nPrint the answer to each test case in a new line.\n\nConstraints:\n1 <= T<= 10^4\n0<= M, P <= 10^16.

ANSWER

#include<iostream>

using namespace std;

int main()
{
       int t;
       cin>>t;
       while(t--)
       {
              unsigned long long int m,p,x=0;
              cin>>m>>p;
              while(m>0 || p>0)
              {
                     if(m%2 != p%2)
                     {
                           x++;
                     }
                     m=m/2;
                     p=p/2;
              }
              cout<<x<<endl;
       }
       return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro