Convert negative decimal to binary

Reply

Join Date: Oct 2007
Posts: 2
Reputation: ALJ is an unknown quantity at this point 
Solved Threads: 0
ALJ ALJ is offline Offline
Newbie Poster

Convert negative decimal to binary

 
0
  #1
Oct 22nd, 2007
Hi, I'm having trouble trying to figure out a code that converts negative decimal numbers to binary, as well as specifying the number of bits. For example. convert -18 using 8 bits. This should come out as 10010010 doing it manually, I think. I'd appreciate the help, thanks.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: davidjhay is an unknown quantity at this point 
Solved Threads: 5
davidjhay davidjhay is offline Offline
Newbie Poster

Re: Convert negative decimal to binary

 
0
  #2
Oct 22nd, 2007
Working with negative numbers in binary you need to use twos complement method. It is a bit confusing to start with (well it was for me). I use it a lot for checksum calculations. Hope this helps.

http://www.rsu.edu/faculty/PMacphers...rams/twos.html

David
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 7
Reputation: jai.mahadeokar is an unknown quantity at this point 
Solved Threads: 0
jai.mahadeokar jai.mahadeokar is offline Offline
Newbie Poster

Re: Convert negative decimal to binary

 
0
  #3
Oct 23rd, 2007
you can try Ex-Or ing the positive number with 10000000... it will work for numbers less than 01111111
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Convert negative decimal to binary

 
0
  #4
Oct 23rd, 2007
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: Pierre Maurette is an unknown quantity at this point 
Solved Threads: 0
Pierre Maurette Pierre Maurette is offline Offline
Newbie Poster

Re: Convert negative decimal to binary

 
0
  #5
Oct 24th, 2007
Originally Posted by ALJ View Post
Hi, I'm having trouble trying to figure out a code that converts negative decimal numbers to binary, as well as specifying the number of bits. For example. convert -18 using 8 bits. This should come out as 10010010 doing it manually, I think. I'd appreciate the help, thanks.
Hum... 10010010 is a bit-sign representation (sign bit followed by the 7 bit representation of 18) wich is very unusual. If you want the complement to 2 reprrésentation, you have to know that whith 8 bits, the binary for N negative is the same than the positive for 255 + N + 1, 238 for -18. I assume you know how to convert a positive value to binary. Look at this, I think it works for any BITS value:
  1. BITS = 8
  2. MAXFORBITS = 1
  3. for i in range (0,BITS):MAXFORBITS *= 2
  4. print MAXFORBITS
  5. testvaleur = -18
  6. print (MAXFORBITS + testvaleur) % MAXFORBITS
  7. testvaleur = 18
  8. print (MAXFORBITS + testvaleur) % MAXFORBITS
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Convert negative decimal to binary

 
0
  #6
Oct 27th, 2007
So in the manual, it says

Of course, Python doesn't use 8-bit numbers. It USED to use however many bits were native to your machine, but since that was non-portable, it has recently switched to using an INFINITE number of bits. Thus the number -5 is treated by bitwise operators as if it were written "...1111111111111111111010".
How?! Specifically, how do they do this efficiently?

Jeff
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Convert negative decimal to binary

 
0
  #7
Oct 28th, 2007
Oh, nevermind. I thought it was saying something else. All it's saying is that when it performs right-shifts, it rolls in the MSB from the left. (Arithmetic instead of Logical shift)

I thought it was saying that it somehow represented negatives as if they had infinite precision, which would be impressive.

Jeff
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC