位域数组 [英] Array of bitfields

查看:463
本文介绍了位域数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道为什么不可能使用位域数组。

这与处理器架构有什么关系?

感谢您的时间。


问候,

Shailendra

Hi,

I would like to know why array of bitfields is not possible.
Is there any relation with processor architecture for this?
Thank you for your time.

Regards,
Shailendra

推荐答案




Mehta Shailendrakumar写道:


Mehta Shailendrakumar wrote:


我想知道为什么bitfields数组是不可能。
与处理器架构有什么关系吗?
感谢您的时间。

问候,
Shailendra
Hi,

I would like to know why array of bitfields is not possible.
Is there any relation with processor architecture for this?
Thank you for your time.

Regards,
Shailendra




如果该位是您实现的可寻址单元,

您可能有一系列位域。



If the bit is an addressable unit on your implementation,
you may have an array of bitfields.

" Mehta Shailendrakumar" < SH ******************* @ de.bosch.com>写道:





#我想知道为什么不可能使用位域数组。

#与处理器架构有什么关系吗?

#感谢您的时间。


因为C专注于易于实现的事物在硬件中

并且通常会给库提供更难的东西,而不是在语法中实现

。没有多少CPU提供任意长度或方向的提取或存储或移动

位向量。您可以在

自己的代码中执行此操作。例如,如果你的CPU允许对整数进行未对齐访问,那么
32位整数和8位字节,


int getSignedBits(char * base,int bitoffset,int bitlength){

断言(bitlength< = 32);

int * intaddress = base +(bitoffset>> 3); //带有值的第一个字节。

int value = * intaddress; //获取所有位。

value<< = offset& 7; //字段符号位 - > int sign。

value>> = 32位长度; // field lsb - > int lsb。

返回值;

}


-

SM Ryan http://www.rawbw.com/~wyrmwif/

我认为这有点个人化;我不认为我应该回答这个问题。
"Mehta Shailendrakumar" <sh*******************@de.bosch.com> wrote:
# Hi,
#
# I would like to know why array of bitfields is not possible.
# Is there any relation with processor architecture for this?
# Thank you for your time.

Because C concentrates on things that are easy to implement in the hardware
and generally leaves harder stuff to libraries rather than implemented
in the syntax. Not many CPUs provide extracting or storing or moving
bit vectors of arbitrary length or orientation. You can do this in your
own code. For example if your CPU allows unaligned access to integers,
32 bit integers and 8 bit bytes,

int getSignedBits(char *base,int bitoffset,int bitlength) {
assert(bitlength<=32);
int *intaddress = base+(bitoffset>>3); // First byte with value.
int value = *intaddress; // Get all of the bits.
value <<= offset & 7; // field sign bit -> int sign.
value >>= 32-bitlength; // field lsb -> int lsb.
return value;
}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I think that''s kinda of personal; I don''t think I should answer that.


ju ********** @ yahoo.co.in 写道:

Mehta Shailendrakumar写道:

Mehta Shailendrakumar wrote:


我想知道为什么不可能使用位域阵列。
这与处理器架构有什么关系吗?
感谢您的时间。

问候,
Shailendra
Hi,

I would like to know why array of bitfields is not possible.
Is there any relation with processor architecture for this?
Thank you for your time.

Regards,
Shailendra



如果这个位是你实现的可寻址单元,那么你可能有一个位域数组。


If the bit is an addressable unit on your implementation,
you may have an array of bitfields.




不在C中,你不能。


问题是C'的数组操作是定义的

指向数组元素的指针 - 例如,[i]是定义为*(a + i)的
。但是,C指针

不能指向位字段; 最小的对象一个C

指针可以指向一个char。由于创建指向位字段的指针是不可能的,所以不可能使用
执行定义的数组索引等操作

就指针而言。


基础硬件可能能够解决单个位的
,但C无法使用那个能力。

(底层硬件通常有很多东西C

不能使用:循环移位,一个进位标志,内存

保护,矢量指令,超线程和

等等.C'的词汇量不够丰富,无法谈论这些东西,所以C程序不能使用它们。)


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


这篇关于位域数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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