作爲一個謎題的一部分,我被要求實現一個函數來檢查兩個int是否可以一起添加而不會溢出。法律運作:! 〜&^| + < < >>。&C的奇怪行爲在C中爲&
例如對於x = 0x80000000的且y = 0x80000000的函數應該返回0,因爲它是溢出,但對於x = 0x80000000的且y = 0x70000000的結果將是1。
我的解決方案到目前爲止是:
int addOK(int x, int y) {
int mask = ~(1 << 31); // 0x7fffffff
int newX = (mask & (x >> 1)); // Shift 1 to the right to make space for overflow bit
int newY = (mask & (y >> 1));
int add = newX + newY; // Add shifted x and y - overflow bit will be the MSB
int res = (add & ~mask); // Set all bits to 0 except MSB - MSB 1 iff overflow 0 otherwise
int endRes = !res; // 0x80000000 -> 0x00000000, 0x00000000 -> 0x00000001
printf("mask %x newX %x newY %x add %x ~mask %x res %x endRes %x\n",mask, newX, newY, add, ~mask, res, endRes);
return endRes;
}
功能打印以下爲X = 0x80000000的和y = 0x80000000的:
mask 7fffffff newX 40000000 newY 40000000 add 80000000 ~mask 80000000 res 0 endRes 1
現在我的問題是,爲什麼是res
0?它應該是0x80000000,因爲add
和~mask
都是0x80000000。任何人都可以向我解釋這種行爲嗎?
不能重現:http://ideone.com/an8ocK – clcto 2014-09-22 20:04:08
你知道轉移到符號位[是壞](http://stackoverflow.com/questions/4009885/arithmetic-bit-shift-on-a -signed-integer),對嗎?你不只是使用'INT_MAX'而不是試圖自己去設計它的任何特殊原因? – WhozCraig 2014-09-22 20:05:01
好的,謝謝;這很奇怪。我不應該爲這個任務使用大於一個字節的任何常量。 – user3273046 2014-09-22 20:19:29