I'm trying run my program which is a bit packing function. However I'm experience multiple errors and looking for some insight. Thanks.
Code:
# include "defs.h"
# define getmask(lastbit, firstbit) ((0xffffffff<<(firstbit)) & (0xffffffff>>(32-(lastbit)-1)))
# include <math.h>
# include <stdlib.h>
/*
* Pack values into bit fields.
*
* Parameters:
* s - The bit field widths.
* un - The unpacked values.
*
* Returns - packed values.
*/
packed pack(sizes s, unpacked un) {
int i = 0;
int maxWidth = 0;
int j = 0;
int shift;
packed p;
for (i = 0; i < s.numWidths; i += 1){
maxWidth += s.fieldWidths[i];
p.n = ceil(maxWidth / 32.0);
p.fieldValues = (int*)malloc(sizeof(int)*p.n);
for (i = 0; i < p.n; i += 1){
p.fieldValues[i] = 0;
}
}
shift = 32;
for (i = 0; i < s.numWidths; i += 1){
shift -= s.fieldWidths[i];
if (shift < 0){
int highBits = s.fieldWidths[i] + shift;
int bitPack1 = un.values[i] & getmask(s.fieldWidths[i] - 1, s.fieldWidths[i] - highBits);
int bitPack2 = un.values[i] & getmask(s.fieldWidths[i] - highBits - 1, 0);
p.fieldValues[j += 1] |= bitPack1;
shift += 32;
p.fieldValues[j] |= (un.values[i] & getmask(s.fieldWidths[i] - 1, 0)) << shift;
continue;
}
p.fieldValues[j] |= (un.values[i] & getmask(s.fieldWidths[i] - 1, 0)) << shift;
}
return p;
} // end of pack function
# include <math.h>
# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
# include "defs.h"
/*
* Test driver for pack.
*/
int main() {
// get the field widths
printf("Enter bit field widths: ");
sizes sizeData;
sizeData.numWidths = 0;
while (true) {
int value;
// scanf("%d", &value);
if (value <= 0) break;
while (value > 31) {
printf("bit field width must be less than 32, try again: ");
scanf("%d", &value);
if (value == 0) break;
}
sizeData.numWidths += 1;
if (sizeData.numWidths == 1) {
sizeData.fieldWidths = (int *)malloc(sizeof(int));
}
else {
sizeData.fieldWidths =
(int *)realloc(sizeData.fieldWidths, sizeData.numWidths*sizeof(int));
}
sizeData.fieldWidths[sizeData.numWidths - 1] = value;
}
if (sizeData.numWidths == 0) {
printf("you must enter at least one field width\n");
return 1;
}
// get the field values
unpacked input;
printf("Enter %d field values: ", sizeData.numWidths);
input.values = (int *)malloc(sizeData.numWidths*sizeof(int));
for (int i = 0; i<sizeData.numWidths; i += 1) {
scanf("%d", &(input.values[i]));
while (input.values[i] < 0 || input.values[i] >= pow(2.0, sizeData.fieldWidths[i])) {
if (input.values[i] < 0) {
printf("negative values are not allowed, try again: ");
}
else {
printf("the value is too big to fit in the field, try again: ");
}
scanf("%d", &(input.values[i]));
}
}
// call the pack function to pack the values into ints
packed p = pack(sizeData, input);
// print the resulting packed array of ints
for (int i = 0; i<p.n; i += 1) {
printf("0x%08x ", p.fieldValues[i]);
}
printf("\n");
} // end of main function
Here is the file including all the structs i'm using as well.
Code:
#ifndef DEFS_H
#define DEFS_H
// a structure that contains field widths
typedef struct {
int * fieldWidths; // a pointer to an array of bit field widths (in bits)
int numWidths; // the number of elements in the array (i.e., the number of bit fields)
} sizes;
// a structure that contains an array of ints containing packed data fields
typedef struct {
int * fieldValues; // a pointer to an array of ints containing packed bit fields
int n; // the number of elements in the array
} packed;
// a structure that contains an array of ints containing individual data values (one per int)
typedef struct {
int * values; // a pointer to an array of ints containing values for bit fields (one per element)
int n; // the number of elements in the array
} unpacked;
packed pack(sizes s, unpacked un);
unpacked unpack(sizes s, packed p);
#endif
thanks