Root/b2/bitset.h

1/*
2 * bitset.h - Bit sets
3 *
4 * Copyright 2012 by Werner Almesberger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#ifndef BITSET_H
13#define BITSET_H
14
15#include <stdint.h>
16
17#include "bitset.h"
18
19
20struct bitset {
21    uint64_t v;
22};
23
24
25static inline void bitset_zero(struct bitset *b)
26{
27    b->v = 0;
28}
29
30
31static inline void bitset_set(struct bitset *b, int n)
32{
33    b->v |= (uint64_t) 1 << n;
34}
35
36
37static inline void bitset_clear(struct bitset *b, int n)
38{
39    b->v &= ~((uint64_t) 1 << n);
40}
41
42
43static inline int bitset_get(const struct bitset *b, int n)
44{
45    return !!(b->v & ((uint64_t) 1 << n));
46}
47
48
49static inline int bitset_empty(const struct bitset *b)
50{
51    return !b->v;
52}
53
54
55static inline int bitset_first(const struct bitset *b)
56{
57    int i;
58
59    for (i = 0; i != sizeof(b->v)*8; i++)
60        if (b->v & ((uint64_t) 1 << i))
61            return i;
62    return -1;
63}
64
65
66static inline int bitset_last(const struct bitset *b)
67{
68    int i = sizeof(b->v)*8;
69
70    while (--i)
71        if (b->v & ((uint64_t) 1 << i))
72            break;
73    return i;
74}
75
76
77static inline int bitset_common(const struct bitset *a, const struct bitset *b)
78{
79    return (a->v & b->v) != 0;
80}
81
82#endif /* !BITSET_H */
83

Archive Download this file

Branches:
master



interactive