下列输出的值:

#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. Bootstrap 按钮

    本章将通过实例讲解如何使用 Bootstrap 按钮.任何带有 class .btn 的元素都会继承圆角灰色按钮的默认外观.但是 Bootstrap 提供了一些选项来定义按钮的样式,具体如下表所示: ...

  2. POJ2001Shortest Prefixes[Trie]

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17683   Accepted: 768 ...

  3. UNITY 之FixedUpdate

    这个机制的加入 比 AS3好了很多 AS3的EnterFrame相当于UNITY的Update 但是FLASH做不了也是因为浏览器的限制吧! Here's how the fixed time ste ...

  4. [No000074]C#创建桌面快捷方式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. PAT 1039. 到底买不买(20)

    小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子 ...

  6. PyQt4入门

    PyQt4入门教程(6)_对话框 文中译者的话将用方括号[]标出.对话框(Dialogs)是现代GUI程序中不可缺少的一部分.对话本来指的是两个或者更多人之间的交流,而在计算机应用中,对话是一个可以让 ...

  7. TCP/IP协议中网关和子网掩码概念

    网关: 不同网段的IP是不能直接互通的,需要一个设备来转发,这个设备就是网关,一般就是路由器,那么路由器的地址就是网关地址. 比如192.168.2.31要往192.168.3.31发送一条消息,他们 ...

  8. Linux -- CentOS7修改防护墙端口

    CentOS升级到7之后,发现无法使用iptables控制Linuxs的端口,原因是Centos 7使用firewalld代替了原来的iptables.下面记录如何使用firewalld开放Linux ...

  9. 修改httpd默认端口号

    Tomcat: vim /etc/httpd/conf/httpd.conf//别忘了service httpd restart Nginx: vim /etc/nginx/nginx.conf//完 ...

  10. vim2

    一.光标控制命令   命令                   移动    k                   向上移一行    j                   向下移一行    h    ...