summaryrefslogtreecommitdiff
path: root/handy.h
diff options
context:
space:
mode:
authorKarl Williamson <khw@cpan.org>2018-11-25 12:14:49 -0700
committerKarl Williamson <khw@cpan.org>2018-11-26 21:26:45 -0700
commit84ff4fa95d422991181a2bdc612d407edf091f79 (patch)
tree8d3f577f4af27e64d193f908184abd34018b7ece /handy.h
parentcb2d98ed897aaa5e67fb60b29abb441de7d63c3c (diff)
downloadperl-84ff4fa95d422991181a2bdc612d407edf091f79.tar.gz
Move isPOWER_OF_2() macro to handy.h
This is in preparation for it to be used outside of the file which previously defined it.
Diffstat (limited to 'handy.h')
-rw-r--r--handy.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/handy.h b/handy.h
index 3e1aa1bb1e..b7877b2663 100644
--- a/handy.h
+++ b/handy.h
@@ -255,6 +255,14 @@ typedef U64TYPE U64;
# endif
#endif
+/* Returns a boolean as to whether the input unsigned number is a power of 2
+ * (2**0, 2**1, etc). In other words if it has just a single bit set.
+ * If not, subtracting 1 would leave the uppermost bit set, so the & would
+ * yield non-zero */
+#if defined(PERL_CORE) || defined(PERL_EXT)
+# define isPOWER_OF_2(n) ((n & (n-1)) == 0)
+#endif
+
/* This is a helper macro to avoid preprocessor issues, replaced by nothing
* unless under DEBUGGING, where it expands to an assert of its argument,
* followed by a comma (hence the comma operator). If we just used a straight