前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >计算机组成原理变形补码计算题_原码反码补码例题详解

计算机组成原理变形补码计算题_原码反码补码例题详解

作者头像
全栈程序员站长
发布2022-09-27 11:24:29
9660
发布2022-09-27 11:24:29
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君

22. 已知 x 和 y,用变形补码计算 x-y,同时指出结果是否溢出。 (1) x=11011,y=-11111 (2) x=10111,y=11011 (3) x=11011,y=-10011

24. 已知 x 和 y,用变形补码计算 x+y,同时指出结果是否溢出。 (1)x=11011,y=00011 (2)x=11011,y=-10101 (3)x=-10110,y=-00001

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int question = 0;
string fu(string str) {
    if (str[0] == '0') {
        str.replace(0, 2, "11");
    } else {
        str.replace(0, 2, "00");
    }
    return str;
}
string fanMa(string str) {
    for (int i = 2; i < str.length(); i++) {
        if (str[i] == '0') {
            str[i] = '1';
        } else {
            str[i] = '0';
        }
    }
    return str;
}
// 正数的原码反码补码都一样, 负数补码是反码+1
string buMa(string str) {
    if (str[0] == '1') {
        str = fanMa(str);
        int flag = 1; // 标记要进位1
        for (int i = str.length() - 1; i >= 0; i--) {
            if (str[i] == '0') {
                if (flag) {
                    flag = 0;
                    str[i] = '1';
                }
            } else if (str[i] == '1') {
                if (flag) {
                    str[i] = '0';
                }
            }
        }
    }
    return str;
}
string minus1(string str) {
    // 相当于找到最后一个1变成0,最后一个1前的0都变成1
    int flag = -1;
    for (int i = str.length() - 1; i >= 0; i--) {
        if (str[i] == '0') {
            if (flag) {
                str[i] = '1';
            }
        } else if (str[i] == '1') {
            if (flag) {
                flag = 0;
                str[i] = '0';
            }
        }
    }
    return str;
}
// 从补码转到原码
string B2Y(string str) {
    string res;
    if (str[0] == str[1]) {
        if (str[0] == '0') {
            res = " +";
            res += str.substr(2);
        } else {
            res = " -";
            res += minus1(str.substr(2));
            res = fanMa(res);
        }
    } else {
        if (str[0] == '0') {
            res = "+";
            res += str.substr(1);
        } else {
            res = "-";
            res += minus1(str.substr(1));
            res = fanMa(res);
        }
    }
    return res;
}
void checkOverFlow(string str) {
    if (str.substr(0, 2) == "01") {
        cout << "正溢出" << endl;
    } else if (str.substr(0, 2) == "10") {
        cout << "负溢出" << endl;
    } else {
        cout << "未溢出" << endl;
    }
}
// 输入X和Y的原码, 计算结果
string Cal(string X, string Y, char oper = '+') {
    printf("\n(%d)\n", ++question);
    X = buMa(X);
    cout << "[X]补:   " << X << endl;
    if (oper == '-') {
        Y = fu(Y);
    }
    Y = buMa(Y);
    if (oper == '-') {
        cout << "[-Y]补:  " << Y << endl;
    } else {
        cout << "[Y]补:   " << Y << endl;
    }
    int flag = 0; // 标记是否要进位
    for (int i = X.length() - 1; i >= 0; i--) {
        int cur = X[i] - '0' + Y[i] - '0' + flag;
        if (cur > 1) {
            flag = 1;
        } else {
            flag = 0;
        }
        if (cur & 1) {
            X[i] = '1';
        } else {
            X[i] = '0';
        }
    }
    printf("------------------\n");
    cout << "[X" << oper << "Y]补: " << X << endl;
    cout << " X" << oper << "Y   : " << B2Y(X) << endl;
    checkOverFlow(X);
    return X;
}

int main() {
    system("chcp 65001");
    cin.tie(0);
    cout.tie(0);
    // bitset<7> a(27);
    // string strX = a.to_string();
    Cal("0011011", "0000011");
    Cal("0011011", "1110101");
    Cal("0011011", "1110011", '-');
    return 0;
}
计算机组成原理变形补码计算题_原码反码补码例题详解
计算机组成原理变形补码计算题_原码反码补码例题详解

nt布置你马的那么多题目…

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/179210.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档