Counting no of set bits in all numbers between a and b

#include
#include
using namespace std;
int bitcount(long int x)
{
int d=(log2(x)+1);
int i;
int count=0;
int v=1;
for (i=0;i<d;i++)
{
if(i!=0)
{
v=v<<1;
}

	 if((v&x)>0)
	 {
       
		count+=1;
	 }

 }
 return count;

}

int main()
{
int t,x,y,i;
int count;
for(i=0;i<t;i++)
{
count=0;
cin>>x>>y;
for(i=x;i<=y;i++)
{
count+=bitcount(i);
}
cout<<count<<endl;
}

}

My actual code
Dont know what is wrong
logic is correct
works fine when checking for no of set bits in one number

#include
#include
using namespace std;
int bitcount(long int x)
{
int d=(log2(x)+1);
int i;
int count=0;
int v=1;
for (i=0;i<d;i++)
{
if(i!=0)
{
v=v<<1;
}

	 if((v&x)>0)
	 {
       
		count+=1;
	 }

 }
 return count;

}

int main()
{
int t,x,y,i;
int count;
for(i=0;i<t;i++)
{
count=0;
cin>>x>>y;
for(i=x;i<=y;i++)
{
count+=bitcount(i);
}
cout<<count<<endl;
}

}

Hi @nischay1111

u havent taken input t
so it assumes some garbage value

1 Like

Solved
Thanks for help !