1

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).

Daniel Zawadzki
  • 179
  • 1
  • 7
  • what is exactly the question ? do you need a decode function for that unsigned short ? – Raxvan Nov 14 '13 at 11:01
  • @Raxvan it would be great, becouse i have much much more tasks like that, but dont have any example to work with. Because of the work I had to leave this topic in university and now I do not have any materials. This is first task and i need help only to understand it. – Daniel Zawadzki Nov 14 '13 at 11:09
  • 2
    Assuming you haven't been specifically asked to use the shift operators, you could make this much simpler for yourself if you used a union with a bitfield (look that up and you should find plenty of example) – benjymous Nov 14 '13 at 11:10
  • Your shift values are off. How did you calculate them? – n. m. could be an AI Nov 14 '13 at 11:12
  • Right, so your shift values don't match the number of bits you have in the list of "values to encode". And you still haven't really put a question into your "Question". – Mats Petersson Nov 14 '13 at 11:20
  • @benjymous in next part of task its specify that encoding must be made by unsigned short coding(int sex, int marital_status, int age, int edu, int city, int region, int reply), and decoding by void info(unsigned short x) – Daniel Zawadzki Nov 14 '13 at 11:24
  • @n.m to be honest im basing on informations from google and i dont know why, but i have really big problem to understand it. Thats why im asking for help :(. – Daniel Zawadzki Nov 14 '13 at 11:26
  • @DanielZawadzki You can still do that using a union with a bitfield (assuming your assignment doesn't specifically mention that you must use shifts) – benjymous Nov 14 '13 at 11:26
  • 2
    You have posted this on the wrong site - you want "www.we-write-your-homework-for-you.com" – Mats Petersson Nov 14 '13 at 11:29
  • @benjymous okay, after i done work i will try with it :) thanks for idea what to look for – Daniel Zawadzki Nov 14 '13 at 11:32

2 Answers2

5

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.

leixure
  • 66
  • 1
  • Thanks a lot! :) tonight im gonna work with this task, basing on these informations – Daniel Zawadzki Nov 14 '13 at 11:40
  • I didn't write an answer like this as I figured it was better to push the OP in the right direction without doing the work for them. Still upvoted, though :) – benjymous Nov 14 '13 at 11:46
  • @benjymous yes, but this example is worth the work, because (I hope) it is derived from a real-world problem. – Wolf Nov 14 '13 at 11:50
1

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;
Community
  • 1
  • 1
Wolf
  • 9,679
  • 7
  • 62
  • 108