Playing bits problem

here is my code:
#include
using namespace std;
int main() {
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int count=0;
int a,b;
cin>>a>>b;
for(int j=a;j<=b;j++)
{
while(j>0)
{
if(j&1)
++count;
j=j>>1;
}
}
cout<<count<<endl;
}
return 0;
}

why is infinite loop running

@gulatigarvita08 your j becomes 0 in the while loop so you cant use same var in for and while loop
here is the updated code i have commented the mistake -


Hope it solved your doubt dont forget to mark it resolved in my doubts section :smiley:

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.