C puzzles详解【6-8题】
第六题
- #include<stdio.h>
- int main()
- {
- int a=;
- switch(a)
- {
- case '':
- printf("ONE\n");
- break;
- case '':
- printf("TWO\n");
- break;
- defa1ut:
- printf("NONE\n");
- }
- return ;
- }
题目讲解:
“defalut”拼写错误。
注意a为int型,case后的’1’,’2’为char型。
第七题
- The following C program segfaults of IA-, but works fine on IA-.
- int main()
- {
- int* p;
- p = (int*)malloc(sizeof(int));
- *p = ;
- return ;
- }
知识点讲解:
IA-64和IA-32属于完全不同的处理器架构,两者的指令集不相互兼容。
http://wangcong.org/blog/archives/291的解释为“IA-64是RISC,不能访问未对齐的地址”,不太明白。
第八题
- Here is a small piece of program(again just lines of program) which counts the number of bits set in a number.
- Input Output
- ()
- ()
- ()
- int CountBits (unsigned int x )
- {
- static unsigned int mask[] = { 0x55555555,
- 0x33333333,
- 0x0F0F0F0F,
- 0x00FF00FF,
- 0x0000FFFF
- } ;
- int i ;
- int shift ; /* Number of positions to shift to right*/
- for ( i =, shift =; i < ; i ++, shift *= )
- x = (x & mask[i ])+ ( ( x >> shift) & mask[i]);
- return x;
- }
- 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题】的更多相关文章
- C puzzles详解【51-57题】
第五十一题 Write a C function which does the addition of two integers without using the '+' operator. You ...
- C puzzles详解【46-50题】
第四十六题 What does the following macro do? #define ROUNDUP(x,n) ((x+n-1)&(~(n-1))) 题目讲解: 参考:http:// ...
- C puzzles详解【38-45题】
第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> # ...
- C puzzles详解【34-37题】
第三十四题 The following times. But you can notice that, it doesn't work. #include <stdio.h> int ma ...
- C puzzles详解【31-33题】
第三十一题 The following is a simple C program to read and print an integer. But it is not working proper ...
- C puzzles详解【26-30题】
第二十六题(不会) The following is a simple program which implements a minimal version of banner command ava ...
- C puzzles详解【21-25题】
第二十一题 What is the potential problem with the following C program? #include <stdio.h> int main( ...
- C puzzles详解【16-20题】
第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...
- C puzzles详解【13-15题】
第十三题 int CountBits(unsigned int x) { ; while(x) { count++; x = x&(x-); } return count; } 知识点讲解 位 ...
- 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 ...
随机推荐
- http请求的referer属性
HTTP Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器籍此可以获得一些信息用于处理.比如从我主页上链 ...
- 织梦DEDECMS文章、栏目页获取当前页面顶级栏目名称的方法
在用织梦做一些项目时,时常会碰到需要在当前页面调用顶级栏目名称的时候,织梦默认{dede:field name='typename' /} 可以获取当前栏目页上一级栏目的名称,而不是当前栏目顶级栏目名 ...
- Redis使用详细教程(转)
一.Redis基础部分: 1.redis介绍与安装比mysql快10倍以上 *****************redis适用场合**************** 1.取最新N个数据的操作 2.排行榜应 ...
- Grid++Report的几点总结
一.同事解决方案: 1.在View文件夹下建立报表文件A,用来作为报表呈现的载体.这个页面负责加载报表模板与加载数据源.其中报表模板由于后缀名为grf,在MVC中不做任何修改的情况下,系统会做路由处理 ...
- [JS]以下是JS省市联动菜单代码
以下是JS省市联动菜单代码: 代码一: <html> <head> <title></title> <script language=" ...
- 如何让Form窗体接收KeyDown事件?
在使用.Net Framework编写窗体应用程序的时候,有时有需要响应窗体的按键消息.当窗体上没有任何其他控件的时候,窗体是可以直接响应这些消息的. 但是当窗体上有其他控件时,会发现窗体再也不会响应 ...
- sql游标的使用
转载:http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263988.html 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集 ...
- hdu 1255 覆盖的面积(线段树 面积 交) (待整理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. In ...
- Oracle 10g RAC中的DRM问题及关闭
在RAC环境中,Oracle使用GRD(Global Resource Service)来记录各个RAC节点的资源信息,具体通过GCS(Global Cache Service)和GES(Global ...
- C# 泛型List用法
C# List Examples by Sam Allen - Updated September 6, 2009 Problem. You have questions about the List ...