联盟和结构布局问题 [英] Union and struct packing problem

查看:81
本文介绍了联盟和结构布局问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一些软件,其中每一位必须是准确的(这是为CPU),所以__packed是非常重要的。

I'm writing some software where each bit must be exact(it's for the CPU) so __packed is very important.

typedef union{
uint32_t raw;
struct{
	unsigned int present:1;
	unsigned int rw:1;
	unsigned int user:1;
	unsigned int dirty:1;
	unsigned int free:7;
	unsigned int frame:20;
} __packed;
}__packed page_union_t;

这是我的结构和联合。但是这不工作:

that is my structure and union. It does not work however:

page_union_t p; //.....
//This:
p.frame=trg_page;
p.user=user;
p.rw=rw;
p.present=present;
//and this:
p.raw=trg_page<<12 | user<<2 | rw<<1 | present;

应该创建相同UINT32。但他们并没有创造同样的事情。

should create the same uint32. But they do not create the same thing.

有什么我看不出是错我的工会?

Is there something I can not see that is wrong with my union?

推荐答案

您结构只有31位。

这篇关于联盟和结构布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆