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
• Number of slices to send:
Optional 'thank-you' note:
Hello,

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

results in the output -45679

Why is this. What is this operator called.

Ray

author & internet detective
Posts: 41403
853
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Brilliant, thanks. This is a great place!

Ray

Saloon Keeper
Posts: 26267
186
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
~ 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