We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,682 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Convert negative decimal to binary

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.

8
Contributors
8
Replies
5 Years
Discussion Span
1 Month Ago
Last Updated
19
Views
Question
Answered
ALJ
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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/PMacpherson/Programs/twos.html

David

davidjhay
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 6
Skill Endorsements: 0

you can try Ex-Or ing the positive number with 10000000... it will work for numbers less than 01111111

jai.mahadeokar
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
bumsfeld
Nearly a Posting Virtuoso
1,495 posts since Jul 2005
Reputation Points: 409
Solved Threads: 235
Skill Endorsements: 1

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:

BITS = 8
MAXFORBITS = 1
for i in range (0,BITS):MAXFORBITS *= 2
print MAXFORBITS
testvaleur = -18
print (MAXFORBITS + testvaleur) % MAXFORBITS
testvaleur = 18 
print (MAXFORBITS + testvaleur) % MAXFORBITS
Pierre Maurette
Newbie Poster
2 posts since Oct 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 160
Skill Endorsements: 0

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

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 160
Skill Endorsements: 0

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

Jason43
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 3 Years Ago by jrcagle, bumsfeld, davidjhay and 3 others
klickagent
Newbie Poster
1 post since Apr 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0800 seconds using 2.66MB