+ 3

Can u explain me logic behind how the output came?

I would like to know as if i change the value from 130 to 123,or any number than what internally is going on and the output came by the compiler. https://code.sololearn.com/c0bsX3E9wf0f/?ref=app

10th Feb 2020, 5:44 PM
Piyush Srivastava
Piyush Srivastava - avatar
5 Answers
+ 4
First understand how the range thing works before you understand the code. https://stackoverflow.com/questions/3621067/why-is-the-range-of-bytes-128-to-127-in-java/42623517
10th Feb 2020, 6:08 PM
Avinesh
Avinesh - avatar
+ 4
byte has a range from -128 to 127. So anything beyond 127 will again start from -128. If you give 130 then the result will be 127+3 = -126.
10th Feb 2020, 6:00 PM
Avinesh
Avinesh - avatar
+ 3
Got it avinesh thank u
10th Feb 2020, 6:16 PM
Piyush Srivastava
Piyush Srivastava - avatar
+ 2
Yes sir internally what thing is happening is that their we have to find the 2's complements of the bits leaving most significant one ☝. Its a better way to remember it in a way like -x-1😅
10th Feb 2020, 6:39 PM
Piyush Srivastava
Piyush Srivastava - avatar
+ 2
b++ 01111101 125 01111110 126 01111111 127 10000000 -128 10000001 -127 10000010 -126
10th Feb 2020, 7:08 PM
zemiak