I have the following loop:
for (byte i = 0 ; i < 128; i++) {
System.out.println(i + 1 + " " + name);
}
When I execute my programm it prints all numbers from -128 to 127 in an infinite loop. Why does this happen?
I have the following loop:
for (byte i = 0 ; i < 128; i++) {
System.out.println(i + 1 + " " + name);
}
When I execute my programm it prints all numbers from -128 to 127 in an infinite loop. Why does this happen?
Best is if you do
Alright, so the reason behind this has been answered already, but in case you were interested in some background:
A
bit
is the smallest unit of storage which the computer can recognize (n.b. not the smallest number). A bit is either a0
or a1
.A
byte
is an 8 bit data type, meaning it is composed of 8 bit strings such as10101010
or0001110
. Using simple combinatorics, we know that there are2^8 = 256
possible combinations of bytes.If we wanted to only represent positive numbers, we could do a straight conversion from base 2 to base 10. The way that works is, for a bit string
b7b6b5b4b3b2b1b0
the number in decimal isdec = sum from n=0 to 7 of (bn * 2^n)
.By only representing positive numbers ( an
unsigned byte
) we can represent 256 possible numbers in the range0
to255
inclusive.The problem comes in when we want to represent signed data. A naive approach (n.b. this is for background, not the way java does it) is to take the left most bit and make it the sign bit where
1
is negative and0
is positive. So for example00010110
would be21
and10010110
would be-21
.There are two major problems with such a system. The first is that
00000000
is0
and10000000
is-0
, but as everyone knows, there is no-0
that is somehow different from0
, but such a system allows for the number and0 ≠ -0
. The second problem is that, due to representing two zeroes, the system only allows for representing numbers from-127
to127
, a range of only254
(2
less than before).A much better system (and the one which most systems use) is called Two's Compliment. In Two's Compliment, the positive numbers are represented with their normal bit string where the leftmost bit is 0. Negative numbers are represented with the left most bit as a 1 and then calculating the two's compliment for that number (from whence the system gets its name)
Although mathematically it is a slightly more complex process, because we are dealing with the number
2
there are some short cuts. Essentially, you can take the positive version and (from right to left) take all zeroes until you hit a 1. Copy those zeroes and one, then take theNOT
of the rest of the bits. So for example, to get-21
, positive21
is00010110
we take the10
and not the rest to get11101010
, the two's compliment representation of-21
.Two's Compliment is a much more difficult system to grasp, but it avoids the previously stated problems, and for an n-bit number can represent all digits from
-2^(n-1)
to2^(n-1)-1
which for our byte means-128
to127
(hence the problem in this question)A couple of notes:
- This is for integer representation only. Real number representation is another system entirely (if there is a request for it, I'm sure we could make a number representation CW post)
- Wikipedia has a couple more number representation systems if you're interested.
After 127, when it increments, it will become -128, so your condition won't match .
It will work like this:
as 8 bits can represent a signed number up to 127.
See here for the primitive data types.
Picture says more than words
byte is a 1-byte type so can vary between -128...127, so condition i < 128 is always true. When you add 1 to 127 it overflows and becomes -128 and so on in a (infinite) loop...
because when i == 127 and and you executes i++ it overflows to -128.
Because bytes are signed in Java so they will always be less than 128.
Why Java chose signed bytes is a mystery from the depths of time. I've never been able to understand why they corrupted a perfectly good unsigned data type :-)
Try this instead:
or, better yet: