C puzzles详解【51-57题】
第五十一题
Write a C function which does the addition of two integers without using the '+' operator. You can use only the bitwise operators.(Remember the good old method of implementing the full-adder circuit using the or, and, xor gates....)
题目讲解: 参考:
http://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/
int Add(int x, int y)
{
// Iterate till there is no carry
while (y != )
{
// carry now contains common set bits of x and y
int carry = x & y; // Sum of bits of x and y where at least one of the bits is not set
x = x ^ y; // Carry is shifted by one so that adding it to x gives the required sum
y = carry << ;
}
return x;
}
或
int Add(int x, int y)
{
if (y == )
return x;
else
return Add( x ^ y, (x & y) << );
}
第五十二题
How do you print I can print % using the printf function? (Remember % is used as a format specifier!!!)
题目讲解: 参考:
http://www.geeksforgeeks.org/how-to-print-using-printf/
printf("%%");
printf("%c", '%');
printf("%s", "%");
第五十三题
What's the difference between the following two C statements?
const char *p;
char* const p;
题目讲解:
const char *p:
p指向的值只读;
char* const p:
p的值只读;
第五十四题
What is the difference between memcpy and memmove?
题目讲解:
对重叠区域(overlapping regions)的处理有区别。
第五十五题
What is the format specifiers for printf to print double and float values?
题目讲解:
double: %lf
float: %f
第五十六题
Write a small C program to determine whether a machine's type is little-endian or big-endian.
题目讲解:
参考:
http://www.geeksforgeeks.org/little-and-big-endian-mystery/
unsigned int determine_endian()
{
unsigned int i = ;
char *c = (char *)&i;
if (*c)
return ;//little endian
else
return ;//big endian
}
第五十七题
Write a C program which prints Hello World! without using a semicolon!!!
题目讲解:
#include <stdio.h> int main()
{
while(printf(“Hello World!”)<)
{}
}
C puzzles详解【51-57题】的更多相关文章
- 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 ...
随机推荐
- Spring切入点表达式常用写法
自从使用AspectJ风格切面配置,使得Spring的切面配置大大简化,但是AspectJ是另外一个开源项目,其规则表达式的语法也稍稍有些怪异. 下面给出一些常见示例的写法: 比如,下面是一个 ...
- Java中-XMX -xmn 是什么的缩写
这个应该是 eclipse 的配置文件 eclipse.ini 中的配置语句.在配置文件中直接传递给 java vm 的参数并不多,调用形式是这样的: 1 eclipse [normal argume ...
- [Flex] IFrame系列 —— 在flex的web应用中嵌入html的方法
在flex的web应用中,我们往往必须有嵌入html的需求,这时候你会发现IFrame很有用! flex而且可以和html中的JavaScript进行交互,flex可以通过iframe调用到html中 ...
- iOS 模态视图
模态视图不是专门的某个类,而是通过视图控制器的presentViewController方法弹出的视图,我们称为模态视图. 模态视图出现的场景一般是临时弹出的窗口,譬如:登录窗口: 模态视图弹出时通过 ...
- java类总结
1.java类与类之间存在六种关系:继承,实现,依赖,关联,聚合组成 2.一个类可以继承另外一个类,并在此基础上添加自己的特有的功能. 3.一个类的方法中操作另外一个类的对象,这种情况称为第一个类依赖 ...
- Network of Schools(强连通分量缩点(邻接表&矩阵))
Description A number of schools are connected to a computer network. Agreements have been developed ...
- Android数据的四种存储方式
作为一个完成的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File. ...
- android 如何设置背景的透明度
半透明<Button android:background="#e0000000" ... />透明<Button android:background=&quo ...
- Appium环境搭建Java篇
1.下载.安装JDK&配置Java环境变量 JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html ...
- linux 将foo制定n, m之间行的内容, 追加到bar文件
sed -ne '196, 207 p' foo >> bar;把文件foo 196-行207行的内容追加到 bar文件