summaryrefslogtreecommitdiff
path: root/bits.h
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2010-11-18 13:54:22 +0100
committerTobias Klauser <tklauser@distanz.ch>2010-11-18 13:54:22 +0100
commit105d2c8f1436a91867ce352144baae5c390a32e1 (patch)
tree053d96dc750df2e549b7840a29cc86a02fab9638 /bits.h
parentf7af7bfcbf18011826beede7cd9e626af5f60765 (diff)
Add bit set/clear functions, differentiate bit mask macros
Diffstat (limited to 'bits.h')
-rw-r--r--bits.h26
1 files changed, 25 insertions, 1 deletions
diff --git a/bits.h b/bits.h
index 8374527..82eb153 100644
--- a/bits.h
+++ b/bits.h
@@ -11,6 +11,30 @@
#ifndef _BITS_H_
#define _BITS_H_
-#define BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL << (n))-1))
+#define BIT_MASK_FILL(n) (((n) == 64) ? ~0ULL : ((1ULL << (n))-1))
+#define BIT_MASK(n) (1UL << ((n) % 32))
+#define BIT_UINT32(n) ((n) / 32)
+
+/**
+ * Sets a bit in memory.
+ */
+static inline void set_bit(unsigned int nr, uint32_t *addr)
+{
+ uint32_t mask = BIT_MASK(nr);
+ uint32_t *p = addr + BIT_UINT32(nr);
+
+ *p |= mask;
+}
+
+/**
+ * Clears a bit in memory.
+ */
+static inline void clear_bit(unsigned int nr, uint32_t *addr)
+{
+ uint32_t mask = BIT_MASK(nr);
+ uint32_t *p = addr + BIT_UINT32(nr);
+
+ *p &= ~mask;
+}
#endif /* _BITS_H_ */