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