cgv
Loading...
Searching...
No Matches
bit_operations.cxx
1
#include "bit_operations.h"
2
3
namespace
cgv
{
4
namespace
utils {
5
7
bool
is_bit_set
(
unsigned
int
bit_idx,
unsigned
int
bit_field)
8
{
9
static
unsigned
int
bit_mask[32] = {
10
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
11
32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608,
12
16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648u };
13
14
return
(bit_field & bit_mask[bit_idx]) != 0;
15
}
17
void
enable_upper_bits
(
unsigned
int
& bit_field,
unsigned
int
fst_bit_idx)
18
{
19
static
unsigned
int
bit_mask[32] = {
20
4294967295u, 4294967294u, 4294967292u, 4294967288u, 4294967280u, 4294967264u, 4294967232u, 4294967168u, 4294967040u,
21
4294966784u, 4294966272u, 4294965248u, 4294963200u, 4294959104u, 4294950912u, 4294934528u, 4294901760u, 4294836224u,
22
4294705152u, 4294443008u, 4293918720u, 4292870144u, 4290772992u, 4286578688u, 4278190080u, 4261412864u, 4227858432u,
23
4160749568u, 4026531840u, 3758096384u, 3221225472u, 2147483648u };
24
25
bit_field |= bit_mask[fst_bit_idx];
26
}
27
29
void
disable_upper_bits
(
unsigned
int
& bit_field,
unsigned
int
fst_bit_idx)
30
{
31
static
unsigned
int
bit_mask[32] = {
32
0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143,
33
524287, 1048575, 2097151, 4194303, 8388607, 16777215, 33554431, 67108863, 134217727, 268435455, 536870911,
34
1073741823, 2147483647u };
35
36
bit_field &= bit_mask[fst_bit_idx];
37
}
38
40
void
enable_lower_bits
(
unsigned
int
& bit_field,
unsigned
int
fst_bit_idx)
41
{
42
static
unsigned
int
bit_mask[32] = {
43
1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143,
44
524287, 1048575, 2097151, 4194303, 8388607, 16777215, 33554431, 67108863, 134217727, 268435455, 536870911,
45
1073741823, 2147483647u, 4294967295u };
46
47
bit_field |= bit_mask[fst_bit_idx];
48
}
49
51
void
disable_lower_bits
(
unsigned
int
& bit_field,
unsigned
int
fst_bit_idx)
52
{
53
static
unsigned
int
bit_mask[32] = {
54
4294967294u, 4294967292u, 4294967288u, 4294967280u, 4294967264u, 4294967232u, 4294967168u, 4294967040u,
55
4294966784u, 4294966272u, 4294965248u, 4294963200u, 4294959104u, 4294950912u, 4294934528u, 4294901760u, 4294836224u,
56
4294705152u, 4294443008u, 4293918720u, 4292870144u, 4290772992u, 4286578688u, 4278190080u, 4261412864u, 4227858432u,
57
4160749568u, 4026531840u, 3758096384u, 3221225472u, 2147483648u, 0 };
58
59
bit_field &= bit_mask[fst_bit_idx];
60
}
61
62
64
void
set_bits
(
unsigned
int
& bit_field,
unsigned
int
off,
unsigned
int
n,
unsigned
int
value)
65
{
66
unsigned
int
tmp = bit_field >> off;
67
disable_lower_bits
(tmp, n-1);
68
disable_upper_bits
(value, n);
69
tmp += value;
70
tmp <<= off;
71
disable_upper_bits
(bit_field, off);
72
bit_field += tmp;
73
}
74
75
}
76
}
cgv::utils::enable_lower_bits
void enable_lower_bits(unsigned int &bit_field, unsigned int fst_bit_idx)
set all the bits of bit_field with index equal or less than fst_bit_idx
Definition
bit_operations.cxx:40
cgv::utils::is_bit_set
bool is_bit_set(unsigned int bit_idx, unsigned int bit_field)
check if a bit of a bit field is set
Definition
bit_operations.cxx:7
cgv::utils::disable_lower_bits
void disable_lower_bits(unsigned int &bit_field, unsigned int fst_bit_idx)
clear all the bits of bit_field with index equal or less than fst_bit_idx
Definition
bit_operations.cxx:51
cgv::utils::enable_upper_bits
void enable_upper_bits(unsigned int &bit_field, unsigned int fst_bit_idx)
set all the bits of bit_field with index equal or larger than fst_bit_idx
Definition
bit_operations.cxx:17
cgv::utils::disable_upper_bits
void disable_upper_bits(unsigned int &bit_field, unsigned int fst_bit_idx)
clear all the bits of bit_field with index equal or larger than fst_bit_idx
Definition
bit_operations.cxx:29
cgv::utils::set_bits
void set_bits(unsigned int &bit_field, unsigned int off, unsigned int n, unsigned int value)
set n bits starting with index off of the given bit field from the first bits of the given integer va...
Definition
bit_operations.cxx:64
cgv
the cgv namespace
Definition
print.h:11
cgv
utils
bit_operations.cxx
Generated by
1.9.8