我正在尝试编写两个函数,以仅使用!~ | & ^ +检查/防止C中的溢出,但无法实现。第一个函数是fitsB(int x, int n),用于检查特定的二进制补码/有符号整数是否适合使用某个位数:其中x是整数,n是要使用的位数。还有一个函数overflowInt(int x, int y),用于检查两个整数相加时是否会发生溢出。如果它们是无符号整数,我可以做到,但负数会使事情变得更加困难。有人知道如何解决吗?
此外,没有强制转换,整数始终为32位。
此外,没有强制转换,整数始终为32位。
/*
* addOK - Determine if can compute x+y without overflow
* Example: addOK(0x80000000,0x80000000) = 0,
* addOK(0x80000000,0x70000000) = 1,
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int addOK(int x, int y) {
// Find the sign bit in each word
//if a and b have different signs, you cannot get overflow.
//if they are the same, check that a is different from c and b is different from c,
// if they are the same, then there was no overflow.
int z=x+y;
int a=x>>31;
int b=y>>31;
int c=z>>31;
return !!(a^b)|(!(a^c)&!(b^c));
}
sizeof(int)*8-1
对我来说比 31
更加清晰。 - ivan_pozdeev如果 x < 2^(n-1),那么 x 就可以适应 n 位。
溢出问题需要更多信息。如果将两个 int 分配给 long(或 double),它们就不会溢出。
为了确定加法是否会溢出,如果您拥有最大值,则 x + y <= 最大值。通过使用代数,我们可以得到 y <= 最大值 - x。然后,您可以比较传入的 y 值,如果它不满足条件,则加法会溢出。例如,如果 x 是最大值,则 y <= 0,因此 y 必须小于或等于零,否则加法将溢出。