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

what is the output?

Greenhorn
Posts: 23
• Number of slices to send:
Optional 'thank-you' note:
hello all!

Does any body know why negating 3 gives -4? If I negate 5 and print the new value, it prints 6.

---here's the code ----

public class Test
{
public static void main (String [] args)
{
byte n = 3;
n = (byte) ~n;

System.out.println(n);

}
}

---- end code ---
thanks.

rey leon
Greenhorn
Posts: 23
• Number of slices to send:
Optional 'thank-you' note:
Anyone knows the answer to the above question? Any idea would be greatly appreciated. thanks.

Ranch Hand
Posts: 113
• Number of slices to send:
Optional 'thank-you' note:
hi
~b=(-b)-1;
~3 =(-3)-1=-4
-5=(-5)-1=-6

Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
Hi V Gala,

i guess by this

-5=(-5)-1=-6

you meant...
~5 = (-5)-1=-6

Thanks a lot!! i was wondering how exactly the ~ operator deduces values...
your formula ~b= (-b)-1 really helped clear my doubt...

V Gala
Ranch Hand
Posts: 113
• Number of slices to send:
Optional 'thank-you' note:
sorry I typed by mistake
it is ~5=(-5)-1=-6

Ranch Hand
Posts: 1274
• Number of slices to send:
Optional 'thank-you' note:
Howdy "Leo R" !

The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp