令人费解的结构体定义??
最近项目组需要用MPLAB编译C程序,在例程的头文件里有如下的结构体定义,标准C里没有描述,恐怕是编译器支持的结构模板,尤其unsigned SID3:1;不知该怎么解释,让人费解
extern volatile far unsigned char RXF0SIDH;
extern volatile far struct {
unsigned SID3:1;
unsigned SID4:1;
unsigned SID5:1;
unsigned SID6:1;
unsigned SID7:1;
unsigned SID8:1;
unsigned SID9:1;
unsigned SID10:1;
} RXF0SIDHbits;
问题点数:200、回复次数:11Top
1 楼whyglinux(山青水秀)回复于 2005-05-31 15:01:56 得分 40
bit-field 位域,一种节省存储空间的结构Top
2 楼hofer_song(逯爻(虫))回复于 2005-05-31 15:10:37 得分 0
多谢多谢,俺孤陋寡闻了,汗Top
3 楼hofer_song(逯爻(虫))回复于 2005-05-31 15:13:08 得分 0
请问青山,volatile关键字怎么解释Top
4 楼huwei001982(凶猛的小狗)回复于 2005-05-31 15:21:26 得分 40
volatile
易变的
http://www.mcu99.com/Article/mcu/200410/63.html
Top
5 楼hofer_song(逯爻(虫))回复于 2005-05-31 15:23:37 得分 0
OK
结贴Top
6 楼jingyueid(干宁)回复于 2005-05-31 15:25:56 得分 60
extern volatile far unsigned char RXF0SIDH;
extern volatile far struct {
unsigned SID3:1;
unsigned SID4:1;
unsigned SID5:1;
unsigned SID6:1;
unsigned SID7:1;
unsigned SID8:1;
unsigned SID9:1;
unsigned SID10:1;
} RXF0SIDHbits;
unsigned SID10:1; :是位域,表示只使用1个位。会导致性能上的下降,通常在网络传输中会用到这个。
volatile可变的,操作系统不会进行优化处理,不会优化进积存器。
例如该变量可被其他进程修改,如果被优化进积存器,那么很可能值是错误的。声明以后,该变量只能从内存中位置读取。Top
7 楼zhousqy(标准C匪徒)(甩拉,甩拉)回复于 2005-05-31 15:29:05 得分 0
位域Top
8 楼zhousqy(标准C匪徒)(甩拉,甩拉)回复于 2005-05-31 15:30:19 得分 0
volatile 是为了防止编译器的优化。Top
9 楼zhousqy(标准C匪徒)(甩拉,甩拉)回复于 2005-05-31 15:32:08 得分 30
6.9 Bit-fields
When storage space is at a premium, it may be necessary to pack several objects into a single machine word; one common use is a set of single-bit flags in applications like compiler symbol tables. Externally-imposed data formats, such as interfaces to hardware devices, also often require the ability to get at pieces of a word.
Imagine a fragment of a compiler that manipulates a symbol table. Each identifier in a program has certain information associated with it, for example, whether or not it is a keyword, whether or not it is external and/or static, and so on. The most compact way to encode such information is a set of one-bit flags in a single char or int.
The usual way this is done is to define a set of ``masks'' corresponding to the relevant bit positions, as in
#define KEYWORD 01
#define EXTRENAL 02
#define STATIC 04
or
enum { KEYWORD = 01, EXTERNAL = 02, STATIC = 04 };
The numbers must be powers of two. Then accessing the bits becomes a matter of ``bit-fiddling'' with the shifting, masking, and complementing operators that were described in Chapter 2.Top
10 楼hblinlin(木木)回复于 2005-05-31 16:12:52 得分 10
学习啊。。。Top
11 楼ericzhangali(另一个空间)回复于 2005-05-31 16:18:19 得分 20
蹭分了,俺也碰到过,对空间极其抠门的时候。Top




