public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Introduce a boolean "single_bit_set" function.
@ 2009-04-23 17:43 Robert P. J. Day
  2009-04-23 19:57 ` David Daney
  2009-05-28 12:21 ` Petr Tesarik
  0 siblings, 2 replies; 17+ messages in thread
From: Robert P. J. Day @ 2009-04-23 17:43 UTC (permalink / raw)
  To: Linux Kernel Mailing List; +Cc: Andrew Morton


A boolean single_bit_set() routine would simplify the numerous
constructs of the form (((n & (n - 1)) == 0)) when testing for
single-bitness.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

---

This is similar to the current is_power_of_2() routine defined in
include/linux/log2.h, which is mathematically identical but,
semantically, should be defined independently just so the code is more
readable.

I'm open to an alternative function name.

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index 6182913..1c0c840 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -45,6 +45,13 @@ static inline unsigned long hweight_long(unsigned long w)
 	return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
 }

+static inline __attribute__((const))
+bool single_bit_set(unsigned long n)
+{
+        return (n != 0 && ((n & (n - 1)) == 0));
+}
+
+
 /**
  * rol32 - rotate a 32-bit value left
  * @word: value to rotate


========================================================================
Robert P. J. Day                               Waterloo, Ontario, CANADA

        Linux Consulting, Training and Annoying Kernel Pedantry.

Web page:                                          http://crashcourse.ca
Linked In:                             http://www.linkedin.com/in/rpjday
Twitter:                                       http://twitter.com/rpjday
========================================================================

^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2009-06-30 10:20 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-23 17:43 [PATCH] Introduce a boolean "single_bit_set" function Robert P. J. Day
2009-04-23 19:57 ` David Daney
2009-04-23 20:11   ` Robert P. J. Day
2009-04-23 23:57   ` Andrew Morton
2009-04-24 10:40     ` Robert P. J. Day
2009-04-24 17:46       ` Andrew Morton
2009-04-25 22:09         ` Robert P. J. Day
2009-06-29 18:15         ` Petr Tesarik
2009-06-29 18:50           ` Robert P. J. Day
2009-06-30  6:12             ` Petr Tesarik
2009-06-30 10:18               ` Robert P. J. Day
2009-04-24 13:51     ` Robert P. J. Day
2009-05-28 12:21 ` Petr Tesarik
2009-05-28 12:27   ` Robert P. J. Day
2009-05-28 12:32   ` Robert P. J. Day
2009-05-28 13:12     ` Petr Tesarik
2009-06-29 18:50       ` H. Peter Anvin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox