This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Negative Numbers in Bits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Negative Numbers in Bits" Watch "Negative Numbers in Bits" New topic
Author

Negative Numbers in Bits

L Sol
Greenhorn

Joined: Mar 23, 2004
Posts: 3
How do you calculate the following number is equal to -6?
1111 1111 1111 1111 1111 1111 1111 1010
Thanks!
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
Originally posted by L Sol:
How do you calculate the following number is equal to -6?
1111 1111 1111 1111 1111 1111 1111 1010
Thanks!

Since negative numbers are represented in two's-complement form you need to
  • Flip all the bits
  • add one
  • restore the negative sign

  • original: 1111 1111 1111 1111 1111 1111 1111 1010
    inverted: 0000 0000 0000 0000 0000 0000 0000 0101
    add one: 0101
    0001
    ----
    0110 (which is 6 in base 10)
    Restore negative sign: -6


    Rich
    SCJP 1.4
    Serghei Jelauc
    Ranch Hand

    Joined: Jul 24, 2002
    Posts: 128
    Let say we have 1111 1101
    Change all 1 and 0 vice versa: 0000 0010
    0000 0010 = 2.
    After it add 1: now have 3
    and add "-" sign: -3
    That's all.
    Hope it halps


    SCJP 1.4 <br />SCBCD 1.3<br />SCWCD 1.4
    Corey McGlone
    Ranch Hand

    Joined: Dec 20, 2001
    Posts: 3271
    If you want more information regarding 2's complement, do a search over this forum - you'll find tons of good information.
    Corey


    SCJP Tipline, etc.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Negative Numbers in Bits
     
    Similar Threads
    Shift Operators
    problem with bit shift >>>.
    bitwise complement operator
    Another IO Question
    bit representation