Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculating char+= negative byte;  RSS feed

 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone As the title screen said " no question is too small " , let me see if this one is .

So basically one of the things about += type of operators is that they do not expect an explicit conversion from types( i am generalizing of course, looking more into comparable signed to unsigned type),plus no implicit conversion to int, when used with only 2 operands. So we can have operations of a char and ( negative value) with no complains from the compiler/JVM even without explicit casts.
I am having problems calculating the result of the operation though.



This prints '?' , no matter how i change the b value it stays '?. Only in smaller byte values it changes as expected. For example b=-1 will make a ='B'; b=-2 will make a='A'; and so on.
As this obviously means that after some arbitrary value the char variable will stop decreasing so i tried with the following



Well the result is again '?'. So i have 2 questions , how is this actually calculated (i checked the bytecode and it did not show anything interesting) and why is '?' the value it stops decreasing at. '?' is 63 decimal so it has space to decrease into.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not actually the '?' char that's getting printed. Rather, it's a placeholder that indicates an unprintable value. And it's not arbitrary. Every char has an equivalent integer value since char is an integer type. 'C' is \u0043 (hex) or 67 decimal. So you can't subtract anything greater because all char integer values are positive. Search for Unicode values
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks , yes, after i posted it i checked a bit more to see if it is actually 63 , and yea its not , its overflowing. The part about having a placeholder about unprintable values is a bit surprising so ill look more into it. I mean if it was changing from ? to a box or smily face, or some other crazy character with the different negative values , it was going to be obvious, but with a constant '?' it got me into a completely different direction.

Thanks again
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I suggest you view the value of the chars with the + operator. Rather than System.out.println use this:-
System.out.printf("0x%04x%n", +a);
The + operator turns the char into an int, so you can see the number and what the result of the subtraction is. If the result is outwith the normal range of the char datatype, you will suffer an overflow error.I tried with -100 and got \uffdf; if you look up this Unicode page, you can find out what \uffdf is.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, Campbell is right, although since you're going negative, I think it's technically an underflow rather than overflow.
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just made a cast System.out.println((int)c); . Anyway , my problem was because the "unprintable character" that i saw looked like a valid one . At some moments you just disregard everything and get a tunnel vision.

Thank you both!

 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Ah yes, Campbell is right, although since you're going negative, I think it's technically an underflow rather than overflow.
I was taught that underflow occurs with floating‑point numbers when you have very small numbers and an underflow error causes the result to degenerate to 0.0. I think what we have here is still called overflow (but in the opposite direction).
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Junilu Lacar wrote:Ah yes, Campbell is right, although since you're going negative, I think it's technically an underflow rather than overflow.
I was taught that underflow occurs with floating‑point numbers when you have very small numbers and an underflow error causes the result to degenerate to 0.0. I think what we have here is still called overflow (but in the opposite direction).

I think you're right, Campbell: http://programmers.stackexchange.com/questions/32893/is-int-min-1-an-underflow-or-overflow

Although there are certainly folks who think the way I was thinking:
https://cwe.mitre.org/data/definitions/191.html
http://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!