This code is not showing any output
Playing With Bits
Can you post your code?
#include
using namespace std;
int countbits(int n1,int n2)
{
int ans=0;
while(n1<=n2)
{
while(n1>0)
{
n1=n1&(n1-1);
ans++;
}
n1++;
}
return ans;
}
int main() {
int t;
cin>>t;
while(t!=0)
{
int n1,n2;
cin>>n1>>n2;
cout<<countbits(n1,n2)<<endl;
t–;
}
return 0;
}
in the above code it is t—
Store the number you’re working upon in a separate variable. Because n1 was getting updated by the inner loop whenever the loop ran, so your outer loop was not able to run for all numbers between n1 and n2. this should be your updated loop.
while(n1<=n2)
{
int no = n1;
while(no > 0)
{
no=no&(no-1);
ans++;
}
n1++;
}
Try and check if it works now
yeah it is working
thanks for your help
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.