Skip to content

Commit

Permalink
BinaryGap.go
Browse files Browse the repository at this point in the history
  • Loading branch information
zengfenfei committed Apr 8, 2016
1 parent 2f9250a commit 251f9ad
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions BinaryGap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package codility

// https://codility.com/programmers/task/binary_gap/
// N [1..2,147,483,647]
func BinaryGap(N int) int {
max0s := 0
cur0s := -1
for i := uint8(0); i < 31; i++ {
if N&(1<<i) > 0 { // Bit is 1
if cur0s > max0s {
max0s = cur0s
}
cur0s = 0
} else if cur0s != -1 {
cur0s++
}
}
return max0s
}

0 comments on commit 251f9ad

Please sign in to comment.