Negative Numbers in Base 2, 16, etc.Date: 01/24/2003 at 23:38:20 From: Jay (Dad) and Nathaniel Wasson Subject: Negative numbers in base 2, 16, etc. Are there negative numbers in alternate bases? Our calculator says that 1-2 in base 16 is FFFFFFFFFF. This can't be right! Our TI-34 calculator also says that 1-3 is FFFFFFFFFE, etc. Why does the calculator consistently do the "negatives" in this pattern if it's wrong instead of saying there is an error? Date: 01/25/2003 at 22:27:25 From: Doctor Peterson Subject: Re: Negative numbers in base 2, 16, etc. Hi, Jay and Nathaniel. Great question! I like to see calculators used to explore numbers (rather than to replace learning to calculate), but sometimes they can raise questions like this. In any base you can just do what we do in base ten, using a negative sign to indicate negative numbers. so 1 - 2 = -1 in base 16 as well as in base 10. But calculators that do binary or hexadecimal arithmetic are designed for programmers - I am one, and I do hexadecimal arithmetic in my head all the time! Computers use binary because they can only store ones and zeroes; so some way had to be invented to store negative signs as ones and zeroes. There are several possibilities for doing this, such as using one bit (digit) as the sign, with 1 meaning negative. But the method that is almost universally used is called "twos-complement," and what you are seeing is the twos-complement representation of negative numbers. The basic idea is to make use of the fact that the numbers stored in a computer are restricted in their range anyway. To use the smallest example found in real life, let's suppose that numbers are stored as eight bits. If we treat them as unsigned numbers, then they will vary from 0 (00000000) to 255 (11111111). If we are to store negative numbers, we'll have to use half of those bit combinations for negative numbers. We take those with a 0 in the first bit as positive: 00000000 = 0 00000001 = 1 ... 01111111 = 127 Now, to make the computer design as simple as possible, we'd like to be able to use the same addition circuits for signed numbers that we use for unsigned. So the number we use for "-1" ought to be the one that gives 0 when we add it to 1. That turns out to be 11111111: 11111111 + 00000001 ---------- 100000000 That doesn't look as if it adds up to 0, until you notice that the answer, 256, has nine digits. If we simply drop the overflow, the answer is 00000000. And in fact, if we represent the number -n by 256-n (as in this case we use 255 to represent 1), then (256-n) + n = 256, which is treated as zero. So the negative numbers are 10000000 = -128 10000001 = -127 ... 11111110 = -2 11111111 = -1 If you convert these to hexadecimal (which programmers use essentially as a shorthand for the binary notation), it looks like this: 80 = 10000000 = -128 81 = 10000001 = -127 ... FE = 11111110 = -2 FF = 11111111 = -1 00 = 00000000 = 0 01 = 00000001 = 1 ... 7F = 01111111 = 127 The same idea can be extended to larger storage; the numbers you reported appear to be 5-byte numbers, which seems odd; but 4-byte (32-bit) numbers are typical, and follow the same pattern. Here are some pages that explain this notation further: Negative Numbers in Binary http://mathforum.org/library/drmath/view/55924.html Two's Complement http://mathforum.org/library/drmath/view/54344.html Twos Complement Notation http://mathforum.org/library/drmath/view/54372.html Binary Division and Negative Binary Numbers http://mathforum.org/library/drmath/view/54375.html If you have any further questions, feel free to write back. - Doctor Peterson, The Math Forum http://mathforum.org/dr.math/ |
Search the Dr. Math Library: |
[Privacy Policy] [Terms of Use]
Ask Dr. Math^{TM}
© 1994-2013 The Math Forum
http://mathforum.org/dr.math/