Skip to content

Commit

Permalink
Add documention to NextPow2 function.
Browse files Browse the repository at this point in the history
  • Loading branch information
armfazh committed Jan 17, 2025
1 parent f99433a commit 39b014f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions math/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package math

import "math/bits"

func NextPow2(n uint) (twoN uint, N uint) {
// NextPow2 finds the next power of two (N=2^k, k>=0) greater than n.
// If n is already a power of two, then this function returns n, and log2(n).
func NextPow2(n uint) (N uint, k uint) {
if bits.OnesCount(n) == 1 {
return n, uint(bits.TrailingZeros(n))
k = uint(bits.TrailingZeros(n))
N = n
} else {
N = uint(bits.Len(n))
return uint(1) << N, N
k = uint(bits.Len(n))
N = uint(1) << k
}
return
}

0 comments on commit 39b014f

Please sign in to comment.