#include
using namespace std;
int main() {
int q;
cin>>q;
while(q){
int a,b;
cin>>a>>b;
int total=0;
for(int i=a;i<=b;i++){
int count=0;
while(i){
i=(i&(i-1));
count++;
}
total+=count;
}
cout<<total<<endl;
q--;
}
return 0;
}
#include
using namespace std;
int main() {
int q;
cin>>q;
while(q){
int a,b;
cin>>a>>b;
int total=0;
for(int i=a;i<=b;i++){
int count=0;
while(i){
i=(i&(i-1));
count++;
}
total+=count;
}
cout<<total<<endl;
q--;
}
return 0;
}
hi aayush
there is small change required in your code
in the for loop
you are continously changing the value of the i(index) only while calculating the number of set bits in the range
instead you have to find set bit for each number in the range.
so
make the following changes
for(int i=a;i<=b;i++){
int count=0;
int val=i;
while(val){
val=(val&(val-1));
count++;
}
ohhhhhhh yaaa thankyou…
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.