• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the output?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone knows the answer to the above question? Any idea would be greatly appreciated. thanks.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
~b=(-b)-1;
~3 =(-3)-1=-4
-5=(-5)-1=-6
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...


Radha
 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry I typed by mistake
it is ~5=(-5)-1=-6
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm posting this because I am one of the moderators of this forum.


Yours,
Bu.
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic