fix zero being power of two (#1684)

This commit is contained in:
Preston Van Loon 2019-02-22 09:45:25 -05:00 committed by terence tsao
parent 69058c126d
commit 9f533fb7ae
2 changed files with 5 additions and 1 deletions

View File

@ -27,7 +27,7 @@ func CeilDiv8(n int) int {
// IsPowerOf2 returns true if n is an // IsPowerOf2 returns true if n is an
// exact power of two. False otherwise. // exact power of two. False otherwise.
func IsPowerOf2(n uint64) bool { func IsPowerOf2(n uint64) bool {
return (n & (n - 1)) == 0 return n != 0 && (n&(n-1)) == 0
} }
// PowerOf2 returns an integer that is the provided // PowerOf2 returns an integer that is the provided

View File

@ -95,6 +95,10 @@ func TestIsPowerOf2(t *testing.T) {
a: 1024, a: 1024,
b: true, b: true,
}, },
{
a: 0,
b: false,
},
} }
for _, tt := range tests { for _, tt := range tests {
if tt.b != IsPowerOf2(tt.a) { if tt.b != IsPowerOf2(tt.a) {