-
Notifications
You must be signed in to change notification settings - Fork 258
/
powers_of_two.go
44 lines (37 loc) · 1.11 KB
/
powers_of_two.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package shares
import (
"fmt"
"golang.org/x/exp/constraints"
)
// RoundUpPowerOfTwo returns the next power of two greater than or equal to input.
func RoundUpPowerOfTwo[I constraints.Integer](input I) I {
var result I = 1
for result < input {
result = result << 1
}
return result
}
// RoundDownPowerOfTwo returns the next power of two less than or equal to input.
func RoundDownPowerOfTwo[I constraints.Integer](input I) (I, error) {
if input <= 0 {
return 0, fmt.Errorf("input %v must be positive", input)
}
roundedUp := RoundUpPowerOfTwo(input)
if roundedUp == input {
return roundedUp, nil
}
return roundedUp / 2, nil
}
// RoundUpPowerOfTwo returns the next power of two that is strictly greater than input.
func RoundUpPowerOfTwoStrict[I constraints.Integer](input I) I {
result := RoundUpPowerOfTwo(input)
// round the result up to the next power of two if is equal to the input
if result == input {
return result * 2
}
return result
}
// IsPowerOfTwo returns true if input is a power of two.
func IsPowerOfTwo[I constraints.Integer](input I) bool {
return input&(input-1) == 0 && input != 0
}