Skip to content

Commit

Permalink
Improve nextPowerOfTwo helper function (#26)
Browse files Browse the repository at this point in the history
Improve speed and avoid potential errors caused by casting of int to float64.
  • Loading branch information
mrekucci authored and dajohi committed Mar 14, 2018
1 parent 32e7a98 commit e8c68f8
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions merkle/merkle.go
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"math"
"math/bits"
"sort"
)

Expand Down Expand Up @@ -67,10 +68,7 @@ func nextPowerOfTwo(n int) int {
if n&(n-1) == 0 {
return n
}

// Figure out and return the next power of two.
exponent := uint(math.Log2(float64(n))) + 1
return 1 << exponent // 2^exponent
return 1 << uint64(bits.Len(uint(n)))
}

// Tree creates a merkle tree from a slice of transactions,
Expand Down

0 comments on commit e8c68f8

Please sign in to comment.