比特申请使用 [英] Bit filed usage

查看:81
本文介绍了比特申请使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi NG,


我对结构中bitfileds的使用有疑问。

例如:


struct pcard

{

unsigned pips:4;

未签名套装:2;

} ;


我看到使用这个的唯一原因是为了节省内存。但这不是真的

在大多数系统中,内存包装数据不是那么多问题,比如

这个?甚至在某些嵌入式机器上还有很多内存,我不确定

包装你的结构所带来的内存增益将远远大于你从中获得的损失

代码大小增加。也许当你有很多这些

结构时你会得到一些东西。


我认为这个有用的另一个原因就是结构

将被写入某个二进制文件。在我看来,文件大小确实很重要。

我在这种情况下看到的唯一问题是每台机器,甚至是同一台机器上的不同编译器,都可以映射

a字的其他地方。这意味着该文件与除了

相同的编译器和同一台机器之外的任何东西都不兼容。


所以我很好奇是否有人使用这些结构和

用于它们。


Mark

解决方案

" Capstar" < spam0 @@@ eg.homeip.net>在消息中写道

news:bg ********** @ news.tudelft.nl ...

嗨NG,
例如:

struct pcard
{
无符号点数:4;
未签名套装:2;
};

我看到使用这个的唯一原因是节省内存。但是,对于大多数系统来说,打包数据
这样的内存不是很大的问题吗?即使在某些嵌入式计算机上输出大量内存,我也不会确保通过打包您的结构获得的内存增益远远大于
您从增加的代码大小中获得的损失。也许当你有很多
这些结构时,你会获得一些东西。

我认为这有用的另一个原因是如果将
结构写入某个二进制文件。在我看来,文件大小确实是
。我在这种情况下看到的唯一问题是每台机器,甚至是同一台机器上的不同编译器,都可以在一个单词中映射
其他地方的位。这意味着该文件与任何
不同,但是相同的编译器和同一台机器。

所以我很好奇是否有人使用过这种结构并且
在哪里它们用于。

Mark




我怀疑这是偏离主题的,但我们使用位域结构时

映射RFID标签的数据,其中内存通常以位为单位测量,

不是字节,更不用说了。


-

Simon Wilkins
www.robotag.co.uk


2003年8月8日星期五13:01:20 +0200

Capstar < spam0 @@@ eg.homeip.net>写道:

struct pcard
{
unsigned pips:4;
未签名套装:2;
};

只有我看到使用这个的原因是为了节省内存。但这不是真的吗?在大多数系统上,内存包装数据不是那么多问题吗?甚至在一些嵌入式机器上还有很多内存,我不确定打包你的结构所带来的内存增益会比你从增加的代码大小中获得的损失大得多。也许当你有很多这些结构时,你会获得一些东西。




使用这些的原因是为了解决那些烦人的按位操作。

他们可能是一个真正的痛苦,即使宏观操作,他们也不会b $ b b提高可读性。

Bitfields将这些操作留给编译器。


并且内存保护不是问题,因为结构是对齐的,无论如何都是
。你给出的例子不会占用6位,它会对齐并占用

a字节,或者一个字或一个int,具体取决于你的平台。


只使用char的结构只使用稍多的内存,但是除掉了所有操作的
。一切都取决于你想要什么,更多的记忆或更多的周期。


-

char * x(c,k,s)char * k,* s; {if(!k)return * s-36?x(0,0,s + 1):s; if(s)if(* s)c = 10 +(c?(x(

c,k,0),x(c,k + = * sc,s + 1),* k):( x(* s,k,s + 1),0));否则c = 10; printf(& x(~0,0,k)[c-〜 -

c +" 1" [〜c< -c]],c);} main(){x (0," ^ [kXc6] dn_eaoh


%C"," -34 * 1 '' +(,03#; +,)/ '' /// * ");}


Hi NG,

I have a question about the usage of bitfileds in a structure.
for instance:

struct pcard
{
unsigned pips : 4;
unsigned suit : 2;
};

The only reason I see fot using this is to save memory. But isn''t it true
that on most systems memory isn''t that much of an issue to pack data like
this? And even on some embedded machine whitout much memory, I''m not sure
the memory gain by packing your structures will be much bigger than the loss
you get from the increased code size. Maybe when you have lots of these
structures you will gain something.

Another reason I would consider this helpfull would be if the structures
would be written to some binary file. In my opinion file size does matter.
The only problem I see in this case is that every machine, and even a
different compiler at the same machine, could map the bits somewhere else in
a word. This would mean that the file is incompatible with anything but the
same compiler and the same machine.

So I am very curious if anybody ever uses these kind of structures and where
they are used for.

Mark

解决方案

"Capstar" <spam0@@@eg.homeip.net> wrote in message
news:bg**********@news.tudelft.nl...

Hi NG,

I have a question about the usage of bitfileds in a structure.
for instance:

struct pcard
{
unsigned pips : 4;
unsigned suit : 2;
};

The only reason I see fot using this is to save memory. But isn''t it true that on most systems memory isn''t that much of an issue to pack data like this? And even on some embedded machine whitout much memory, I''m not sure the memory gain by packing your structures will be much bigger than the loss you get from the increased code size. Maybe when you have lots of these structures you will gain something.

Another reason I would consider this helpfull would be if the structures would be written to some binary file. In my opinion file size does matter. The only problem I see in this case is that every machine, and even a
different compiler at the same machine, could map the bits somewhere else in a word. This would mean that the file is incompatible with anything but the same compiler and the same machine.

So I am very curious if anybody ever uses these kind of structures and where they are used for.

Mark



I suspect this is off-topic, but we use bit-field structures when
mapping data for RFID tags where the memory is usually measured in bits,
not bytes let alone kB.

--
Simon Wilkins
www.robotag.co.uk


On Fri, 8 Aug 2003 13:01:20 +0200
"Capstar" <spam0@@@eg.homeip.net> wrote:

struct pcard
{
unsigned pips : 4;
unsigned suit : 2;
};

The only reason I see fot using this is to save memory. But isn''t it true
that on most systems memory isn''t that much of an issue to pack data like
this? And even on some embedded machine whitout much memory, I''m not sure
the memory gain by packing your structures will be much bigger than the loss
you get from the increased code size. Maybe when you have lots of these
structures you will gain something.



A reason for using these is to get around those annoying bitwise operations.
They can be a real pain, and even when macro-ifying the operations, they don''t
improve readability.
Bitfields leave those operations to the compiler.

And the memory conservation is not an issue, since the structs are aligned
anyway. The example you gave won''t take up just 6 bits, it''ll align and take up
a byte, or a word, or an int, depending on your platform.

Just using a struct of char''s uses only slightly more memory, but gets rid of
all the operations. All depends on what you want, more memory or more cycles.

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh


%c","-34*1''.+(,03#;+,)/''///*");}


这篇关于比特申请使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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