-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountTotalSubbits.java
More file actions
49 lines (35 loc) · 1.19 KB
/
Copy pathCountTotalSubbits.java
File metadata and controls
49 lines (35 loc) · 1.19 KB
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
42
43
44
45
46
47
48
49
//{ 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
if(n==0)
return 0;
long x = larPowOf2(n);
long y = x * (1 << (x - 1));
long z = n - (1 << x);
return y + z + 1 + countBits(z);
}
static long larPowOf2(long n) {
long x = 0;
while((1 << x) <= n)
x++;
return x - 1;
}
}