From 5d8383f23f2d825bf22a88e982684a114c2ff77a Mon Sep 17 00:00:00 2001 From: Udhay <72250606+Udhay-Brahmi@users.noreply.github.com> Date: Tue, 8 Dec 2020 09:07:41 +0530 Subject: [PATCH] Create Count total set bits --- Count total set bits | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Count total set bits diff --git a/Count total set bits b/Count total set bits new file mode 100644 index 0000000..01eab9a --- /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