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
• Paul Clapham
• Ron McLeod
• Liutauras Vilda
• Junilu Lacar
Sheriffs:
• Tim Cooke
• Jeanne Boyarsky
• Knute Snortum
Saloon Keepers:
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
• Piet Souris
Bartenders:
• salvin francis
• fred rosenberger
• Frits Walraven

Primitive value

Ranch Hand
Posts: 486
public short s=(short)(90+900000);

Sheriff
Posts: 11343
90 + 900000 is 900090, which is a 32-bit int. In binary, this is:

0000 0000 0000 1101 1011 1011 1111 1010

When cast to a 16-bit short, this is:

1011 1011 1111 1010

Ranch Hand
Posts: 59
Overflow?

gives..

hope this helps.

Shiva Mohan
Ranch Hand
Posts: 486
Hi,
Thanks for the reply.I was working on the above problem.When i do binary to decimal conversion of 16 bit of short

marc weber
Sheriff
Posts: 11343
For signed integral primitives (byte, short, int, and long), a leading 1 indicates a negative value. To determine the quantity, you "flip the bits" (switch all the zeros and ones), then add 1.

1011101111111010 <== Original value

0100010000000101 <== Flipped bits

Does this help?
[ May 09, 2006: Message edited by: marc weber ]

Shiva Mohan
Ranch Hand
Posts: 486
Marc-Thanks for the reply.It helped me to understand.

marc weber
Sheriff
Posts: 11343
See Two's Complement for more detail.

 pie. tiny ad: Two software engineers solve most of the world's problems in one K&R sized book https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton