Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Knute Snortum
• Paul Clapham
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• Jeanne Boyarsky
• Bear Bibeault
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Ron McLeod
• Piet Souris
• Frits Walraven
Bartenders:
• Ganesh Patekar
• Tim Holloway
• salvin francis

# binary / decimal conversion of negatives

Ranch Hand
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.
Thanks.
Derek

Ranch Hand
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.
hth

Derek Baker
Ranch Hand
Posts: 46
Perfect. Thanks, Ray.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.