• 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

Explain the code...

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was just trying out a code as below

public class Flip{
public static void main(String argv[]){
System.out.println(~4);
}
}

And i get the result as -5.

Kindly explain me how does it happen?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The tilde "~" inverts the bits ( 0 to 1 and 1 to 0)
More here
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html
 
sharmistha mohapatra
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply!!!

But still under confusion,as where does this invertion come into action...
If i replace the number in the code to 5 or 6 or 7,the output gives a +1 to the number with a negative sign.

For eg: if i replace it with say 6 now...
I get the output as -7
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sharmistha mohapatra wrote:

If i replace the number in the code to 5 or 6 or 7,the output gives a +1 to the number with a negative sign.

For eg: if i replace it with say 6 now...
I get the output as -7



Yes, That's an easy way to calculate the result without trying it in two's complement way (which is what happens internally).

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two thing you need to understand

1) the '~' inverts all the bits. Since a '4' is represented as

00000000 00000000 00000000 00000100

when you invert the bits you get

11111111 11111111 11111111 11111011

2) now you need to understand how java interprets this value. google or search this forum for "two's complement". basically, you look a the left-most digit, and if it's a 1, you know your value will be negative. you then invert all the digits and add one, so you get



which is 5. and we know our answer is negative, so "11111111 11111111 11111111 11111011" = -5.
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic