-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicBitUtilty.h
More file actions
55 lines (41 loc) · 1.44 KB
/
DynamicBitUtilty.h
File metadata and controls
55 lines (41 loc) · 1.44 KB
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
#pragma once
#include <cstdint>
/** *********DynamicBitUtilty *********
* This is a collection of functions usefull for operating on dynamicly sized bit fields
* Currently only supports bit fields backed by uint32_t but features planes exists for
* similar functions for uint64_t to be added.
*/
namespace Common
{
namespace DynamicBitUtility
{
// Sets the bit at index 'bit' in the bitfield 'bits' to value
inline void setBit( uint32_t *bits, unsigned bit, uint32_t value )
{
unsigned elem = bit / 32;
bit = bit % 32;
value &= 1;
uint32_t mask = ~(1 << bit);
bits[elem] = (bits[elem] & mask) | ((value&1) << bit);
}
// Returns the bit at index 'bit' in the bitfield 'bits'
inline uint32_t getBit( const uint32_t *bits, unsigned bit )
{
unsigned elem = bit / 32;
bit = bit % 32;
uint32_t mask = 1<< bit;
return !!(bits[elem] & mask);
}
// Sets the bit, and returns the old value
inline uint32_t swapBit( uint32_t *bits, unsigned bit, uint32_t value )
{
unsigned elem = bit / 32;
bit = bit % 32;
value &= 1;
uint32_t mask = ~(1 << bit);
uint32_t tmp = bits[elem];
bits[elem]= (tmp & mask) | ((value&1) << bit);
return !!(tmp & ~mask);
}
}
}