• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

~ Operatior

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

int i = 45678;
int j = ~i;
System.out.println(j);

results in the output -45679

Why is this. What is this operator called.

Many thanks in advance.

Ray
 
author & internet detective
Posts: 41403
853
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ray,
It's called the "bitwise complement" operator. If you represent your number as bits (in binary), it turns all the 0s into 1's and 1's into 0's.
 
Raymund Beyer
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brilliant, thanks. This is a great place!

Ray
 
Saloon Keeper
Posts: 26267
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Ray,
If you represent your number as bits (in binary), it turns all the 0s into 1's and 1's into 0's.



Including the sign bit. But you can tell the difference between systems that use 2's complement negative numbers and 1's complement, because the absolute numeric value would be off by 1 in the other case!
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
~ operator is bitwise inversion operator which is not on the exam if you have enrolled for scjp-SE 6

but remember this....
~n=-(n+1)
 
Let nothing stop you! Not even this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic