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 ...
随机推荐
- 用R在字符串中提取匹配的部分
例如在aaaa12xxxx中提取12,在参考了stackoverflow后比较方便的大致有以下几种方法: 利用sub跟gsub sub(".*?([0-9]+).*", " ...
- 如何构建日均千万PV Web站点
http://www.cnblogs.com/xiaocen/p/3723839.html http://www.cnblogs.com/xiaocen/p/3726763.html http://w ...
- C++ primer 练习9.49
如果一个字母延伸到中线之上,如d或f,则称其有上出头部分.如果一个字母延伸到中线之下,如p或g, 则称其有下出头部分.编写程序,读入一个单词,输出最长的即不包含上出头部分,也不包含下出头部分单 词. ...
- hadoop配置优化
yarn-site.xml <property> <name>yarn.nodemanager.resource.memory-mb</name> <valu ...
- 关于前置式递增和后置式递增的小知识(++x与x++)
list<char>::iterator pos; //list<char> coll; for(pos=coll.begin();pos!=coll.end();++pos) ...
- WIN7上搭建Windows Phone 8 开发环境——VMware Workstation下Win8 “无法安装Hyper-V, 某个虚拟机监控程序正在运行”问题解决的办法
最近在试着在Windows 7上搭建Windows Phone 8的开发调试环境,使用的是VMware Workstation + Win8 Pro的虚拟环境, 在漫长的WPexpress_full下 ...
- Android——主流分辨率
VGA:480*640 QVGA:240*320 HVGA:320*480 WVGA:480*800 FWVGA:480*854 IntelHaxm.exe 模拟器加速器
- Kung fu
1. originPeople in Primitive society(原始社会)in order to survive,they have to hunt for food efficiently ...
- DAY13 Matlab实现图像错切源代码
Matlab实现图像错切源代码 %错切im=(imread('robot.jpg'));im1=rgb2gray(im);figure,imshow(im1);[row,col]=size(im1); ...
- Ubuntu 16.04 LTS U盘安装要点
一.UltraISO UltraISO是一款功能强大而又方便实用的光盘映像文件制作/编辑/转换工具,它可以直接编辑ISO文件和从ISO中提取文件和目录,也可以从CD-ROM制作光盘映像或者将硬盘上的文 ...