The friendly Operating System for the Internet of Things
bitfield.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 INRIA
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
25 #ifndef BITFIELD_H
26 #define BITFIELD_H
27 
28 #include <stdint.h>
29 #include <stdbool.h>
30 #include <stddef.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
42 #define BITFIELD(NAME, SIZE) uint8_t NAME[((SIZE) + 7) / 8]
43 
50 static inline void bf_set(uint8_t field[], size_t idx)
51 {
52  field[idx / 8] |= (1u << (idx % 8));
53 }
54 
61 static inline void bf_unset(uint8_t field[], size_t idx)
62 {
63  field[idx / 8] &= ~(1u << (idx % 8));
64 }
65 
72 static inline void bf_toggle(uint8_t field[], size_t idx)
73 {
74  field[idx / 8] ^= (1u << (idx % 8));
75 }
76 
83 static inline bool bf_isset(uint8_t field[], size_t idx)
84 {
85  return (field[idx / 8] & (1u << (idx % 8)));
86 }
87 
99 int bf_get_unset(uint8_t field[], int size);
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
106 #endif /* BITFIELD_H */
static void bf_set(uint8_t field[], size_t idx)
Set the bit to 1.
Definition: bitfield.h:50
static bool bf_isset(uint8_t field[], size_t idx)
Check if the bet is set.
Definition: bitfield.h:83
static void bf_unset(uint8_t field[], size_t idx)
Clear the bit.
Definition: bitfield.h:61
int bf_get_unset(uint8_t field[], int size)
Atomically get the number of an unset bit and set it.
static void bf_toggle(uint8_t field[], size_t idx)
Toggle the bit.
Definition: bitfield.h:72