Playing with bit problem

here is my code:
#include
using namespace std;
int main() {
int t,count=0;
cin>>t;
for(int i=0;i<t;i++)
{
unsigned int a,b;
cin>>a>>b;
for(int j=a;j<=b;j++)
{
for(int k=0;k<63;k++)
{
int mask=1<<k;
if(j&mask)
++count;

		}
	}
	cout<<count<<endl;
}
return 0;

}

why the output is coming wrong

u are running ur loop upto 62 and 1<<62 is number that cannot fit in int .
so either run loop upto 31 ,or use long long for all variables.

1 Like