第六题

  #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题】的更多相关文章

  1. C puzzles详解【51-57题】

    第五十一题 Write a C function which does the addition of two integers without using the '+' operator. You ...

  2. C puzzles详解【46-50题】

    第四十六题 What does the following macro do? #define ROUNDUP(x,n) ((x+n-1)&(~(n-1))) 题目讲解: 参考:http:// ...

  3. C puzzles详解【38-45题】

    第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> # ...

  4. C puzzles详解【34-37题】

    第三十四题 The following times. But you can notice that, it doesn't work. #include <stdio.h> int ma ...

  5. C puzzles详解【31-33题】

    第三十一题 The following is a simple C program to read and print an integer. But it is not working proper ...

  6. C puzzles详解【26-30题】

    第二十六题(不会) The following is a simple program which implements a minimal version of banner command ava ...

  7. C puzzles详解【21-25题】

    第二十一题 What is the potential problem with the following C program? #include <stdio.h> int main( ...

  8. C puzzles详解【16-20题】

    第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...

  9. C puzzles详解【13-15题】

    第十三题 int CountBits(unsigned int x) { ; while(x) { count++; x = x&(x-); } return count; } 知识点讲解 位 ...

  10. 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 ...

随机推荐

  1. 软件开发过程文档-cgaowei

    鸡肋——食之无味,弃之可惜”,软件开发过程文档遭遇了鸡肋一样的境遇. 目前敏捷软件开发过程非常流行.相对于软件开发过程文档,敏捷软件开发过程更加重视可运行的程序.关于软件开发过程文档,两个极端都是不可 ...

  2. Intellisense for Xrm.Page in CRM 2011

    Intellisense for Xrm.Page in CRM 2011 In CRM 2011 javascripts for crm forms can be stored externally ...

  3. 出现java.lang.NoClassDefFoundError: com/google/common/base/Charsets异常错误

    使用selenium,出现java.lang.NoClassDefFoundError: com/google/common/base/Charsets异常错误 原因:selenium-server- ...

  4. 2013 ACM 通化邀请赛D.D-City 并查集

    点击打开链接 D.D-City Description Luxer is a really bad guy. He destroys everything he met. One day Luxer ...

  5. 日志挖掘Logmnr

    日志挖掘 9.1 日志中数据用途 所有对用户数据以及数据字典的改变全部被保存在联机日志中.当然nologging,insert/*+append+/情况比较特殊除外,因此归档日志可以用来做数据库的恢复 ...

  6. 在线重定义(Rdefine Table online)

    二.        概念理解 在线重定义用于对表的逻辑或者物理结构的修改,而且在修改时不影响表的可用性与传统方式相比.当一个表被重定义时,会被锁定为exclusive mode很短一段时间,这段时间的 ...

  7. js压缩反压缩

    JavaScript unpacker and beautifier JavaScript Beautifier http://prettydiff.com/?m=beautify&s=htt ...

  8. 程序员必备:Oracle日常维护命令

        上一篇讲了Linux的日常维护命令,这篇讲讲Oracle的日常维护命令.工作中需要使用Oracle数据库的童鞋们,相信或多或少都需要对Oracle做一些基本的维护操作,例如导入导出总该有吧?( ...

  9. 01-事件处理简介/UIView拖拽

    1.ios当中常见的事件?         触摸事件        加速计事件         远程控制事件2.什么是响应者对象?     继承了UIResponds的对象我们称它为响应者对象 UIA ...

  10. HDFS副本机制&负载均衡&机架感知&访问方式&健壮性&删除恢复机制&HDFS缺点

    副本机制 1.副本摆放策略 第一副本:放置在上传文件的DataNode上:如果是集群外提交,则随机挑选一台磁盘不太慢.CPU不太忙的节点上:第二副本:放置在于第一个副本不同的机架的节点上:第三副本:与 ...