Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

integer range

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can somebody explain why i am getting -727379968 as the product?
Thanks
Madan
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
integer range -2147483648 2147483647
 
Madan, Gopal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daniel:
I know the integer range (-2,147,483,648 to 2,147,483,647) cannot accomodate product of 1 million by 1 million (1000,000,000,000).
My question is how I got -727,379,968
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


My question is how I got -727,379,968


From JLS �15.17.1:


If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as
represented in some sufficiently large two's-complement format. As a result, if overflow occurs, then the
sign of the result may not be the same as the sign of the mathematical product of the two operand values.


1000000*1000000 results in an overflow. The lower-order
32 bits (int is 32 bits) are 0xd4a51000 which is -727,379,968.
[This message has been edited by Nain Hwu (edited October 09, 2001).]
 
Madan, Gopal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nain:
When i fed the Hex # you said is the low-order 32 bits in NT calc. I am getting 3567587328 and not the number you said/I asked. Also, can you explain/write down how you got 0xD4A51000.
Thanks
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madan,
I used the toHexString() method in class Integer.
Here is what I did:

Here is the output:


D:\pfe101i>java Test
d4a51000
-727379968


Notice that 0xd4a51000 is a negative integer value (in java,
integer is stored as two complement value).
Looks like NT calc treats it as a positive value, which gives
you 3567587328.
Hope this help.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic