Skip to content

Commit

Permalink
Create Count_set_bit.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartikay123 authored Oct 23, 2021
1 parent a151e5a commit d909621
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Count_set_bit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution{
public:

int countSetBits(int n)
{


if(n==0)return 0;
int x=(log2(n));
return((pow(2,x-1)*x)+(n-pow(2,x)+1)+countSetBits(n-pow(2,x)));

}


};



int main()
{
int t;
cin>>t;// input testcases
while(t--) //while testcases exist
{
int n;
cin>>n; //input n
Solution ob;
cout << ob.countSetBits(n) << endl;
}
return 0;
}

0 comments on commit d909621

Please sign in to comment.