我在C中提出了這個相對較短的解決方案,該解決方案處理從INT_MIN
到INT_MAX
的整個範圍的整數。
它期望有符號整數實現爲2的補碼,並且它不會受到有符號溢出的不良影響(已知會導致未定義的行爲)。
#include <stdio.h>
#include <limits.h>
int isGreater(int x, int y)
{
// "x > y" is equivalent to "!(x <= y)",
// which is equivalent to "!(y >= x)",
// which is equivalent to "!(y - x >= 0)".
int nx = ~x + 1; // nx = -x (in 2's complement integer math)
int r = y + nx; // r = y - x (ultimately, we're interested in the sign of r,
// whether r is negative or not)
nx ^= nx & x; // nx contains correct sign of -x (correct for x=INT_MIN too)
// r has the wrong sign if there's been an overflow in r = y + nx.
// It (the r's sign) has to be inverted in that case.
// An overflow occurs when the addends (-x and y) have the same sign
// (both are negative or both are non-negative) and their sum's (r's) sign
// is the opposite of the addends' sign.
r ^= ~(nx^y) & (nx^r); // correcting the sign of r = y - x
r >>= (CHAR_BIT * sizeof(int) - 1); // shifting by a compile-time constant
return r & 1; // return the sign of y - x
}
int testDataSigned[] =
{
INT_MIN,
INT_MIN + 1,
-1,
0,
1,
INT_MAX - 1,
INT_MAX
};
int main(void)
{
int i, j;
for (j = 0; j < sizeof(testDataSigned)/sizeof(testDataSigned[0]); j++)
for (i = 0; i < sizeof(testDataSigned)/sizeof(testDataSigned[0]); i++)
printf("%d %s %d\n",
testDataSigned[j],
">\0<=" + 2*!isGreater(testDataSigned[j], testDataSigned[i]),
testDataSigned[i]);
return 0;
}
輸出:
-2147483648 <= -2147483648
-2147483648 <= -2147483647
-2147483648 <= -1
-2147483648 <= 0
-2147483648 <= 1
-2147483648 <= 2147483646
-2147483648 <= 2147483647
-2147483647 > -2147483648
-2147483647 <= -2147483647
-2147483647 <= -1
-2147483647 <= 0
-2147483647 <= 1
-2147483647 <= 2147483646
-2147483647 <= 2147483647
-1 > -2147483648
-1 > -2147483647
-1 <= -1
-1 <= 0
-1 <= 1
-1 <= 2147483646
-1 <= 2147483647
0 > -2147483648
0 > -2147483647
0 > -1
0 <= 0
0 <= 1
0 <= 2147483646
0 <= 2147483647
1 > -2147483648
1 > -2147483647
1 > -1
1 > 0
1 <= 1
1 <= 2147483646
1 <= 2147483647
2147483646 > -2147483648
2147483646 > -2147483647
2147483646 > -1
2147483646 > 0
2147483646 > 1
2147483646 <= 2147483646
2147483646 <= 2147483647
2147483647 > -2147483648
2147483647 > -2147483647
2147483647 > -1
2147483647 > 0
2147483647 > 1
2147483647 > 2147483646
2147483647 <= 2147483647
+1 [德·摩根定律(http://en.wikipedia.org/wiki/De_Morgan's_laws):) – ulmangt 2012-04-14 04:52:33
這是完美的謝謝! – Guambler 2012-04-14 05:00:49