Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ~ operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "~ operator" Watch "~ operator" New topic

~ operator


Joined: Dec 26, 2000
Posts: 5
Does Java allow you to use ~ operator for integer type variables?
Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133
Yes. Just test it out:
public class Try {
public static void main(String args[]){
int a=3;
output is -4( the same if you change a to byte, char, short and long ).

------------------------<BR>Sun Certified Java Programmer<BR>----------------------------------
sabyasachi mondal
Ranch Hand

Joined: Dec 26, 2000
Posts: 33
Thank you Tom. It's simple but mostly overlooked.

Joined: Dec 26, 2000
Posts: 5
Thanks Tom,
So ~ operator works with any integral types as opposed to floating types!!
Is that right?
Sivaram Ghorakavi
Ranch Hand

Joined: Nov 30, 2000
Posts: 56
TIP: ~i = (-i) -1
I agree. Here's the link:
subject: ~ operator
Similar Threads
can any one explain this
Picking variables with a given propability
Iterations - finding the same value for 2 variables
Interview Question
convert Postiive number to negative..