Why this solution is not passing all the test cases?

Sir i wanted to know that why this solution is passing only 1 test case, because i think this is correct solution!

#include<bits/stdc++.h>
using namespace std;

int main(){

int t;
cin>>t;
while(t--){
	int n;
	cin>>n;
	if((n&(n-1))!=0){
		int p=log2(n);
		int a=1<<p;
		int d=n-a;
		int q=d&(d-1);
		if(q==0) cout<<"2"<<endl;
		else cout<<"3"<<endl;
	}
	else cout<<"1"<<endl;
}
return 0;

}

Hello @rohitmathur.mathur.6 in this question you have to just find the number of set bits to find the answer.
and you can simply do in this way:
cout<<__builtin_popcount(n)<<endl;
if you have any doubt you can ask here:
Happy Learning!!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.