# Hi, I'm Ataias Reis

Programmer, made in Brazil, Alma mater University of Brasilia

# 30 days of code in Go: Day 10 - Binary Numbers

The challenge for today is interesting, but I keep wondering if I could do it more efficiently. Let me know in the comments if you have any suggestion after reading my solution.

The problem is counting the maximum number of consecutive binary ones in an integer. It is easier to understand with an example:

• Consider the number 25 represented in base 10
• Its representation in binary is 11001
• The max number of consecutive ones we can see is 2

What I did was shifting to the right and checking the least significant bit until every single bit of it was checked. The solution is available below.

``````package main

import "fmt"

func getMaxConsecutiveOnes(N uint) uint {
var maxCount uint = 0
var count uint = 0
var i uint
for i = 0; i < 64; i++ {
if (N>>i)&0x1 == 1 {
count++
} else {
if count > maxCount {
maxCount = count
}
count = 0
}
}

return maxCount
}

func main() {

var N uint
fmt.Scanln(&N)
fmt.Printf("%d\n", getMaxConsecutiveOnes(N))

}

``````

An example of input and output:

``````# input
13
# output
2
``````

That's all for today, thanks for reading!