[PATCH v2 18/20] linux/power_of_2.h: Add __IS_POWER_OF_2(n) and __IS_POWER_OF_2_OR_0(n) macros
From: Alejandro Colomar
Date: Sat Nov 20 2021 - 08:02:31 EST
Tested:
$ cat pow2.c
#include <stdio.h>
#define is_power_of_2_or_0(n) (((n) & ((n) - 1)) == 0)
#define is_power_of_2(n) (is_power_of_2_or_0(n) && ((n) != 0))
int main(void)
{
printf("%i, %i, %i\n", 8, is_power_of_2_or_0(8), is_power_of_2(8));
printf("%i, %i, %i\n", 7, is_power_of_2_or_0(7), is_power_of_2(7));
printf("%i, %i, %i\n", 6, is_power_of_2_or_0(6), is_power_of_2(6));
printf("%i, %i, %i\n", 5, is_power_of_2_or_0(5), is_power_of_2(5));
printf("%i, %i, %i\n", 4, is_power_of_2_or_0(4), is_power_of_2(4));
printf("%i, %i, %i\n", 3, is_power_of_2_or_0(3), is_power_of_2(3));
printf("%i, %i, %i\n", 2, is_power_of_2_or_0(2), is_power_of_2(2));
printf("%i, %i, %i\n", 1, is_power_of_2_or_0(1), is_power_of_2(1));
printf("%i, %i, %i\n", 0, is_power_of_2_or_0(0), is_power_of_2(0));
}
$ cc pow2.c
$ ./a.out
8, 1, 1
7, 0, 0
6, 0, 0
5, 0, 0
4, 1, 1
3, 0, 0
2, 1, 1
1, 1, 1
0, 1, 0
Signed-off-by: Alejandro Colomar <alx.manpages@xxxxxxxxx>
---
include/linux/power_of_2.h | 10 ++++++++++
1 file changed, 10 insertions(+)
create mode 100644 include/linux/power_of_2.h
diff --git a/include/linux/power_of_2.h b/include/linux/power_of_2.h
new file mode 100644
index 000000000000..812fe86eefcd
--- /dev/null
+++ b/include/linux/power_of_2.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_POWER_OF_2_H
+#define _LINUX_POWER_OF_2_H
+
+
+#define __IS_POWER_OF_2_OR_0(n) (((n) & ((n) - 1)) == 0)
+#define __IS_POWER_OF_2(n) (__IS_POWER_OF_2_OR_0(n) && ((n) != 0))
+
+
+#endif /* _LINUX_POWER_OF_2_H */
--
2.33.1