Thread: C Bit Pack Function

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    18

    C Bit Pack Function

    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

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    We aren't mind readers, so tell us what kind of errors you're experiencing.

    Compiler errors? Please copy-paste them exactly, with corresponding line numbers.

    Run-time errors? Does it crash? Show no output? Show garbled output? Show incorrect output? For all inputs or just some input? Please provide the input you gave that causes a problem, the (correct) output you expect to see, and the actual (incorrect) output you actually see.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're reusing the loop variable i here:
    Code:
        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;
            }
        }
    That's going to mess up your calculations for sure. Use a different variable for the inner loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using the pack function
    By tbrown81 in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:54 PM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. Replies: 1
    Last Post: 06-03-2005, 01:03 AM
  4. To service pack or not to service pack
    By Waldo2k2 in forum Tech Board
    Replies: 17
    Last Post: 01-08-2003, 05:35 AM
  5. Try out my new game Pack
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 06-18-2002, 11:06 PM

Tags for this Thread