forked from foxsi/foxsigse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsefulFunctions.cpp
More file actions
executable file
·59 lines (46 loc) · 1.26 KB
/
UsefulFunctions.cpp
File metadata and controls
executable file
·59 lines (46 loc) · 1.26 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
56
57
58
59
/*
* UsefulFunctions.cpp
* Untitled
*
* Created by Steven Christe on 7/22/09.
* Copyright 2009 FOXSI. All rights reserved.
*
*/
#include "UsefulFunctions.h"
string convertBase(unsigned long v, long base)
{
string digits = "0123456789abcdef";
string result;
if((base < 2) || (base > 32)) {
result = "Error: base out of range.";
}
else {
do {
result = digits[v % base] + result;
v /= base;
}
while(v);
}
return result;
}
unsigned getbits(unsigned x, int p, int n)
{
// This function extracts n bits, starting at position p, from integer x.
// The most common use is n=1 to extract a single bit at position p.
// p=0 refers to the rightmost (LSB) bit.
// The full description is at http://www.java-samples.com/showtutorial.php?tutorialid=500
return (x >> (p+1-n)) & ~(~0 << n);
}
unsigned reversebits(unsigned x, int n)
{
// This function reverses the bit order of an integer and returns it. Only the n number of LSB are included;
// all other bits are zero.
// example for 5 bits; the code does:
// getbits(x,4,1)*1 + getbits(x,3,1)*2 + getbits(x,2,1)*4 + getbits(x,1,1)*8 + getbits(x,0,1)*16
unsigned y = 0;
for(int i=0; i<n; i++){
y = y + getbits(x, n-i-1, 1)*pow(2,i);
// cout << y << '\t'; // debug
}
return y;
}