易截截图软件、单文件、免安装、纯绿色、仅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/c++经典面试试题及标准答案

经过几次面试,发现笔试题基本上都是那几道,没有什么创新或者改变,总结出来给大家参考参考.
  
一、请填写BOOL , float, 指针变量与“零值”比较的 if 语句。(10分)
请写出 BOOL flag 与“零值”比较的 if 语句。(3分)
标准答案:
    if ( flag )
  & ......

C\C++ byte或者char数组转int值

假设有char a[2];
如要把a转换为int值。应是如下写法int b=*(int *)a;
即,先把指针a 转换为一个int指针,然后再此基础上取值。
但是另一种写法 int b=(int)(*a);是不对的,*a 取a的内存单元内容,因为现在a是char指针,所以只会取a[1]中内容,最大为255. 这里要说明的是,在把char或byte数组转换为其他类型的值时,要先 ......

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库函数详解——assert

c库函数详解——assert
函数名: assert
功  能: 测试一个条件并可能使程序终止
用  法: void assert(int test);
程序例:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
struct ITEM {
   int key;
   int value;
};
/* add item to ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号