• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Two's Complement

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source : Marcus Green

What will be the result if you attempt to compile and run the following code ?
public class Flip{
public static void main(String argv[]){
System.out.println(~4);
}
}


1 Compile time error
2 compilation and output of 11
3 Compilation and output of -4
4 Compilation and output of -5

Answer : 4

As per me, it should be 11.
Because, bit representation of 4 is 0100. Two's complement will be 1100 which is 11.

Please clarify the doubt.

Thanks
Hemant
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because by default the number 4 is of type int and 32 bits are being involved here!

Try doing the same logic with 32 bits
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This link would help you to understand the negative binary in a better and easy way -> http://www.allaboutcircuits.com/vol_4/chpt_2/3.html
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you execute ~4, keep in mind that "4" is an int, which is 32 bits. 4 is this:

00000000 00000000 00000000 00000100

When you complement that, you get this:

11111111 11111111 11111111 11111011

Because the sign bit is a 1, we know this is a negative number. To see what number that is, we flip the bits and add one:

00000000 00000000 00000000 00000100 (bits flipped)
00000000 00000000 00000000 00000101 (added 1)

That final value is 5, so our negative number was -5.

More info here: Negative Numbers in Java - Two's Complement

Also, from the JLS, 15.15.5 Bitwise Complement Operator ~:


At run time, the value of the unary bitwise complement expression is the bitwise complement of the promoted value of the operand; note that, in all cases, ~x equals (-x)-1.


[ June 26, 2008: Message edited by: Corey McGlone ]
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll buy you a (virtual) beer if you ever need this knowledge in the real world. By the way bit shifting has not been included in the exam since at least 1.5 and I have some freely visible questions on 1.5 at

http://www.examulator.com/phezam
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice seeing you Corey after a long time!
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also an easy formula: If k is of type int or long, than
~k = -k -1
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic