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 ...
随机推荐
- tar.gz和rpm安装文件(转载)
from:http://bbs.chinaunix.net/thread-2277750-1-1.html Linux软件的二进制分发是指事先已经编译好二进制形式的软件包的发布形式,其优点是安装使用容 ...
- 计算webView的 高度 和自适应屏幕大小
- (void)webViewDidFinishLoad:(UIWebView *)webView{ [webView stringByEvaluatingJavaScriptFromString: ...
- mysqldump 数据库迁移并改换engine
1. 导出数据库 mysqldump -h172.18.165.xxx -P3306 -uroot -pxxx --add-drop-database --default-character-set= ...
- UIView之userInteractionEnabled属性介绍
来源:http://my.oschina.net/hmj/blog/108002 属性作用 该属性值为布尔类型,如属性本身的名称所释,该属性决定UIView是否接受并响应用户的交互. 当值设置为NO后 ...
- 第5章 Posix 消息队列
5.1 概述 消息队列可以认为是一个链表.有写权限的线程可往消息队列中放置消息,有读权限的线程可以从消息队列中取走消息. 消息队列和管道/FIFO的区别: (1)消息队列往一个队列中写消息前,并不需要 ...
- [Java] 03 String获取文件后缀名,判断文件是否合法
package test.string; import java.util.Arrays; import java.util.List; public class GetFileType { priv ...
- webstrom插件:如何设置才能让webstrom能提示bootstrap的语法
<link href="bootstrap.min.css" rel="stylesheet"> <link href="boots ...
- jquery怎么实现跨域的访问呢?与别人提供的接口连接
下面这个例子你可以参考下 <script> $.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty ...
- Linux Hugepage ,AMM及 USE_LARGE_PAGES for oracle 11G(转载)
1. Hugepage基本概念 系统进程是通过虚拟地址访问内存,但是CPU必须把它转换成物理内存地址才能真正访问内存.为了提高这个转换效率,CPU会缓存最近的“虚拟内存地址和物理内存地址”的 ...
- 区域生长算法(附MATLAB代码实现)
一.理论概念 区域生长是按照事先定义的生长准则将一个像素或者子区域逐步聚合成一个完整独立的连通区域过程.对于图像感兴趣目标区域R,z为区域R上事先发现的种子点,按照规定的生长准则逐步将与种子点z一定邻 ...