用于解码无符号短值的函数
Function for decoding unsigned short value
我对某些任务有小问题。
我们对此主题进行了调查。单个调查的结果(从一个受访者获得)提供了以下信息,这些信息将被编码为无符号短变量(可以假设它是 2 个字节 - 16 位)
- 性别 - 1 位 - 2 种可能性
- 婚姻状况 - 2 位 - 4 种可能性
- 年龄 - 2 位 - 4 种可能性
- 教育 - 2 位 - 4 种可能性
- 城市 - 2 位 - 4 种可能性
- 区域 - 4 位 - 16 种可能性
-
答案 - 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; }
在这里,它对结果进行编码(希望它是正确的),但我不知道如何准备将显示信息的函数,我已经在无符号的短 x 中编码了这些信息。
首先,我必须对其进行编码:
unsigned short x = coding(0, 3, 2, 3, 0, 12, 6);
然后我需要准备另一个函数,它将无符号短 X 中的信息解码为这种形式:
info(x);
RESULT
sex: 0
martial status: 3
age: 2
education: 3
city: 0
region: 12
reply: 6
我将感谢您的帮助,因为我甚至不知道如何开始以及寻找什么。
我的问题是是否有人可以检查无符号的短编码功能并帮助编写无效信息(无符号短 x)。
您可以使用位字段
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;
};
如果需要在短之间转换它,可以像这样定义一个联合
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 ++标准。
解决方案很简单,主要是文本工作。传输您的数据描述
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
};
这是位集的标准用例,甚至是传统的 C,但也可用于每个符合标准的C++实现。
让我们为存储store_t
命名 16 位编码数据类型(从使用的几个定义中,我们使用 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;
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 为什么我的从base64解码的函数不起作用?
- 是否有函数可以将 base64 字符串解码为张量流 c++ 中的张量
- 给定哈希函数解码消息的最佳方法是什么
- 霍夫曼解码中的递归函数不退出
- 用于解码无符号短值的函数
- 当用户自定义哈希函数在 unordered_map 中时,无法解码 g++ 中模糊的编译器错误
- 解码函数的输出很奇怪
- BitStreaming解码函数返回差值(指针问题?)
- 我如何在java中使用条件运算符编写解码函数