-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitset.c
61 lines (48 loc) · 1.05 KB
/
bitset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* bitset.c
* Patater GUI Kit
*
* Created by Jaeden Amero on 2022-02-02.
* Copyright 2022. SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "bitset.h"
#include <stddef.h>
#include <strings.h>
void BitsetSet(unsigned char *bitset, size_t bit)
{
bitset[bit / 8] |= 1 << (bit % 8);
}
void BitsetReset(unsigned char *bitset, size_t bit)
{
bitset[bit / 8] &= ~(1 << (bit % 8));
}
size_t BitsetCountSetBits(const unsigned char *bitset, size_t len)
{
size_t i;
size_t count;
count = 0;
for (i = 0; i < len * 8; ++i)
{
count += !!BitsetTestBit(bitset, i);
}
return count;
}
size_t BitsetCountClearBits(const unsigned char *bitset, size_t len)
{
size_t i;
size_t count;
count = 0;
for (i = 0; i < len * 8; ++i)
{
count += !BitsetTestBit(bitset, i);
}
return count;
}
unsigned char BitsetTestBit(const unsigned char *bitset, size_t bit)
{
return bitset[bit / 8] & (1 << (bit % 8));
}
void BitsetClearBits(unsigned char *bitset, size_t len)
{
bzero(bitset, len);
}