Struct in bit manipulation

can anyone explain the logic i mean how do we get to strike those logic.
question-link

MY LOGIC
long sumXor(long n) {
long long int x=0,count=0;
while(x!=n)
{
if((x+n)==(x^n))
{
count++;
}
x++;
}
return count;
}
tells TLE
OTHER PEOPLE CODE
long sumXor(long n) {
long long int x=0,count=0;
while(n>0)
{
if((n&1)==0)
{
count++;
}
n>>=1;
}
return pow(2,count);
}
i understand the logic behind the code but i dont know to how get these type of logics for other questions, can anyone tell me best way to master only bit-Manipulation i am not that begginer completed a lot of videos and notes.