【问题标题】:Function for decoding unsigned short value解码无符号短值的函数
【发布时间】:2013-11-14 10:52:08
【问题描述】:

我在某些任务上遇到了小问题。

我们对此主题进行了调查。单次调查的结果(从一名受访者那里获得)提供了以下信息,这些信息将被编码为 un​​signed short 类型的变量(可以假设它是 2 字节 - 16 位)

  1. 性别 - 1 位 - 2 种可能性
  2. 婚姻状况 - 2 位 - 4 种可能性
  3. 年龄 - 2 位 - 4 种可能性
  4. 教育 - 2 位 - 4 种可能性
  5. 城市 - 2 位 - 4 种可能性
  6. 区域 - 4 位 - 16 种可能性
  7. 答案 - 3 位 - 8 种可能性

     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;
    }
    

在这里它对结果进行编码(希望它是正确的),但我不知道如何准备将显示信息的函数,我已经在 unsigned short x 中编码了这些信息。

首先我必须对其进行编码:

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

然后我需要准备另一个函数,它将unsigned short x中的信息解码为这种形式:

    info(x);


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

感谢您的帮助,因为我什至不知道如何开始以及寻找什么。

我的问题是,是否有人可以检查 unsigned short 编码函数并帮助编写 void info(unsigned short x)。

【问题讨论】:

  • 到底是什么问题?你需要那个 unsigned short 的解码函数吗?
  • @Raxvan 会很棒,因为我有更多这样的任务,但没有任何示例可以使用。因为工作的关系,我不得不把这个话题留在大学里,现在我没有任何材料。这是第一项任务,我需要帮助才能理解它。
  • 假设您没有被特别要求使用移位运算符,如果您使用带位域的联合,您可以让自己更简单(查看它,您应该会找到很多示例)
  • 您的班次值已关闭。你是怎么计算出来的?
  • 你在错误的网站上发布了这个 - 你想要“www.we-write-your-homework-for-you.com”

标签: c++ c data-structures decode short


【解决方案1】:

你可以使用位域

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;
};

如果需要在short之间进行转换,可以这样定义一个union

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

使用这些类型

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

记住位域的布局是由实现定义的;不同的编译器可能会以不同的顺序解释它们,例如在 MSVC 中,它是 lsb 到 msb;详情请参考编译器手册和c/c++标准。

【讨论】:

  • 非常感谢! :) 今晚我将根据这些信息完成这项任务
  • 我没有写这样的答案,因为我认为最好在不为他们工作的情况下将 OP 推向正确的方向。不过还是赞成的:)
  • @benjymous 是的,但是这个例子是值得的,因为(我希望)它来自一个现实世界的问题。
【解决方案2】:

解决方案很简单,主要是文字工作。传输您的数据描述

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

进入这个 C/C++ 结构:

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
};

这是 bit sets 的标准用例,它甚至是传统的 C,但也可用于每个符合标准的 C++ 实现。

让我们将用于存储的 16 位编码数据类型命名为 store_t(来自 several definitions in use,我们使用 C 标准标头 stdint.h):

#include <stdint.h>
typedef uint16_t store_t;

Data 结构示例可用于编码

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

解码您的数据集:

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

您像访问普通结构一样访问位集结构Data

Data data;
data.sex = 1;
data.marital = 0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多