Skip to content

Commit edfb485

Browse files
authored
****************************BIT MANIPULATION***************************
1 parent d6fd137 commit edfb485

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Count Set Bits/program.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
int poweroftwo(int n)
2+
{
3+
int x=0;
4+
while((1<<x)<=n)
5+
{
6+
x++;
7+
}
8+
return x-1;
9+
}
10+
11+
int solve(int n)
12+
{
13+
if(n==0)
14+
{
15+
return 0;
16+
}
17+
int res=poweroftwo(n);
18+
int ans=(1<<res-1)*res+(n-(1<<x)+1)+solve(n-(1<<x));
19+
return ans;
20+
}

0 commit comments

Comments
 (0)