944,138 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 32049
  • Python RSS
Oct 22nd, 2007
0

Convert negative decimal to binary

Expand 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.
Similar Threads
ALJ
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ALJ is offline Offline
3 posts
since Oct 2007
Oct 22nd, 2007
0

Re: Convert negative decimal to binary

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
Reputation Points: 10
Solved Threads: 6
Newbie Poster
davidjhay is offline Offline
12 posts
since Mar 2007
Oct 23rd, 2007
0

Re: Convert negative decimal to binary

you can try Ex-Or ing the positive number with 10000000... it will work for numbers less than 01111111
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jai.mahadeokar is offline Offline
7 posts
since Oct 2007
Oct 23rd, 2007
0

Re: Convert negative decimal to binary

Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 24th, 2007
0

Re: Convert negative decimal to binary

Click to Expand / Collapse  Quote originally posted by ALJ ...
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:
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Pierre Maurette is offline Offline
2 posts
since Oct 2007
Oct 27th, 2007
0

Re: Convert negative decimal to binary

So in the manual, it says

Quote ...
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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Oct 28th, 2007
0

Re: Convert negative decimal to binary

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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Jan 28th, 2010
1
Re: Convert negative decimal to binary
Click to Expand / Collapse  Quote originally posted by ALJ ...
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.

This may help you out:

Signed Numbers (2 methods)
Signed‐and‐Magnitude
Leading digit is the sign: 0 = positive; 1 = negative
+9 = 00001001
-9 = 10001001
+23 = 00010111
-23 = 10010111
2’s Compliment
Decimal to Binary
Invert all 0’s and 1’s, and add 1.
+9 = 00001001
-9 = 11110110
00000001 +
--------
11110111
+23 = 00010111
-23 = 11101000
00000001 +
--------
11101001
Binary to Decimal
Check the sign bit. If the sign bit is 0, number is positive, and you’re done. If it is 1, then number is negative, so invert
the bits and add 1.
00001101 = positive number
= + 13 (Done)
11110010 negative number
00001100 invert
00000001 + add one
--------
00001101 = - 13
11111111 negative number
00000000 invert
00000001 + add one
--------
00000001 = - 1
10000000 negative number
01111111 invert
00000001 + add one
--------
10000000 = - 128
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Jason43 is offline Offline
1 posts
since Jan 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: How does this work (regarding datetime module)?
Next Thread in Python Forum Timeline: Self Replication





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC