Why my code is returning + 2 bits for every input except 0

code- https://ide.codingblocks.com/s/233924

i have added fast count and it is not working too code- https://ide.codingblocks.com/s/233933

For Basic Apprach

it will depend on system
if you write

 while(i<32){
        if( n & n<<i){
            ans++;
        }
        i++;
    }

it will run

if int is 64 bit only then your code will give correct output

what about fast count??

in fast count you have done
n&n-1 n&n-2 n&n-3 till n&n-i is not zero

which is not correct what you have to do is
n=n&n-1;
so that every time n and n-1 both will update
for Ex n=13, n-1=12

  1. n=n&n-1 so n=13&12=12
  2. n=12&11=8
    3)n=8&7=0

so ans is 3 as 3 iteration required
in this way you have to do

i hope you understand

if you have more doubts regarding this feel free to ask
and if your doubt is resolved please mark it as resolved from your doubt section in your course

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.