Skip to content

Commit 15df786

Browse files
authored
Playing With Bits
1 parent 8b06183 commit 15df786

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Playing With Bits.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int setBit(int n)
4+
{
5+
int ans = 0;
6+
while(n != 0)
7+
{
8+
ans += (n&1);
9+
n= n>>1;
10+
}
11+
return ans;
12+
13+
14+
}
15+
16+
17+
int main()
18+
{
19+
20+
int tc;
21+
cin>>tc;
22+
while(tc--)
23+
{
24+
25+
int a , b;
26+
cin>>a>>b;
27+
int cnt = 0;
28+
for(int i = a; i<= b ; i++)
29+
{
30+
cnt += setBit(i);
31+
}
32+
cout<<cnt;
33+
34+
}
35+
36+
37+
38+
39+
40+
}

0 commit comments

Comments
 (0)