| | |
Convert negative decimal to binary
![]() |
•
•
Join Date: Mar 2007
Posts: 12
Reputation:
Solved Threads: 5
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
http://www.rsu.edu/faculty/PMacphers...rams/twos.html
David
Check the preample of:
http://wiki.python.org/moin/BitwiseOperators
http://wiki.python.org/moin/BitwiseOperators
Should you find Irony, you can keep her!
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 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.
Python Syntax (Toggle Plain Text)
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
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
So in the manual, it says
How?! Specifically, how do they do this efficiently?
Jeff
•
•
•
•
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".
Jeff
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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
I thought it was saying that it somehow represented negatives as if they had infinite precision, which would be impressive.
Jeff
![]() |
Similar Threads
- Help With Decimal to Binary conversion (IT Professionals' Lounge)
- Converting Decimal Value to Binary (MIPS) (Assembly)
- Convert decimal to binary and with base the 8 (C)
- How to convert an integer number to binary?? (C)
- decimal to binary conversion (C++)
- decimal > binary > Oct > Hex (C++)
Other Threads in the Python Forum
- Previous Thread: Need help with the 5 mouse click house program
- Next Thread: Return a class in function definition???
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






