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 ...
随机推荐
- linux挂载U盘(转载)
一.Linux挂载U盘:1.插入u盘到计算机,如果目前只插入了一个u盘而且你的硬盘不是scsi的硬盘接口的话,那它的硬件名称为:sda1.2.在mnt目录下先建立一个usb的目录(如:[root@lo ...
- jstatd命令
jstatd [options] 此命令是一个RMI Server应用程序,提供了对JVM的创建和结束监视,也为远程监视工具提供了一个可以attach的接口options -nr 当一个存在的RMI ...
- 91、sendToTarget与sendMessage
Message msg = handler.obtainMessage(); msg.arg1 = i; msg.sendToTarget(); ...
- java小程序实例 闰年
判断闰年. package com.test; import java.util.Scanner; import org.junit.Test; public class TestRunNian { ...
- javascript 传递引用类型参数
JavaScript代码如下: function setName(obj){ obj.name = "test1"; obj = new Object(); obj.name = ...
- 手机端的各种默认样式比如 ios的按钮变灰色
1.ios按钮变灰色,给按钮加样式: -webkit-appearance: none; 2.有圆角话 ; } 3.去除Chrome等浏览器文本框默认发光边框 input:focus, textare ...
- delphi Pointer 转成string
var s: string; p: pointer; s := PChar(p);前提p指向的字符串要以#0结尾.
- [ACDream 1430]SETI 后缀数组
题目链接:http://acdream.info/problem?pid=1430 题目大意:给你一个长度不超过10000的字符串,问你出现过两次或两次以上的不重叠的子串有多少个. 后缀数组计算出he ...
- java环境变量完整版
jdk默认安装 Key: JAVA_HOME(新建) Value: C:\Program Files\Java\jdk1.8.0_25 Key: Path(编辑) Value: %JAVA_HOME% ...
- 01-Quartz2D介绍
01-PPT介绍 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !impo ...