深入理解计算机系统 练习题2.27 答案与分析

无符号数加法

#include <stdio.h>  
#include "stdafx.h"
#include <iostream>


using namespace std;

int uadd_ok(unsigned x,unsigned y) {
    cout << x << endl;
    cout << y << endl;
    unsigned result = x + y;
    return result >= x;
}
int main() {
    unsigned x = 1;
    unsigned y = 4294967295;
    cout << uadd_ok(x, y)<<endl;

}

如果s没有溢出,我们能够肯定s>=x。另一方面,如果s确实溢出了,我们就有s=x + y - 2 w 。假设y< 2 w ,我们就有y - 2 w < 0,因此s = x + (y - 2 w ) < x.

猜你喜欢

转载自blog.csdn.net/ciqingloveless/article/details/82743985