Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

~ operator

 
sthorat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Java allow you to use ~ operator for integer type variables?
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Just test it out:
public class Try {
public static void main(String args[]){
int a=3;
System.out.println(~a);
}
}
output is -4( the same if you change a to byte, char, short and long ).
 
sabyasachi mondal
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tom. It's simple but mostly overlooked.
 
sthorat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tom,
So ~ operator works with any integral types as opposed to floating types!!
Is that right?
 
Sivaram Ghorakavi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TIP: ~i = (-i) -1
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic