• 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

how to execute the j = ~i?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of the following code?
1: int i = 45678;
2: int j = ~i;
3:
4: System.out.println(j);
A) Compilation error at line 2. ~ operator applicable to boolean values only.
B) Prints 45677.
C) Prints -45677.
D) Prints -45679.

could somebody illustrate me the execute of the code above.I have no idea with the given option .
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gong James:
What is the output of the following code?
1: int i = 45678;
2: int j = ~i;
3:
4: System.out.println(j);
A) Compilation error at line 2. ~ operator applicable to boolean values only.
B) Prints 45677.
C) Prints -45677.
D) Prints -45679.

could somebody illustrate me the execute of the code above.I have no idea with the given option .


i = 45678 which in binary is 00000000 00000000 10110010 01101110
~i = 11111111 11111111 01001101 10010001 which in decimal is -45679. Compare these two -
<pre>
i = 00000000 00000000 10110010 01101110
~i = 11111111 11111111 01001101 10010001
</pre>
Run this code, and you'll understand why. (~ changes all binary 0's to 1's and 1's to 0's)

HTH,
- Manish
[This message has been edited by Manish Hatwalne (edited October 25, 2001).]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gong James:
What is the output of the following code?
1: int i = 45678;
2: int j = ~i;
3:
4: System.out.println(j);
A) Compilation error at line 2. ~ operator applicable to boolean values only.
B) Prints 45677.
C) Prints -45677.
D) Prints -45679.

could somebody illustrate me the execute of the code above.I have no idea with the given option .


Hi James,
The easiest way to calculate ~n (where n is number) is :
~10 is actually -(10) - 1 = -11
In your case :
1: int i = 45678;
2: int j = ~i;
j will have value -(45678) - 1 = -45679
cheers,
Roy
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
~i = (- i) - 1.
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very mcuh for everyone for your kindly help
!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic