如果你說的是在循環結束時爲-1;這僅僅是因爲int
已經簽名,並且它包裹了。
int foo = 1;
while (foo >= 0) {
foo += foo + 1;
}
System.out.println(foo);
將要求output -1。你可以trace through it:
foo += 1 + 1 ---> 3
foo += 3 + 1 ---> 7
foo += 7 + 1 ---> 15
foo += 15 + 1 ---> 31
foo += 31 + 1 ---> 63
foo += 63 + 1 ---> 127
foo += 127 + 1 ---> 255
foo += 255 + 1 ---> 511
foo += 511 + 1 ---> 1023
foo += 1023 + 1 ---> 2047
foo += 2047 + 1 ---> 4095
foo += 4095 + 1 ---> 8191
foo += 8191 + 1 ---> 16383
foo += 16383 + 1 ---> 32767
foo += 32767 + 1 ---> 65535
foo += 65535 + 1 ---> 131071
foo += 131071 + 1 ---> 262143
foo += 262143 + 1 ---> 524287
foo += 524287 + 1 ---> 1048575
foo += 1048575 + 1 ---> 2097151
foo += 2097151 + 1 ---> 4194303
foo += 4194303 + 1 ---> 8388607
foo += 8388607 + 1 ---> 16777215
foo += 16777215 + 1 ---> 33554431
foo += 33554431 + 1 ---> 67108863
foo += 67108863 + 1 ---> 134217727
foo += 134217727 + 1 ---> 268435455
foo += 268435455 + 1 ---> 536870911
foo += 536870911 + 1 ---> 1073741823
foo += 1073741823 + 1 ---> 2147483647
foo += 2147483647 + 1 ---> -1
數學只是這樣工作。每次迭代的結果都是1的值,小於2的冪。我猜如果你計算出代數,你可以顯示出來。所以有意義的是,它會達到-1,這在32位有符號的int
中是2 -1。
然後,由於tom和Tomáš Zíma巧妙地指出,它被卡住,因爲-1 + -1 + 1
仍然只是-1
。
還要注意,正如tom在下面的評論中發現的那樣,無論你開始的是什麼數字,你都會打-1。這是因爲foo += foo + 1
與foo = 2 * foo + 1
相同,這實際上只是foo = (foo << 1) | 1
(左移[導致低位0],然後打開低位 - 與數字偶數時加1相同)。所以不管你從哪裏開始,在最多32個迭代(或者是多個比特)迭代之後,你最終將你的起始值從左邊移開,並用全1替換(這在二進制補碼中signed int,值是-1)。例如。有符號的8位號碼:
abcdefgh starting foo, 8 unknown bits
bcdefgh0 add to itself (or multiply by two, or left shift)
bcdefgh1 add one
...
cdefgh11 again
defgh111 and again
efgh1111 and again
fgh11111 and again
gh111111 and again
h1111111 and again
11111111 and again, now it's -1
... and for completeness:
11111110 left shift -1
11111111 add 1, it's back to -1
順便說一句,不管你開始什麼數着,你永遠不會打60(或任何偶數,對於這個問題)。你可以用一些快速代數表示:如果60 = 2 * foo + 1,則前面的foo = 59/2它已經不是一個整數;所以你永遠不會有一個整數foo這樣60 = 2 * foo + 1。
您可以通過單擊答案旁邊的複選標記來接受答案。 – 2014-10-27 23:20:42
我看不到這會導致'-1'。第一次迭代應該是3。它可能是一個無限循環,因爲它會跳過60的數字。但是可以通過說'if(foo> = bar)' – 2014-10-27 23:23:16
來解決這個問題。我不能用你的代碼在問題中重現這一點。你能解決問題到可重現的事情嗎?通常你會發現這個過程已經可以回答你的問題。 – 2014-10-27 23:25:10