下列输出的值:

#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和指针 第五章 习题的更多相关文章

  1. 《学习Opencv》第五章 习题6

    这是第五章 习题5.6的结合版,其中实现了摄像头抓拍功能,能够成功运行. #include "stdafx.h" #include "cv.h" #includ ...

  2. 统计学习导论:基于R应用——第五章习题

    第五章习题 1. 我们主要用到下面三个公式: 根据上述公式,我们将式子化简为 对求导即可得到得到公式5-6. 2. (a) 1 - 1/n (b) 自助法是有有放回的,所以第二个的概率还是1 - 1/ ...

  3. C和指针 第六章 习题

    6.1编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集中出现的字符,返回第一个找到的字符位置指针,未找到返回NULL #include <stdio.h> char * f ...

  4. C和指针 第十五章 习题

    15.8 十六进制倾印码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

  5. C和指针 第十七章 习题

    17.8 为数组形式的树编写模块,用于从树中删除一个值,如果没有找到,程序节点 ArrayBinaryTree.c // // Created by mao on 16-9-18. // #inclu ...

  6. C和指针 第十三章 习题

    1,1标准输入读入字符,统计各类字符所占百分比 #include <stdio.h> #include <ctype.h> //不可打印字符 int isunprint(int ...

  7. C和指针 第十一章 习题

    1编写calloc,内部使用malloc函数获取内存 #include <stdio.h> #include <stdlib.h> void *myAlloc(unsigned ...

  8. C和指针 第七章 习题

    7.1 hermite递归函数 int hermite(int n, int x) { if (n <= 0) { return 1; } if (n == 1) { return 2 * x; ...

  9. C和指针 第五章 位数组

    5.4的习题:编写一组函数,实现维数组,函数原型如下: //指定位设置为1void set_bit(char bit_array[], unsigned bit_number); //指定位清零 vo ...

随机推荐

  1. 苹果iOS强制HTTPS迫在眉睫,距离2017年只剩1天,准备好了么?

    其实不久前苹果就发了通告,要求ios上的应用全部以HTTPS来进行接口调用以及数据访问,这样做是为了数据安全,一方面为了自己,另一方面也是对应用的要求更加严格,这么做很好,也加强了市场app的规范,虽 ...

  2. linux ubuntu14 更改为root用户登录

    设置使用root用户登陆首先要修改root的登陆密码sudo passwd root1.输入sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu ...

  3. 代码覆盖工具(gcov、lcov)的使用

    一.安装 gcov:是随gcc一起发布的,并不需要独立安装:lcov:其他博客说是随ltp发布的,结果下载下ltp之后编译了10多分钟,最后也没见lcov,最后到sourceforge下载了lcov单 ...

  4. ASP.NET(C#) Web Api通过文件流下载文件到本地实例

    下载文件到本地是很多项目开发中需要实现的一个很简单的功能.说简单,是从具体的代码实现上来说的,.NET的文件下载方式有很多种,本示例给大家介绍的是ASP.NET Web Api方式返回HttpResp ...

  5. php 异常处理类

    PHP具有很多异常处理类,其中Exception是所有异常处理的基类. Exception具有几个基本属性与方法,其中包括了: message 异常消息内容code 异常代码file 抛出异常的文件名 ...

  6. WebForm路由踩坑 ajax请求多次

    WebForm路由踩坑 再次接触Asp.Net WebForm已是4年后的今天,源起新入职的公司,一个老的项目. Web接触的少,那就多动手写写. WebForm1.aspx <body> ...

  7. 关于DOS与cmd(windows系统)

    dos是计算机的最初期的操作系统,对电脑操作必须输入各种dos命令窗口,可以理解成运行计算机机器内部语言,知道编程吗?其实早期dos命令操作系统就是运行计算机内部的编程命令,因此操作人员都必须具有一定 ...

  8. 如何在个人博客引擎 Hexo 中添加 Swiftype 搜索组件

    在您现在看到的我的博客站点,后台使用的是 Hexo 作为博客引擎,但是默认集成的搜索组件是进行 form 提交到 Google 进行搜索的,为了更好地体验,本文介绍如何在 Hexo 博客中集成 Swi ...

  9. 回文自动机(BZOJ2565)

    #include <cstdio> #include <cstring> #include <iostream> using namespace std; ][], ...

  10. SQL联合查询(内联、左联、右联、全联)的语法(转)

    最近在做一个比较复杂的业务,涉及的表较多,于是在网上找了一些sql联合查询的例子进行研究使用. 概述: 联合查询效率较高,举例子来说明联合查询:内联inner join .左联left outer j ...