diff --git a/Count total set bits b/Count total set bits new file mode 100644 index 000000000..01eab9a75 --- /dev/null +++ b/Count total set bits @@ -0,0 +1,40 @@ +// { Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + + + // } Driver Code Ends + + +//User function Template for C++ + +// Function to count set bits in the given number x +// n +int countSetBits(int n) +{ + // Your logic here + if(n==0)return 0; + int x=log(n)/log(2); + return (x*(1<<(x-1)) + n+1-(1<>t;// input testcases + while(t--) //while testcases exist + { + int n; + cin>>n; //input n + + cout << countSetBits(n) << endl;// print the answer + } + return 0; +} + // } Driver Code Ends