第六题

  1. #include<stdio.h>
  2. int main()
  3. {
  4. int a=;
  5. switch(a)
  6. {
  7. case '':
  8. printf("ONE\n");
  9. break;
  10. case '':
  11. printf("TWO\n");
  12. break;
  13. defa1ut:
  14. printf("NONE\n");
  15. }
  16. return ;
  17. }

题目讲解:

“defalut”拼写错误。

注意a为int型,case后的’1’,’2’为char型。

第七题

  1. The following C program segfaults of IA-, but works fine on IA-.
  2.  
  3. int main()
  4. {
  5. int* p;
  6. p = (int*)malloc(sizeof(int));
  7. *p = ;
  8. return ;
  9. }

知识点讲解:

IA-64和IA-32属于完全不同的处理器架构,两者的指令集不相互兼容。

http://wangcong.org/blog/archives/291的解释为“IA-64是RISC,不能访问未对齐的地址”,不太明白。

第八题

  1. Here is a small piece of program(again just lines of program) which counts the number of bits set in a number.
  2. Input Output
  3. ()
  4. ()
  5. ()
  6.  
  7. int CountBits (unsigned int x )
  8. {
  9. static unsigned int mask[] = { 0x55555555,
  10. 0x33333333,
  11. 0x0F0F0F0F,
  12. 0x00FF00FF,
  13. 0x0000FFFF
  14. } ;
  15.  
  16. int i ;
  17. int shift ; /* Number of positions to shift to right*/
  18. for ( i =, shift =; i < ; i ++, shift *= )
  19. x = (x & mask[i ])+ ( ( x >> shift) & mask[i]);
  20. return x;
  21. }
  22. Find out the logic used in the above program.

题目讲解:

计算一个int型参数二进制模式中1的个数。传统的算法要经过32次循环,

此算法最多只需5次。

以x = 1234为例,

1234的二进制表示为:

0000 0100 1101 0010

第一步:相邻2位相加

0000 0100 1101 0010 ---> 0000 0100 1001 0001

第二步:相邻4位相加

0000 0100 1001 0001 ---> 0000 0001 0011 0001

第三步:相邻8位相加

0000 0001 0011 0001 ---> 0000 0001 0000 0100

第四步:相邻16位相加

0000 0001 0000 0100 ---> 0000 0000 0000 0101

第五步:相邻32位相加

无需继续计算。

结果为5。

至于这么做为什么恰巧得到的是1的个数,不解。

C puzzles详解【6-8题】的更多相关文章

  1. C puzzles详解【51-57题】

    第五十一题 Write a C function which does the addition of two integers without using the '+' operator. You ...

  2. C puzzles详解【46-50题】

    第四十六题 What does the following macro do? #define ROUNDUP(x,n) ((x+n-1)&(~(n-1))) 题目讲解: 参考:http:// ...

  3. C puzzles详解【38-45题】

    第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> # ...

  4. C puzzles详解【34-37题】

    第三十四题 The following times. But you can notice that, it doesn't work. #include <stdio.h> int ma ...

  5. C puzzles详解【31-33题】

    第三十一题 The following is a simple C program to read and print an integer. But it is not working proper ...

  6. C puzzles详解【26-30题】

    第二十六题(不会) The following is a simple program which implements a minimal version of banner command ava ...

  7. C puzzles详解【21-25题】

    第二十一题 What is the potential problem with the following C program? #include <stdio.h> int main( ...

  8. C puzzles详解【16-20题】

    第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...

  9. C puzzles详解【13-15题】

    第十三题 int CountBits(unsigned int x) { ; while(x) { count++; x = x&(x-); } return count; } 知识点讲解 位 ...

  10. C puzzles详解【9-12题】

    第九题 #include <stdio.h> int main() { float f=0.0f; int i; ;i<;i++) f = f + 0.1f; if(f == 1.0 ...

随机推荐

  1. http请求的referer属性

    HTTP Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器籍此可以获得一些信息用于处理.比如从我主页上链 ...

  2. 织梦DEDECMS文章、栏目页获取当前页面顶级栏目名称的方法

    在用织梦做一些项目时,时常会碰到需要在当前页面调用顶级栏目名称的时候,织梦默认{dede:field name='typename' /} 可以获取当前栏目页上一级栏目的名称,而不是当前栏目顶级栏目名 ...

  3. Redis使用详细教程(转)

    一.Redis基础部分: 1.redis介绍与安装比mysql快10倍以上 *****************redis适用场合**************** 1.取最新N个数据的操作 2.排行榜应 ...

  4. Grid++Report的几点总结

    一.同事解决方案: 1.在View文件夹下建立报表文件A,用来作为报表呈现的载体.这个页面负责加载报表模板与加载数据源.其中报表模板由于后缀名为grf,在MVC中不做任何修改的情况下,系统会做路由处理 ...

  5. [JS]以下是JS省市联动菜单代码

    以下是JS省市联动菜单代码: 代码一: <html> <head> <title></title> <script language=" ...

  6. 如何让Form窗体接收KeyDown事件?

    在使用.Net Framework编写窗体应用程序的时候,有时有需要响应窗体的按键消息.当窗体上没有任何其他控件的时候,窗体是可以直接响应这些消息的. 但是当窗体上有其他控件时,会发现窗体再也不会响应 ...

  7. sql游标的使用

    转载:http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263988.html 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集 ...

  8. hdu 1255 覆盖的面积(线段树 面积 交) (待整理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.   In ...

  9. Oracle 10g RAC中的DRM问题及关闭

    在RAC环境中,Oracle使用GRD(Global Resource Service)来记录各个RAC节点的资源信息,具体通过GCS(Global Cache Service)和GES(Global ...

  10. C# 泛型List用法

    C# List Examples by Sam Allen - Updated September 6, 2009 Problem. You have questions about the List ...