• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Primitive Casting

 
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks ,,
when we cast a long value to a byte which is larger than 127 (the largest number a byte can store)
you dont get a run time error and you will get a number ,, my question is : is it neccessary to know how the number was calculated ? if yes please explain

as shown in the below example


the output will be -126,,,please could you tell me how the conversion is done from 130 to -126?
 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

java language specification 5.1.3 wrote:A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.



For an understanding of how integers are stored read this.
 
Ranch Hand
Posts: 808
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hama,

Imagine the possible values of a byte variable like the face of a clock. At the top of the clock, where 12 would be, there is the value zero. Imagine that moving clockwise the values increase until we reach 127. Imagine that moving counter-clockwise from zero the values are negative, and decrease until we reach -128.

These two values, -128 and 127, are next to each other at the bottom of the clock face where the 6 would be.

Now imagine your long value to be a piece of string, or a rope of licorice or fruit leather -- something that is rolled up and can be unrolled. The length of the rolled-up long is its value, and the units are the same as the units on the clock face. We are going to unroll the long, along the circumference of the clock, starting at zero and moving clockwise. If the value is 127 or lower, then we do not run out of positive values on the clock face before we have unrolled the long value, and then we don't see a difference in the value of the byte after casting.

BUT

If the value is greater than 127, guess what? We just keep unrolling around the edge of the clock, up into the negative values! We started with the value of 130, and now we have used up the positive values from 1 to 127. Now we will move from -128 toward zero. We have 3 units left to account for, so we end up at -126.
 
Piyush Joshi
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
great explanation Dennis
 
Ranch Hand
Posts: 206
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to what Dennis left off, the calculation simply taking the difference between 130L and -128, and from -128 count or subtract 2. Anything less than or equal to 127 is a byte value. Let's convert the analogy anything or value greater than 127 to mathematical formula for easy to understand, and find out the value quickly and accurately.

For example,

Let's X represent any primitive value.
Let's Y represent the maximum negative primitive value.
Let's Z represent the difference value between X and Y.
Let's R represent the result of (-)Y - Z.

(-) represent negative sign exclude from calculation.

So we come up with a formula:

1) X diff (-)Y = Z

2) (-)Y - Z = (-)R

Formula in action:






 
My, my, aren't you a big fella. Here, have a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic