第六题

  #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. ubuntu 修改静态IP和DNS

    1.修改配置文件/etc/network/interfacesroot@ubuntu:~# sudo vi /etc/network/interfaces 添加以下内容:auto eth0       ...

  2. Informix如何释放异常的锁资源

    问题 在Informix数据库中,锁的使用和释放是自动完成的.但在某些异常情况下,当前台程序退出(正常或异常)后,相应在数据库中的会话没有终止,其占有的资源(主要是锁)没有被释放,影响了其他用户的使用 ...

  3. [ActionScript 3.0] flash如何访问父级或者舞台上的变量、函数等的方法

    方法一: 进行类型转换,先将root.parent强制转换为MovieClip类型,再进行使用,如下:MovieClip(root).i.MovieClip(this.parent).i.MovieC ...

  4. Mac 下用 go 开发Android应用环境设置

    需要的工具 设置代理 请参考:http://www.cnblogs.com/ghj1976/p/5087049.html Mac 下命令行设置代理: export http_proxy=http:// ...

  5. centos7 下安装oracle 11g笔记

    终于在vmare的centos7下将oracle11g安装成功了,不容易,将结果记录如下 启动oracle监听及服务的步骤,使用oracle用户登录,执行以下命令 登录到CentOS,切换到oracl ...

  6. Oracle常用命令1

    一. 安装是用户管理: sqlplus /nolog; connect /as sysdba; alter user sys identified by change_on_install; alte ...

  7. OC基础(13)

    内存管理简介 引用计数器 dealloc方法 野指针\空指针 *:first-child { margin-top: 0 !important; } body > *:last-child { ...

  8. Rman-03002,Rman-12010,Rman-12012

    为什么会出现如此的错误呢? 答:因为我们在分配通道时设备类型分配为磁带,但是你并没有安装磁带设备,所以就会出现这样的错误. 错误如下: 解决方法: 登陆你的目标数据库,重新进行设置,命令如下: con ...

  9. Yii 中比较常用的rules验证规则记录

    查看代码   打印 01 return array( 02   03     //必须填写 04     array('email, username, password,agree,verifyPa ...

  10. Web前端面试常识

    大四校招即将席卷而来,现在临时抱抱佛脚,百度一下大概可能会问到的知识点,愿与君共勉吧! 1.Doctype(html4) A.strict   严格版本 B.transitional  过渡版本(防止 ...