-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count Total Setbits
41 lines (33 loc) · 933 Bytes
/
Count Total Setbits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//{ Driver Code Starts
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while(t-- > 0){
long N;
N = Long.parseLong(br.readLine().trim());
Solution obj = new Solution();
long res = obj.countBits(N);
System.out.println(res);
}
}
}
// } Driver Code Ends
class Solution {
public static long countBits(long N) {
// code here
long ans = 0,i=1,n=N;
while(n!=0){
long temp = (N+1)/(i*2);
ans+=temp*i;
temp = (N+1)/i;
if(temp%2==1)ans+=(N+1)%i;
i*=2;
n>>=1;
}
return ans;
}
}