Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes convert negative decimal number to binary Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "convert negative decimal number to binary" Watch "convert negative decimal number to binary" New topic
Author

convert negative decimal number to binary

K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
What's the method of converting negative decimal number to binary say converting -13 to binary.
Thanks
[ June 28, 2004: Message edited by: Anshul Kayastha ]
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
Do you mean something like Integer.toBinaryString(int)?


--Tim
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
Can u please tell me how is that manually done.
I know how it is done for positive numbers (divide by 2 until remainder is 0). But i can't make it work for -ive numbers.
[ June 28, 2004: Message edited by: Anshul Kayastha ]
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
Do you mean with two's complement representation of -ves? You'll have to decide that before you work on the conversion.

If you do, you could cheat and look at the source of Integer.toBinaryString:



Of course, that doesn't exactly give you a simple, clear answer.


--Tim
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
Thanks Tim
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
No stress . But can you understand that code? I sure have trouble...ahh, efficiency!
 
 
subject: convert negative decimal number to binary
 
Similar Threads
basic question about binary, oct and Hex
Shift Operators
binary / decimal conversion of negatives
Binar representation of octal & hexadecimal nos
Convert decimal to binary