-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountBits.go
More file actions
35 lines (28 loc) · 711 Bytes
/
CountBits.go
File metadata and controls
35 lines (28 loc) · 711 Bytes
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
package basic_programming
import (
"fmt"
)
/*
Given a number N, print the number of set bits in the binary representation of this number.
Input:
The first contains a single integer T denoting the number of test cases. Each test case contains a single integer N
Output:
For each test case, print a single integer denoting the number of set bits in the binary representation of the given N .
*/
func PrintCountBits() {
var N int
fmt.Scanf("%d", &N)
for i := 0;i < N; i += 1 {
var num int
fmt.Scanf("%d", &num)
fmt.Println(CountBits(num))
}
}
func CountBits(num int) int {
counter := 0
for num > 0 {
num = num & (num - 1) // flips rightmost bit everytime
counter += 1
}
return counter
}