-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutility.cpp
59 lines (54 loc) · 1.3 KB
/
utility.cpp
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
//-----------------------------------------------------
// Copyright 2013 Ontario Institute for Cancer Research
// Written by Jared Simpson (jared.simpson@gmail.com)
// Released under the GPL
//-----------------------------------------------------
//
// utility - small utility functions that don't fit
// cleanly elsewhere
//
#include <assert.h>
#include "utility.h"
//
int calculateShiftValue(int divisor)
{
assert(divisor > 0);
assert(IS_POWER_OF_2(divisor));
// m_sampleRate is a power of 2, count what bit is set
unsigned int v = divisor;
unsigned int c = 0; // c accumulates the total bits set in v
while(v != 1)
{
v >>= 1;
++c;
}
assert(1 << c == divisor);
return c;
}
//
std::string int2Binary(size_t v, int numBits)
{
std::string tmp;
int bits = sizeof(v) * 8;
for(int i = bits - 1; i >= 0; --i)
{
// test if the i-th bit is set
size_t mask = 1;
mask <<= i;
char b = (v & mask) ? '1' : '0';
tmp.append(1, b);
}
size_t pos = 0;
if(numBits == 0)
{
// Truncate leading zeros
size_t pos = tmp.find_first_of('1');
if(pos == std::string::npos)
pos = tmp.size() - 1;
}
else
{
pos = tmp.size() - numBits;
}
return tmp.substr(pos);
}