+ 2
Can u plz explain how's the output '-128' A challenge Q&A of sololearn quiz
The question posted was as follows byte by=1; while(by>0) b++; System.out.print(by); The right output given was '-128' Can u plz explain the logic behind it.
6 Respostas
+ 6
Every number type has a limit to what it can hold.
A byte can hold the numbers from -128 to 127.
So if you add 1, after a while the byte 'overflows'.
Then it starts over from the bottom end and now is < 0.
+ 3
HonFu thanks bro Had similar thoughts but wanna verified.
+ 2
Dint get u?
+ 2
while(by>0) {
++by;
printf("%d", by);
}
Like this, you can examine what happens.
+ 1
A good way to verify that sort of thing (also to debug short codes):
Rewrite the code, but output the values!
That way you can exactly follow what happens step by step.
- 1
HonFu u were too in pythonist grp right?