This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Java Explicit Casting.  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!

Could you please help me to understand the following:



I was expected 127 value. Why the result is -121?

Thank you!
 
author & internet detective
Posts: 38925
686
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madalin,
Welcome to CodeRanch!

5+30 is 135. However, this is above the maximum value of byte. So Java wraps around to the minimum value of byte rather than stopping at the maximum value.
 
Madalin Popa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Thank you very much for your answer.

Is this behaviour expected for all primitives?

 
Jeanne Boyarsky
author & internet detective
Posts: 38925
686
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Madalin Popa wrote:Is this behaviour expected for all primitives?


Nope. Give writing code to prove/disprove this a shot. I think you'll find it interesting. (And memorable)
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it cuts the overflowing ammount and adds it to the minimum value. Try also this:
Code:
 
Marshal
Posts: 61805
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Note you were using 131 as the second term in that addition, and the original post said 130. The results follow the usual rules about arithmetic overflow errors in two's complement arithmetic.
 
Campbell Ritchie
Marshal
Posts: 61805
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens for such overflow is this:-The bit₂₄ of the int becomes the bit₀ of the byte and (this isn't an accurate description of complementary arithmetic) becomes equivalent to −128 in decimal. A bit₀ in a two's complement number is always negative.
 
Dimitrios Tosidis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are absolutely right.
I was referring to  the Number Circle for Two's Complement numbers.
When you are adding 1 to the maximum value then you go to the minimum value and the opposite.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
135 is outside the range of a byte which is -128 to 127. Instead the top bit of the 8-bit value is treated as the signed which indicates it is negative in this case. So the number is 135 - 256 = -121.
 
Campbell Ritchie
Marshal
Posts: 61805
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
The bit₇ in an 8‑bit two's complement number isn't a true sign bit. Otherwise you would have 1010_1010 = −42 and 0010_1010 = 42. The bit₇ has a value of −128, so 1010_1010 = −86, 0010_1010 = 42, and 1000_0000 = −128.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!