Categories
Go: Count Bits
Date
Author
Implement a function that counts the number of set bits in the binary representation of a 32-bit integer.
Example
num = 126
126 = 11111102 has 6 bits set
num =128
128 = 100000002 has 1 bit set.
Function Description
Complete the function countBits in the editor below.
countBits has the following parameter:
uint32 num: an integer value
Returns:
int32: the number of bits set in num
Constraints
- 0 ≤ num ≤ 232 – 1
SOLUTION:
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// Complete the 'countBits' function below.
// The function is expected to return an int32.
// The function accepts uint32 num as parameter.
func countBits(num uint32) int32 {
count := 0
for num > 0 {
// Use bitwise AND operation to check if the least significant bit is set
if num&1 == 1 {
count++
}
// Right shift the number by 1 bit
num >>= 1
}
return int32(count)
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16*1024*1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 16*1024*1024)
numTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
num := uint32(numTemp)
result := countBits(num)
fmt.Fprintf(writer, "%d\n", result)
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}