C和指针 第五章 习题
下列输出的值:
#include <stdio.h>
int func(){
static int count = 1;
return ++count;
}
int main()
{
int answer = 0;
answer = func() - func() * func();
printf("%d\n", answer);
return 0;
}
answer = 2 - 3 * 4;
所以结果 -10;
5.3 编写函数 unsigned int reverse_bits(unsigned int value);将二进制模式从左到右变换,输入25输出2 550 136 832
#include <stdio.h> //反转bit位
unsigned int reverse_bits(unsigned int value)
{
unsigned int result = 0;
unsigned int tmp = 1;
//计算unsigned int位数
int len = sizeof(unsigned int) * 8;
int idx; //每次向右位偏移一位
for(idx = 0; idx < len; idx++){
//判断bit值,如果是1
if(((value >> idx)) & 1){
//则将tmp中对应的另一端的位置为1
tmp = 1 << (len - idx - 1);
//tmp和result取或运算置位1
result |= tmp;
}
}
return result;
}
//将value二进制形式打印出来
void print_bits(unsigned int value)
{
int len = sizeof(unsigned int) * 8;
int idx;
int bit; for(idx = 1; idx <= len; idx++){
//从左往右,通过位偏移后,和1取与运算,打印bit位的值
bit = 1 & (value >> (len - idx)); printf("%d", bit);
//四位一空,方便阅读
if(idx % 4 == 0){
printf(" ");
}
}
printf("\n");
} int main()
{
unsigned int input = 25;
unsigned int result; printf("%d\n", input);
print_bits(input); result = reverse_bits(input); printf("%u\n", result);
print_bits(result); return 0;
}
输出:
25
0000 0000 0000 0000 0000 0000 0001 1001
2550136832
1001 1000 0000 0000 0000 0000 0000 0000
5.5把给定的值存储到一个整数中指定的几个位
#include <stdio.h> int store_bit_field(int original_value, int value_to_store, unsigned starting_bit, unsigned ending_bit)
{
int mask = 0;
int tmp = 0;
//制作对应的起始位置掩码
for(int idx = starting_bit; idx >= ending_bit; idx--){
tmp = 1 << idx;
mask |= tmp;
} //通过掩码将original_value 对应的范围内置0
original_value &= ~mask;
//将value_to_store对齐起始和结束位置
value_to_store <<= ending_bit;
//value_to_store中将超出范围的部分置0
value_to_store &= mask;
//将值和original_value取或保存值
original_value |= value_to_store; return original_value;
} int main()
{
int result;
result = store_bit_field(0xffff, 0x123, 13, 9);
printf("0x%0x\n", result); return 0;
}
输出:
oxc7ff
C和指针 第五章 习题的更多相关文章
- 《学习Opencv》第五章 习题6
这是第五章 习题5.6的结合版,其中实现了摄像头抓拍功能,能够成功运行. #include "stdafx.h" #include "cv.h" #includ ...
- 统计学习导论:基于R应用——第五章习题
第五章习题 1. 我们主要用到下面三个公式: 根据上述公式,我们将式子化简为 对求导即可得到得到公式5-6. 2. (a) 1 - 1/n (b) 自助法是有有放回的,所以第二个的概率还是1 - 1/ ...
- C和指针 第六章 习题
6.1编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集中出现的字符,返回第一个找到的字符位置指针,未找到返回NULL #include <stdio.h> char * f ...
- C和指针 第十五章 习题
15.8 十六进制倾印码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...
- C和指针 第十七章 习题
17.8 为数组形式的树编写模块,用于从树中删除一个值,如果没有找到,程序节点 ArrayBinaryTree.c // // Created by mao on 16-9-18. // #inclu ...
- C和指针 第十三章 习题
1,1标准输入读入字符,统计各类字符所占百分比 #include <stdio.h> #include <ctype.h> //不可打印字符 int isunprint(int ...
- C和指针 第十一章 习题
1编写calloc,内部使用malloc函数获取内存 #include <stdio.h> #include <stdlib.h> void *myAlloc(unsigned ...
- C和指针 第七章 习题
7.1 hermite递归函数 int hermite(int n, int x) { if (n <= 0) { return 1; } if (n == 1) { return 2 * x; ...
- C和指针 第五章 位数组
5.4的习题:编写一组函数,实现维数组,函数原型如下: //指定位设置为1void set_bit(char bit_array[], unsigned bit_number); //指定位清零 vo ...
随机推荐
- docker-compose启动的tomcat无法远程连接jmx
最近想学习下java GC优化,就用了一下VisualVM,在远程服务器启动了一个非docker的tomcat,很顺利的就连接了,但是用docker-compose启动的服务却 怎么也连不上,一定是d ...
- JAVA中int、String的类型转换
int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...
- Win10系统出问题?简单一招即可修复win10!
时至今日,win10系统的普及率越来越高,在微软多种策略的强推下,10月份win10系统的市场份额已达22.59%,但win10系统也不是完美的,总有些还是会出现一些诸如打开应用程序出现闪退.乱码.总 ...
- 《Java程序设计》 课程教学
<Java程序设计> 课程教学 给学生 考核方式 100分构成 翻转课堂考核12次(5*12 = 60):每次考试20-30道题目,考试成绩规格化成5分(比如总分20分就除以4) 注意:不 ...
- npoi批量导入实现及相关技巧
批量导入功能对于大部分后台系统来说都是不可或缺的一部分,常见的场景-基础数据的录入(部门,用户),用批量导入方便快捷.最近项目需要用到批量导入,决定花点时间写套比较通用的Excel导入功能.经过考虑, ...
- php面向对象编程(一)
类与对象关系: 类就像一个人类的群体 我们从类中实例化一个对象 就像是制定一个人. 面向对象程序的单位就是对象,但对象又是通过类的实例化出来的,所以我们首先要做的就是如何来声明类, 做出来一个类很容易 ...
- LocalBroadcastManager 的实现原理,Handler还是 Binder?
原文: http://www.trinea.cn/android/localbroadcastmanager-impl/ 对 LocalBroadcastManager 大家应该都不陌生,相对 Bro ...
- NSUserDefaults:熟悉与陌生(转)
转载自:http://swiftcafe.io/2016/04/04/nsuserdefaults/?hmsr=toutiao.io&utm_medium=toutiao.io&utm ...
- cocos2d-x打飞机实例总结(一):程序入口分析和AppDelegate,Application,ApplicationProtocol三个类的分析
首先,是个敲代码的,基本上都知道程序的入口是main函数,显然,就算在cocos2d-x框架中也一样 我们看看main函数做了什么 #include "main.h" #inclu ...
- Code[VS] 3123 高精度练习之超大整数乘法
FFT 做 高精度乘法 #include <bits/stdc++.h> ); struct complex { double a, b; inline complex( , ) { a ...