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 Silly question on binary conversion! 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 "Silly question on binary conversion!" Watch "Silly question on binary conversion!" New topic

Silly question on binary conversion!

Vinny Chun

Joined: Dec 06, 2001
Posts: 24
I know this is a silly question. However, I forgot how to convert a negative decimal to a binary. If there any web site on this topic, thanks!
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
here goes:
take the positive counterpart of your negative number, then convert it to binary. Finally inverts all bits (0 to 1 and 1 to 0) and add 1.
Negative number: -145
Positive number: 145
Binary of 145: 00000000 00000000 00000000 10010001
invert all bits: 11111111 11111111 11111111 01101110
add 1: 11111111 11111111 11111111 01101111
the last binary sequence is actually the binary representation of -145
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 18, 2001).]

[Blog] [Blogroll] [My Reviews] My Linked In
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
This is not really!!! I studied it and forgot about it and stuy it again!! forgot it again!! and .. etc etc.
take a look at this link...
Bala Arul

Joined: Feb 09, 2001
Posts: 29
Hi Fei,
So, what is wrong with the above conversion method.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Hey Bala:
Nothing wrong....just that FEI forgot again.....
sorry couldn't resist....

- satya

Take a Minute, Donate an Hour, Change a Life
Vinny Chun

Joined: Dec 06, 2001
Posts: 24
Thanks, Valentin. Your explanation is simple and clear!
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

Have a look at this discussion and see if it is of some help -
- Manish
[Edited by Val: the link was not a hyperlink ]
[This message has been edited by Valentin Crettaz (edited December 19, 2001).]
I agree. Here's the link:
subject: Silly question on binary conversion!
It's not a secret anymore!