您的位置:首页 > 理论基础

计算机系统的初步学习(持续更新)--关于replace_byte()函数

2017-09-19 22:03 260 查看
知识整理:

                 visual studio   c++输出16进制数:cout<<setbase(16)<<...//需要调用函数iomanip

                                               输入16进制数:scanf(”%x“)//浪费了我好多时间


                 关于此程序:需要注意unsigned char 的大小,容易溢出,在编译时vs报错提醒溢出

深入学习计算机系统家庭作业2.60

代码如下

#include "stdafx.h"

#include<iostream>

#include<iomanip>

using namespace std;

unsigned replace_byte(unsigned int  x, int i, unsigned int b);

int main()

{

    unsigned int x;

    int i;

    unsigned int b;

    scanf("%x %d %x", &x,&i,&b);

    unsigned int y= replace_byte(x, i, b);;

    cout <<"0x"<< setbase(16) << y;

    //replace_byte(x, i, b);

}

unsigned  replace_byte(unsigned int x, int i, unsigned int b) {

    //printf("0x%x", (x&(~(0x000000FF << (i << 3)))) | (b << (i << 3)));

    return (x&(~(0x000000FF << (i << 3)))) | (b << (i << 3));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐