+ 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.

20th Apr 2019, 2:32 PM
Ayush Pandey
+ 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.
20th Apr 2019, 3:09 PM
HonFu
+ 3
HonFu thanks bro Had similar thoughts but wanna verified.
20th Apr 2019, 3:52 PM
Ayush Pandey
+ 2
Dint get u?
20th Apr 2019, 3:57 PM
Ayush Pandey
+ 2
while(by>0) { ++by; printf("%d", by); } Like this, you can examine what happens.
20th Apr 2019, 4:02 PM
HonFu
+ 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.
20th Apr 2019, 3:56 PM
HonFu
- 1
HonFu u were too in pythonist grp right?
20th Apr 2019, 3:55 PM
Ayush Pandey