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 ...
随机推荐
- MySQL优化技巧之四(数据库设计中的一些技巧)
1. 原始单据与实体之间的关系 可以是一对一.一对多.多对多的关系.在一般情况下,它们是一对一的关系:即一张原始单据对应且只对应一个实体.在特殊情况下,它们可能是一对多或多对一的关系,即一张原始单证对 ...
- 【Python】迭代器、生成器、yield单线程异步并发实现详解
转自http://blog.itpub.net/29018063/viewspace-2079767 大家在学习python开发时可能经常对迭代器.生成器.yield关键字用法有所疑惑,在这篇文章将从 ...
- [HDU 4585] Shaolin (map应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题目大意:不停的插入数字,问你跟他相距近的ID号.如果有两个距离相近的话选择小的那个. 用map ...
- T4 模板的调试方法,方便大家遇到问题自己快速定位和优化
T4 模板的调试方法,方便大家遇到问题自己快速定位和优化 :1. .ttinclude文件的第一行修改为 <#@ template language="C#" debug=& ...
- Fiddler录制jmeter脚本,干货分享
我们知道以前jmeter的脚本来源有三个,手动书写.badboy录制.自带的录制功能(jmeter3.0该功能还比较好),目前我们又多了一个fiddler生成,自上次分享出来fiddler ...
- VR就是下一个浪潮_2016 (GMGC) 全球移动游戏大会观后感
"VR就是下一个浪潮" --2016 (GMGC) 全球移动游戏大会观后感 早在2014年参会Unity举办的一年一度的金立方盛典大会,就初次体验了VR头盔设备,于是印象深刻 ...
- Class attributes
In order to print Card objects in a way that people can easily read, we need a mapping from the inte ...
- jQuery Mask
<script type="text/javascript" src="/assets/mask/jquery.mask.min.js"></ ...
- 【Hibernate 3】一对一映射配置
一.一对一映射简介 一对一关联映射有两种实现策略: 第一种:主键关联,即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库表不会有额外的字段来维护它们之间的关系,仅通过表的主键来关联 ...
- JS与JQ倒计时的写法
页面需要制作一个倒计时的功能:然后度娘了一遍,找到两种写法,原生JS与JQ 的,经过测试原生JS在IE可能会有不刷新的现象所以结合了一个大神的JQ写法修改好了一个. 原生JS写法: HTML: < ...