﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-浮生如梦-文章分类-读《深入理解计算机系统》</title><link>http://www.cppblog.com/masiyou/category/12021.html</link><description>沉思往事立残阳，当时只道是寻常</description><language>zh-cn</language><lastBuildDate>Wed, 07 Oct 2009 08:18:13 GMT</lastBuildDate><pubDate>Wed, 07 Oct 2009 08:18:13 GMT</pubDate><ttl>60</ttl><item><title>位域</title><link>http://www.cppblog.com/masiyou/articles/98018.html</link><dc:creator>浮生如梦</dc:creator><author>浮生如梦</author><pubDate>Wed, 07 Oct 2009 04:04:00 GMT</pubDate><guid>http://www.cppblog.com/masiyou/articles/98018.html</guid><wfw:comment>http://www.cppblog.com/masiyou/comments/98018.html</wfw:comment><comments>http://www.cppblog.com/masiyou/articles/98018.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/masiyou/comments/commentRss/98018.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/masiyou/services/trackbacks/98018.html</trackback:ping><description><![CDATA[有些信息在存储时，并不需要占用一个完整的字节， 而只需占几个或一个二进制位。例如在存放一个开关量时，只有0和1 两种状态， 用一位二进位即可。为了节省存储空间，并使处理简便，Ｃ语言又提供了一种数据结构，称为&#8220;位域&#8221;或&#8220;位段&#8221;。所谓&#8220;位域&#8221;是把一个字节中的二进位划分为几 个不同的区域， 并说明每个区域的位数。每个域有一个域名，允许在程序中按域名进行操作。 这样就可以把几个不同的对象用一个字节的二进制位域来表示。一、位域的定义和位域变量的说明位域定义与结构定义相仿，其形式为： <br>struct 位域结构名 <br>{ 位域列表 };<br>其中位域列表的形式为： 类型说明符 位域名：位域长度 <br><br>例如： <br>struct bs<br>{<br>int a:8;<br>int b:2;<br>int c:6;<br>};<br>位域变量的说明与结构变量说明的方式相同。 可采用先定义后说明，同时定义说明或者直接说明这三种方式。例如： <br>struct bs<br>{<br>int a:8;<br>int b:2;<br>int c:6;<br>}data;<br>说明data为bs变量，共占两个字节。其中位域a占8位，位域b占2位，位域c占6位。对于位域的定义尚有以下几点说明：<br><br>1. 一个位域必须存储在同一个字节中，不能跨两个字节。如一个字节所剩空间不够存放另一位域时，应从下一单元起存放该位域。也可以有意使某位域从下一单元开始。例如： <br>struct bs<br>{<br>unsigned a:4<br>unsigned :0 /*空域*/<br>unsigned b:4 /*从下一单元开始存放*/<br>unsigned c:4<br>}<br>在这个位域定义中，a占第一字节的4位，后4位填0表示不使用，b从第二字节开始，占用4位，c占用4位。<br><br>2. 由于位域不允许跨两个字节，因此位域的长度不能大于一个字节的长度，也就是说不能超过8位二进位。<br><br>3. 位域可以无位域名，这时它只用来作填充或调整位置。无名的位域是不能使用的。例如： <br>struct k<br>{<br>int a:1<br>int :2 /*该2位不能使用*/<br>int b:3<br>int c:2<br>};<br>从以上分析可以看出，位域在本质上就是一种结构类型， 不过其成员是按二进位分配的。<br><br>二、位域的使用位域的使用和结构成员的使用相同，其一般形式为： 位域变量名&#183;位域名 位域允许用各种格式输出。<br>main(){<br>struct bs<br>{<br>unsigned a:1;<br>unsigned b:3;<br>unsigned c:4;<br>} bit,*pbit;<br>bit.a=1;<br>bit.b=7;<br>bit.c=15;<br>printf("%d,%d,%d\n",bit.a,bit.b,bit.c);<br>pbit=&amp;bit;<br>pbit-&gt;a=0;<br>pbit-&gt;b&amp;=3;<br>pbit-&gt;c|=1;<br>printf("%d,%d,%d\n",pbit-&gt;a,pbit-&gt;b,pbit-&gt;c);<br>} <br><br>上例程序中定义了位域结构bs，三个位域为a,b,c。说明了bs类型的变量bit和指向bs类型的指针变量pbit。这表示位域也是可以使用指针的。<br>程序的9、10、11三行分别给三个位域赋值。( 应注意赋值不能超过该位域的允许范围)程序第12行以整型量格式输出三个域的内容。第13行把位域变量bit的地址送给指针变量pbit。第14行用指针 方式给位域a重新赋值，赋为0。第15行使用了复合的位运算符"&amp;="， 该行相当于： pbit-&gt;b=pbit-&gt;b&amp;3位域b中原有值为7，与3作按位与运算的结果为3(111&amp;011=011,十进制值为 3)。同样，程序第16行中使用了复合位运算"|="， 相当于： pbit-&gt;c=pbit-&gt;c|1其结果为15。程序第17行用指针方式输出了这三个域的值。<br>
<img src ="http://www.cppblog.com/masiyou/aggbug/98018.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/masiyou/" target="_blank">浮生如梦</a> 2009-10-07 12:04 <a href="http://www.cppblog.com/masiyou/articles/98018.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一些技巧（以后还有更新，未完，待续）</title><link>http://www.cppblog.com/masiyou/articles/98015.html</link><dc:creator>浮生如梦</dc:creator><author>浮生如梦</author><pubDate>Wed, 07 Oct 2009 03:21:00 GMT</pubDate><guid>http://www.cppblog.com/masiyou/articles/98015.html</guid><wfw:comment>http://www.cppblog.com/masiyou/comments/98015.html</wfw:comment><comments>http://www.cppblog.com/masiyou/articles/98015.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/masiyou/comments/commentRss/98015.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/masiyou/services/trackbacks/98015.html</trackback:ping><description><![CDATA[1、a&amp;&amp;5/a可以避免处以0的错误， r = j ? i/j : div_zero()也可以<br>2、p&amp;&amp;*p++不会导致间接引用空指针<br>3、！x^y 等价于x==y <br>4、由于每个小写字母的ASCII值比对应大写字母大32，要将小写字母转换为大写字母需要将第六位设为0，所以可以用<br>ch &amp;=223<br>将小写字母转换为对应大写字母（当然也可以直接减去32）；<br>而将大写字母转换为对应小写字母，需要将第六位设为1，所以可以用<br>ch |= 32<br>将大写字母转换为对应小写字母（当然也可以直接加32）<br><br>
<img src ="http://www.cppblog.com/masiyou/aggbug/98015.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/masiyou/" target="_blank">浮生如梦</a> 2009-10-07 11:21 <a href="http://www.cppblog.com/masiyou/articles/98015.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>位运算的性质</title><link>http://www.cppblog.com/masiyou/articles/98013.html</link><dc:creator>浮生如梦</dc:creator><author>浮生如梦</author><pubDate>Wed, 07 Oct 2009 03:14:00 GMT</pubDate><guid>http://www.cppblog.com/masiyou/articles/98013.html</guid><wfw:comment>http://www.cppblog.com/masiyou/comments/98013.html</wfw:comment><comments>http://www.cppblog.com/masiyou/articles/98013.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/masiyou/comments/commentRss/98013.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/masiyou/services/trackbacks/98013.html</trackback:ping><description><![CDATA[交换律 <br>a|b = b|a<br>a&amp;b = b&amp;a<br>a^b = b^a<br><br>结合律<br>(a|b)|c = a|(b|c)<br>(a&amp;b)&amp;c = a&amp;(b&amp;c)<br>(a^b)^c = a^(b^c)<br><br>分配律(其他类推)<br>a&amp;(b|c) = (a&amp;b)|(a&amp;c)<br>a^(b|c)=(a^b)|(a^c)<br><br>其他<br>a|0 = a<br>a&amp;1 = a<br>a&amp;0 = 0<br><br>a^a = 0<br>a^0 =a<br><br>a|~a = 1<br>a&amp;~a = 0<br>a&amp;a = a<br>a|a = a<br><br>a|(a&amp;b) = a<br>a&amp;(a|b) = a<br>
<img src ="http://www.cppblog.com/masiyou/aggbug/98013.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/masiyou/" target="_blank">浮生如梦</a> 2009-10-07 11:14 <a href="http://www.cppblog.com/masiyou/articles/98013.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用^实现两个数的交换——据说没有性能上的优势</title><link>http://www.cppblog.com/masiyou/articles/98010.html</link><dc:creator>浮生如梦</dc:creator><author>浮生如梦</author><pubDate>Wed, 07 Oct 2009 02:39:00 GMT</pubDate><guid>http://www.cppblog.com/masiyou/articles/98010.html</guid><wfw:comment>http://www.cppblog.com/masiyou/comments/98010.html</wfw:comment><comments>http://www.cppblog.com/masiyou/articles/98010.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/masiyou/comments/commentRss/98010.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/masiyou/services/trackbacks/98010.html</trackback:ping><description><![CDATA[void inplace_swap(int *x, int *y)<br>{<br>*x = *x ^ *y;<br>*y = *x ^ *y;<br>*x = *x ^ *y;<br>}
<img src ="http://www.cppblog.com/masiyou/aggbug/98010.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/masiyou/" target="_blank">浮生如梦</a> 2009-10-07 10:39 <a href="http://www.cppblog.com/masiyou/articles/98010.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用typedef将byte_pointer定义为指向unsigned char的对象的指针（新）</title><link>http://www.cppblog.com/masiyou/articles/98006.html</link><dc:creator>浮生如梦</dc:creator><author>浮生如梦</author><pubDate>Wed, 07 Oct 2009 02:29:00 GMT</pubDate><guid>http://www.cppblog.com/masiyou/articles/98006.html</guid><wfw:comment>http://www.cppblog.com/masiyou/comments/98006.html</wfw:comment><comments>http://www.cppblog.com/masiyou/articles/98006.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/masiyou/comments/commentRss/98006.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/masiyou/services/trackbacks/98006.html</trackback:ping><description><![CDATA[<p><font face="Courier New, Courier, mono" size=-1>&nbsp;#include &lt;stdio.h&gt;</font></p>
<p><font face="Courier New, Courier, mono" size=-1>typedef unsigned char *byte_pointer;</font></p>
<p><font face="Courier New, Courier, mono" size=-1>void show_bytes(byte_pointer start, int len)<br>{<br>&nbsp;&nbsp;&nbsp; int i;<br>&nbsp;&nbsp;&nbsp; for(i= 0; i &lt; len; i++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%.2x", start[i]);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; printf("\n");<br>}</font></p>
<p><font face="Courier New, Courier, mono" size=-1>void show_int(int x)<br>{<br>&nbsp;&nbsp;&nbsp; show_bytes((byte_pointer)&amp;x, sizeof(int));<br>}</font></p>
<p><font face="Courier New, Courier, mono" size=-1>void show_float(float x)<br>{<br>&nbsp;&nbsp;&nbsp; show_bytes((byte_pointer)&amp;x, sizeof(float));<br>}<br>void show_pointer(void *x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; show_bytes((byte_pointer)&amp;x,sizeof(void*));&nbsp;&nbsp;&nbsp;&nbsp; //sizeof(void *)????<br>}<br>int main()<br>{<br>&nbsp;&nbsp;&nbsp; int ival = 10;<br>&nbsp;&nbsp;&nbsp; float fval = (float) ival;<br>&nbsp;&nbsp;&nbsp; int *pval = &amp;ival;<br>&nbsp;&nbsp;&nbsp; show_int(ival);<br>&nbsp;&nbsp;&nbsp; show_float(fval);<br>&nbsp;&nbsp;&nbsp; show_pointer(pval);<br>&nbsp;&nbsp;&nbsp; return 0;<br>}<br></font></p>
输出是<br><br>0a000000<br>00002041<br>74ff2200<br><br>不明白怎么来的<br>尤其是那个<font face="Courier New" size=2>sizeof(void *)????</font> 、<br>还有那个printf("%.2x")和printf("%2x")有什么差别哦？？？
<img src ="http://www.cppblog.com/masiyou/aggbug/98006.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/masiyou/" target="_blank">浮生如梦</a> 2009-10-07 10:29 <a href="http://www.cppblog.com/masiyou/articles/98006.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>