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 ...
随机推荐
- Word Frequency
https://leetcode.com/problems/word-frequency/ Write a bash script to calculate the frequency of each ...
- Delphi ServerSocket,ClientSocket示例
Delphi ServerSocket,ClientSocket示例 2008-05-09 16:20 Delphi TServerSocket,TClientSocket实现传送文件代码 1.建立两 ...
- [kuangbin带你飞]专题二十二 区间DP
ID Origin Title 17 / 60 Problem A ZOJ 3537 Cake 54 / 105 Problem B LightOJ 1422 Hallowee ...
- Regex.Replace的基本用法
Regex构造函数Regex(string pattern)Regex(string pattern,RegexOptions options)参数说明pattern:要匹配的正则表达式模式optio ...
- Android中通过广播方式调起第三方App
今天紧急的跟进一个百度视频App无法调起百度贴吧App的问题,当然,这个是只发现是在4.x的android系统下发生,在2.x版本下,一切正常,(其实是3.1及以上的版本都有问题)具体场景为: 1.贴 ...
- AOP切入点注解报错
开始学习AOP,出现如下的错误,最后发现是JDK与aspectj,aspectjweaver版本问题造成的.之后改成最新版本,代码运行正常. 利用这些特性可以进行代码的插入,比如权限,缓存结合mecm ...
- C# odbc
一直下一步,注意需要 勾选你要连接的库名 odbc 命名空间 System.Data.Odbc
- Android——inflate 将一个xml中定义的布局找出来
通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组 ...
- H264-AVS POC理解
H264码流的输出顺序是编码顺序,所以在编码B帧的时候,由于B是双向预测,需要先编码后面编码帧P/I,这时候先输出I/P,后面才有B帧. 在解码段拿到相应的I/P帧后,不能马上丢到buffer lis ...
- java 实例之杨辉三角
public class study{ public static void main(String args[]){ int i,j,level=7; int Yang[][] = new int[ ...