易截截图软件、单文件、免安装、纯绿色、仅160KB

【转】C的另一重要数据结构bit fields

今天看K&R的书的时候顺便温习了C的另一重要数据结构bit-fields,我想bit-fields在编写底层驱动
驱动程序的时候应该比较好用,它可以绕开"&"和"|"进行位操作,而且更加节约内存空间。废话不多说
了,还是先来看看它的真面目吧:
bit-field来源:
bit-field是为了节约存储空间而创造的一种数据结构(to pack several objects into a single
machine word)
 
bit-field用途:
Externally-imposed data formats,such interfaces to hardware
devices,also often
require the ability to get a pieces of a word
 
定义方法:
struct (tag) {
bit-field list ~~~
} bit-field variable list;
 
由定义方法可见,bit-field也是以structure的形式组织起来的数据结构,只不过是以二进制进行分发
的而已。其中bit-field list表示方法: type fieldnamme:width;
width:field的宽度,以bit
表示,还是举个例子来看吧:
 
struct bf{
unsigned int is_keyword:1;
unsigned int is_extern:1;
unsigned int is static:1;
}flags;
 
定义了一个bit-field变量flags,它包括3个1-bit的field,访问bit-field其实和访问一般数据结
构的成员一样,例如:
flags.is_static=0;
 
在定义bit-field的时候,有以下几点要注意(K&R版本的观点)
1、Almost everything about fields is implemention-dependent !
Whether a field may
   overlap a word boundary is implemention-defined.
2、Fields can only be declared as intS,for protability,specify
signed or unsigned
   explicitly.
3、unnamed field can be used for padding(没有名字的field可以用来作为填充或者调整用)
   例如:
   struct bf{
   unsigned int a:2;
   unsigned int  :4; /*该4位不能用*/
   unsigned int b:2;
   };
 
 4、Special width 0 may be used to force alignment at the next word
boundary.
   (如果width=0的话,那么下一个field就被强制存放在下一个内存单元里面了),例如:
  
   struct bf{
   unsigned int a:2;
   unsigned int  :0; /*空的field*/
   unsigned int b:2


相关文档:

用Eclipse CDT 配置 C/C++ 编译环境

1、Java JDK的安装
Eclipse是一款跨平台的工具,只需要基本的java虚拟机就可以运行。
安装Java
SDK很简单。到http://java.sun.com上找到适合你的操作系统的Java
JDK安装程序,下载之并运行,Java SDK就会安装到你的系统中。
注意,更高版本的Eclipse需要更高版本的Java JDK。
2、Eclipse及CDT的安装
到Eclipse的官方 ......

C程序员的情书

#include <stdio.h>
#include <string.h>
int
main(void)
{
char str[] =
"3BVPSq4xF.K?=u#,"
"G'K<MrDnRr7gH%#,"
"XKf<f%G`w^=?C<#,"
"HgU_AnNR?*PDQU#,"
......

使用C语言扩展Python(二)

在上一篇中我们已经使用c语言实现了一个最简单的扩展模块,这一篇中将在其基础上进行功能的丰富。首先来考虑如何从外部的Python向C模块传递进参数,foo_bar2展示了如何向C模块传递整数,浮点数,字符串三个参数,其中"ids"指明了传入参数的数据类型。PyArg_ParseTuple负责对args进行解析,若解析失败则返回0.代码#include&n ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号