ndn-lite
bit-operations.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Xinyu Ma
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v3.0. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 #ifndef UTIL_BIT_OPERATIONS_H_
9 #define UTIL_BIT_OPERATIONS_H_
10 
11 #include <stddef.h>
12 #include <stdint.h>
13 
14 typedef uint64_t ndn_bitset_t;
15 
16 #define LEAST_SIG_BIT(x) ((x) & (-(x)))
17 
18 static inline ndn_bitset_t bitset_set(ndn_bitset_t set, size_t val){
19  return (set | (((ndn_bitset_t)1) << ((ndn_bitset_t)val)));
20 }
21 
22 static inline ndn_bitset_t bitset_unset(ndn_bitset_t set, size_t val){
23  return (set & ~(((ndn_bitset_t)1) << ((ndn_bitset_t)val)));
24 }
25 
26 static inline size_t bitset_log2(ndn_bitset_t val){
27  return __builtin_ctz(val);
28 }
29 
30 static inline size_t bitset_pop_least(ndn_bitset_t* val){
31  ndn_bitset_t lsbit = LEAST_SIG_BIT(*val);
32  size_t ret = bitset_log2(lsbit);
33  *val &= ~lsbit;
34  return ret;
35 }
36 
37 #endif // UTIL_BIT_OPERATIONS_H_
static size_t bitset_pop_least(ndn_bitset_t *val)
Definition: bit-operations.h:30
static ndn_bitset_t bitset_set(ndn_bitset_t set, size_t val)
Definition: bit-operations.h:18
static size_t bitset_log2(ndn_bitset_t val)
Definition: bit-operations.h:26
static ndn_bitset_t bitset_unset(ndn_bitset_t set, size_t val)
Definition: bit-operations.h:22
#define LEAST_SIG_BIT(x)
Definition: bit-operations.h:16
uint64_t ndn_bitset_t
Definition: bit-operations.h:14