sol: https://ide.codingblocks.com/s/381774
prob: https://hack.codingblocks.com/app/contests/1975/65/problem
Bitwise operator problem (going into tle)
Hey @raghav007 the logic you are using is wrong
Use this instead
int countSetBits(int a, int b){
int ans=0;
for(int i=a;i<=b;i++){
int j=0;
while( i>>j ){
ans +=((i>>j)&1);
j++;
}
}
return ans;
}
im not able to understand this implementation after the first for loop like why is j = 0 and why are we shifting i j times
Extracting all the set bits from i at every ity digit means if i is
i = 10110
So j is traversing from back
1. 10110
^
J
2. 10110
^
J
.
.
.
5. 10110
^
J
oh okay but how can j act as a pointer because 10110 is not in an array
It’s not a pointer , j pointing to any index means that
If it’s pointing
10111
—^
—j000
Which means that j is one and remaining digit behind is zero
Then we do and of j and that index and count if it’s a set bit or not
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.