summaryrefslogtreecommitdiffhomepage
path: root/pkg/bits
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/bits')
-rwxr-xr-xpkg/bits/bits32.go8
-rwxr-xr-xpkg/bits/bits64.go8
2 files changed, 16 insertions, 0 deletions
diff --git a/pkg/bits/bits32.go b/pkg/bits/bits32.go
index 4e9e45dce..28134a9e7 100755
--- a/pkg/bits/bits32.go
+++ b/pkg/bits/bits32.go
@@ -23,3 +23,11 @@ func Mask32(is ...int) uint32 {
func MaskOf32(i int) uint32 {
return uint32(1) << uint32(i)
}
+
+// IsPowerOfTwo returns true if v is power of 2.
+func IsPowerOfTwo32(v uint32) bool {
+ if v == 0 {
+ return false
+ }
+ return v&(v-1) == 0
+}
diff --git a/pkg/bits/bits64.go b/pkg/bits/bits64.go
index f49158792..73117b19b 100755
--- a/pkg/bits/bits64.go
+++ b/pkg/bits/bits64.go
@@ -23,3 +23,11 @@ func Mask64(is ...int) uint64 {
func MaskOf64(i int) uint64 {
return uint64(1) << uint64(i)
}
+
+// IsPowerOfTwo returns true if v is power of 2.
+func IsPowerOfTwo64(v uint64) bool {
+ if v == 0 {
+ return false
+ }
+ return v&(v-1) == 0
+}