File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes binary / decimal conversion of negatives Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "binary / decimal conversion of negatives" Watch "binary / decimal conversion of negatives" New topic

binary / decimal conversion of negatives

Derek Baker
Ranch Hand

Joined: May 23, 2003
Posts: 46
Could someone please explain to me (or provide some reference to something that can) how to convert negative decimal numbers to binary and negative binary numbers to decimal? If they're positive, I'm fine, but I'm having some trouble with negatives.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Hi Derek,
Converting binary to negative and back again is almost as easy as adding and removing the sign in decimal.
'2s complement' is used to represent binary as negative.
To find 2s complement, simply inverse all the bits (find its complement) then add 1 (giving you its 2nd complement, or 2s complement).

To find the 2s complement of a negative decimal, first find the binary representation of the positive decimal, then apply the 2s complement proceedure and you'll have the negative binary representation.
If you want to convert from negative binary to decimal, find the 2s complement

convert to decimal and make it negative.
You'll note that it's the exact same proceedure whether going to or from negative.
Derek Baker
Ranch Hand

Joined: May 23, 2003
Posts: 46
Perfect. Thanks, Ray.
I agree. Here's the link:
subject: binary / decimal conversion of negatives
It's not a secret anymore!