Converting Negative Decimals to Hexadecimal
Date: 05/30/2001 at 08:27:06 From: Josh Goodpaster Subject: Converting negative decimal numbers to hexadecimal numbers I'm trying to find out the formula for converting a negative decimal number to a hexadecimal number. Using calculators I can get any of the conversions like - 16 = F0, but what I really need to know is HOW to get to F0 from - .16
Date: 06/05/2001 at 15:47:09 From: Doctor Twe Subject: Re: Converting negative decimal numbers to hexidecimal numbers Hi Josh - thanks for writing to Dr. Math. That way of representing signed number is called "sixteen's complement" (the hexadecimal equivalent of two's complement in binary). Generally, sixteen's complement hexadecimal is used as a "shorthand notation" for two's complement signed binary numbers, therefore I think it is easier to understand when taken in two steps. To find the sixteen's complement hexadecimal value, you would first convert the number to two's complement signed binary, then convert that binary value to hexadecimal. To learn about two's complement, check out: Two's Complement http://mathforum.org/dr.math/problems/corbin.07.13.99.html Negative Numbers in Binary http://mathforum.org/dr.math/problems/akella.8.19.99.html To learn about converting binary to hexadecimal, check out: Binary to Hexadecimal http://mathforum.org/dr.math/problems/hamilton12.8.98.html Binary Conversion http://mathforum.org/dr.math/problems/stirling1.7.98.html Converting Bases (bottom of the page) http://mathforum.org/dr.math/problems/reinhardt12.21.97.html all from our Ask Dr. Math archives. Let's do an example. Suppose we want to find the hexadecimal representation of -53 (base 10). First, we convert 53 to binary: 1 1 0 1 0 1 / / / / / / 0 r 1 ---+ / / / / / ---- / / / / / 2 ) 1 r 1 ---+ / / / / ---- / / / / 2 ) 3 r 0 ---+ / / / ---- / / / 2 ) 6 r 1 ---+ / / ----- / / 2 ) 13 r 0 ---+ / ----- / 2 ) 26 r 1 ---+ ----- 2 ) 53 So 53 (base 10) = 110101 (base 2). Next, we take the two's complement. We'll use 8-bit values (as in your example), so we have to fill the value to 8 bits by adding 2 leading zeroes on the left. Then invert the bits and add 1. We then have: 00110101 (positive value) | v 11001010 (invert) + 1 (add 1) ---------- 11001011 (two's complement negative result) This is how -53 is represented in two's complement signed binary. The final step is to convert the binary to hexadecimal. We simply group the bits into groups of four, then convert each group to its hexadecimal equivalent, like so: 1100 1011 \__/ \__/ 12 11 C B So -53 (base 10) = CB (signed hexadecimal using complements). I hope this helps. If you have any more questions, write back. - Doctor TWE, The Math Forum http://mathforum.org/dr.math/
Search the Dr. Math Library:
Ask Dr. MathTM
© 1994- The Math Forum at NCTM. All rights reserved.