判断补码加法是否溢出

题目:

在这里插入图片描述

/*
* Return 1 when x can be represented as an n-bit, 2' s-complement
* number; 0 otherwise
*Assume1<=n<=w
*/
int fits_ bits(int x,int n);

补码加法的溢出有两种情况:正溢出,负溢出

/*Determine whether arguments can be added without overflow */
int tadd_ok(int x, int y) {
	int sum = x + y;
	int neg_over = x < 0 && y < 0 && sum >= 0;
	int pos_over = x >= 0 && y >= 0 && sum < 0;
	return !neg_over && !pos_over;
}

版权声明:本文为weixin_45861496原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。