lkml.org 
[lkml]   [2021]   [Nov]   [20]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH v2 18/20] linux/power_of_2.h: Add __IS_POWER_OF_2(n) and __IS_POWER_OF_2_OR_0(n) macros
Date
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@gmail.com>
---
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
\
 
 \ /
  Last update: 2021-11-20 14:03    [W:0.338 / U:0.676 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site