作者gn00618777 (非常念旧)
看板C_and_CPP
标题[问题] struct bit field
时间Thu May 19 23:33:38 2016
不好意思请问一下...我在网路上范例都是直接说出 struct 大小为 4 byte
struct {
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status;
请问有比较详细的算法吗?
我的想法是
总共两个 bit ,但一次存取是 4 byte ,所以後面 padding 30 bit
求出大小为 4 byte
不知道我这样想对不对....
感谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 58.115.110.72
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1463672022.A.2F5.html
1F:→ Caesar08: 因为C++规定,sizeof不能是0 05/19 23:48
2F:→ Caesar08: 既然不能是0,那最少也得是1 05/19 23:49
3F:→ Caesar08: 阿,对不起,我看错题目了... 05/19 23:55
4F:→ bibo9901: 我以为这意思是: 只使用unsigned int 的 2 个bit ? 05/19 23:55
5F:推 dijkstra: 同上 05/20 01:03
6F:→ gn00618777: = = 那既然只用了 2 个 bit ,她怎出现 4 byte 呢? 05/20 05:32
8F:→ gn00618777: 那这样我的想法应该是对的! 05/20 05:55
9F:→ Caesar08: 另外,我不是很懂为什麽需要知道bit field的大小 05/20 10:37
10F:→ Caesar08: bit field的实际大小是implementation-defined 05/20 10:42
11F:→ james1022jk: 他应该是想要知道bit field怎麽影响struct的大小 05/20 11:19
13F:→ gn00618777: 一句有解答到我的问题,那这样一切都了了~! 05/20 18:33
14F:→ bibo9901: 所谓 implementation-defined 就是不同编译器可能有不同 05/21 00:43
15F:→ bibo9901: 作法 05/21 00:43
16F:→ Caesar08: 他好像不是很懂甚麽是implementation-defined... 05/21 09:58