Question

i have small problem with some task.

We conduct a survey on the subject. Result of a single survey (obtained from one respondent) provides the following information to be encoded in a variable of type unsigned short (it can be assumed that it is 2 bytes - 16 bits)

  1. sex - 1 bit - 2 possibilities
  2. marital status - 2 bits - 4 possibilities
  3. Age - 2 bits - 4 possibilities
  4. Education - 2 bits - 4 possibilities
  5. City - 2 bits - 4 possibilities
  6. region - 4 bits - 16 possibilities
  7. answer - 3 bits - 8 possibilities

     unsigned short coding(int sex, int marital_status, int age, int edu, int city, int region, int reply){
    
        unsigned short result = 0;
    
        result = result + sex;
        result = result + ( marital_status << 1 );
        result = result + ( age << 3);
        result = result + ( edu << 5 );
        result = result + ( city << 6 );
        result = result + ( region << 11 );
        result = result + ( reply << 13 );
    
        return result;
    }
    

Here it encodes the results (hope its correct), but I have no idea how to prepare function which will display informations, which i have encoded inside of unsigned short x.

First I have to encode it:

     unsigned short x = coding(0, 3, 2, 3, 0, 12, 6);

then i need to prepare another function, which will decode informations from unsigned short x into this form:

    info(x);


    RESULT
    sex:                 0
    martial status:      3
    age:                 2
    education:           3
    city:                0
    region:              12
    reply:               6

I will be grateful for your help, because I have no idea how to even get started and what to look for.

My question is if someone can check unsigned short coding function and help with with writing void info(unsigned short x).

Était-ce utile?

La solution

you can use bit fields

struct survey_data
{
    unsigned short sex            : 1;
    unsigned short marital_status : 2;
    unsigned short age            : 2;
    unsigned short education      : 2;
    unsigned short city           : 2;
    unsigned short region         : 4;
    unsigned short answer         : 3;
};

if you need to convert it between short, you can define a union like this

union survey
{
    struct survey_data detail;
    unsigned short s;
};

to use these types

struct survey_data sd;
sd.sex = 0;
sd.marital_status = 2;
...
unsigned short s = 0xCAFE;
union servey x;
x.s = s;
printf("Sex: %u, Age: %u", x.detail.sex, x.detail.age);

keep in mind the layout of bit fields is implementation defined; different compiler may interpret them in different order, e.g. in MSVC, it is lsb to msb; pelase refer to the compiler manual and c/c++ standard for details.

Autres conseils

The solution is straightforward, and it's mostly text work. Transfer your data description

sex - 1 bit - 2 possibilities
marital status - 2 bits - 4 possibilities
Age - 2 bits - 4 possibilities
Education - 2 bits - 4 possibilities
City - 2 bits - 4 possibilities
region - 4 bits - 16 possibilities
answer - 3 bits - 8 possibilities

into this C/C++ structure:

struct Data {
    unsigned sex:        1; //  2 possibilities
    unsigned marital:    2; //  4 possibilities
    unsigned Age:        2; //  4 possibilities
    unsigned Education:  2; //  4 possibilities
    unsigned City:       2; //  4 possibilities
    unsigned region:     4; // 16 possibilities
    unsigned answer:     3; //  8 possibilities
};

It's a standard use case for bit sets, which is even traditional C, but also available in each standard-conform C++ implementation.

Let's name your 16-bit encoded data type for storage store_t (from the several definitions in use we use the C standard header stdint.h):

#include <stdint.h>
typedef uint16_t store_t;

The example Data structure can be used for encoding:

/// create a compact storage type value from data
store_t encodeData(const Data& data) {
    return *reinterpret_cast<const store_t*>(&data);
}

or decoding your data set:

/// retrieve data from a compact storage type
const Data decodeData(const store_t code) {
    return *reinterpret_cast<const Data*>(&code);
}

you access the bitset structure Data like an ordinary structure:

Data data;
data.sex = 1;
data.marital = 0;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top