-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUsefulFunctions.cpp
executable file
·111 lines (94 loc) · 2.17 KB
/
UsefulFunctions.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* 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;
}
unsigned int median(unsigned int *a, int n)
{
// find the median of an array of size n
//
//
float temp;
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
if(n%2==0)
return (a[n/2]+a[n/2-1])/2;
else
return a[n/2];
}
unsigned long maximumValue(unsigned long *array, int n, int min_index)
{
// find the maximum value in an array of size n
// ignore elements below min_index
long max = array[min_index]; // start with max = first element
for(int i = min_index+1; i < n; i++)
{
if(array[i] > max){
max = array[i];}
}
return max;
}
void get_version(char *text){
FILE *pfile;
pfile = fopen("/Users/foxsi/Desktop/FOXSI-GSE-latest/version.txt", "r");
if (pfile == NULL) {
printf("Error opening file");
} else {
if (fgets(text, 100, pfile) != NULL) {
puts(text);
printf("%s", text);
}
fclose(pfile);
}
}