• 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

short or int?

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's wrong with the following ?
short s=9;
short p=s+9;
why s+9 is integer (since storage is 16bits for short)
i am confused,please help
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java is doing that to you on purpose. Java converts the types of the operands in arithmatic expressions. For example, if you add a long and an int, the result is a long because Java makes sure that both operands are promoted to long before doing the math. Operands of type short or byte are both automatically converted to int before performing the operation. That is why your result is an int. You could solve this problem by casting your result before setting the variable. For example:

<PRE>
short a = 1;
short b = (short)(a + 1);
</PRE>
Hope this helps.
Nick
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Nick!
i am little confused with the fundas of shifting bits.specially for negative integers.'
for example -1 is written as:111111110,is it appropriate to write it in integer form as 11111111 11111111 11111111 11111110
because we add 1's at higher order bits for a negative number.please help if i am wrong.
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in continuation of my previous problem what happens to
-64>>4 instead of doing -64>>>4
please explain.
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry...
-1 is written as 11111111 is it correct?
now -1>>4 or -1>>5 or in general -1>>i will always give us -1.it it correct?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes its true that -1 is 11111111111111111111111111111111 in binary, so in any case-1>>i will give -1 because when it gives a right shift,again it remains samei.e 11111111111111111111111111111111

Originally posted by nachiket deshpande:
sorry...
-1 is written as 11111111 is it correct?
now -1>>4 or -1>>5 or in general -1>>i will always give us -1.it it correct?



[This message has been edited by Niti Gupta (edited October 31, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic