• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert negative decimal number to binary

 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean something like Integer.toBinaryString(int)?


--Tim
 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No stress . But can you understand that code? I sure have trouble...ahh, efficiency!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic