• 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

Question about ~ operator

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone shed some light on this behavior??

short i =0;
i>>=i; //compiles fine, an int is implicitly converted to a short here
i~=i; //throws a compiler error, explict casting is required here

Why?
TIA
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, there is no such operator as ~=. ~ is the Unary NOT operator, which means that it only functions on one operand. The compound assignment operators are designed to be shorthand notations of Binary Operators, such as +, -, >>, etc.
 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i get it, thanks
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sudhakar Krishnamurthy:
Can anyone shed some light on this behavior??

short i =0;
i>>=i; //compiles fine, an int is implicitly converted to a short here
i~=i; //throws a compiler error, explict casting is required here

Why?
TIA



I changed the code slightly as below:

The compiler indicates that it will not compile because of a possible loss of precision. This makes sense because you are trying to put an integer value (32 bits) into a short value (16 bits). Remember that the value of the short variable,i, will be promoted to an integer before applying the unary operator. Thus, an explicit cast is required. This can result in some strange results when attempting this operation.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic