位字段排序 [英] bit fields ordering
问题描述
如果我有以下结构:
typedef union BYTE {
uint8_t bite;
struct {
uint8_t b0:1;
uint8_t b1:1;
uint8_t b2:1;
uint8_t b3:1;
uint8_t b4:1;
uint8_t b5:1;
uint8_t b6:1;
uint8_t b7:1 ;
}位;
} BYTE;
在我的盒子上,使用gcc我将msb存储在b7中,全部这个方法虽然是存储在b0中的lsb。但是,上面的代码是多么便携 - 我可以假设
这些字段总是以这种方式订购吗?
If I have the following structure:
typedef union BYTE {
uint8_t bite;
struct {
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
} bits;
} BYTE;
On my box, with gcc I get the msb stored in b7, all the way though to
the lsb stored in b0. BUT, how portable is the above code - can I assume
that the fields will always be ordered in such a way?
推荐答案
在文章< sl ********************* @ HAL.honeypot>,
Jonathan Maddison< jmaddi gmail> ;写道:
In article <sl*********************@HAL.honeypot>,
Jonathan Maddison <jmaddi gmail > wrote:
如果我有以下结构:
typedef union BYTE {
uint8_t bite;
struct {
uint8_t b0:1;
uint8_t b1:1;
uint8_t b2:1;
uint8_t b3:1;
uint8_t b4:1;
uint8_t b5:1;
uint8_t b6:1 ;
uint8_t b7:1;
}位;
} BYTE;
在我的盒子里,用gcc我把msb存储在b7中,一直到存储在b0中的lsb。但是,上面的代码有多便携 - 我可以假设这些字段总是以这种方式排序吗?
If I have the following structure: typedef union BYTE {
uint8_t bite;
struct {
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
} bits;
} BYTE; On my box, with gcc I get the msb stored in b7, all the way though to
the lsb stored in b0. BUT, how portable is the above code - can I assume
that the fields will always be ordered in such a way?
否,位的顺序标准中未定义char中的内容。
此外,uint8_t仅存在于可以支持它的框上。
如果CHAR_BIT是(比方说)9,那么uint8_t在
实现中不存在。
-
如果你喜欢VT-52那就太费了。
No, the order of bits within a char is undefined by the standard.
Besides, uint8_t only exists on boxes that can support it.
If CHAR_BIT is (say) 9, then uint8_t doesn''t exist in the
implementation.
--
Feep if you love VT-52''s.
ro******@ibd.nrc- cnrc.gc.ca (Walter Roberson)写道:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
在文章< sl ********************* @ HAL.honeypot>,
Jonathan Maddison< jmaddi gmail>写道:
In article <sl*********************@HAL.honeypot>,
Jonathan Maddison <jmaddi gmail > wrote:
如果我有以下结构:
typedef union BYTE {
uint8_t bite;
struct {
uint8_t b0:1;
uint8_t b1:1;
uint8_t b2:1;
uint8_t b3:1;
uint8_t b4:1;
uint8_t b5:1;
uint8_t b6:1;
uint8_t b7:1;
}位;
} BYTE;
typedef union BYTE {
uint8_t bite;
struct {
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
} bits;
} BYTE;
在我的盒子上,使用gcc我将msb存储在b7中,一直到存储在b0中的lsb。但是,上面的代码是多么可移植 - 我可以假设这些字段总是以这种方式排序吗?
On my box, with gcc I get the msb stored in b7, all the way though to
the lsb stored in b0. BUT, how portable is the above code - can I assume
that the fields will always be ordered in such a way?
不,char中的位顺序是标准未定义。
此外,uint8_t仅存在于可以支持它的盒子上。
如果CHAR_BIT是(比如说)9,那么uint8_t在
中不存在实现。
No, the order of bits within a char is undefined by the standard.
Besides, uint8_t only exists on boxes that can support it.
If CHAR_BIT is (say) 9, then uint8_t doesn''t exist in the
implementation.
位字段唯一允许的类型是int,signed int,
unsigned int和_Bool(或其他一些实现) -defined类型)。
-
Keith Thompson(The_Other_Keith) ks ***@mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
And the only allowed types for bit fields are int, signed int,
unsigned int, and _Bool (or some other implementation-defined type).
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jonathan Maddison在2005年8月27日写道:
Jonathan Maddison wrote on 27/08/05 :
typedef union BYTE {
uint8_t bite ;
你的意思是字节吗?
struct {
uint8_t b0:1;
uint8_t b1:1;
uint8_t b2:1;
uint8_t b3:1;
uint8_t b4:1;
uint8_t b5:1;
uint8_t b6:1;
uint8_t b7:1;
}位;
} BYTE;
在我的盒子里,用gcc我把msb存储在b7中,一直到存储在b0中的lsb。但是,上面的代码有多便携 - 我可以假设这些字段总是以这种方式订购吗?
typedef union BYTE {
uint8_t bite;
Do you meant byte ?
struct {
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
} bits;
} BYTE; On my box, with gcc I get the msb stored in b7, all the way though to
the lsb stored in b0. BUT, how portable is the above code - can I assume
that the fields will always be ordered in such a way?
你不能。位域不可移植。对于便携式接口,请使用
无符号整数和按位运算符。
这可以帮助
http://mapage.noos.fr/emdel/clib/ed/inc/bits。 h
-
Emmanuel
C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
C库: http:// www .dinkumware.com / refxc.html
C是一个尖锐的工具
You can''t. Bitfields are not portable. For portable interfaces, use
unsigned integers and bitwise operators.
This can help
http://mapage.noos.fr/emdel/clib/ed/inc/bits.h
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"C is a sharp tool"
这篇关于位字段排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!