Re: A83: Signed Division
[Prev][Next][Index][Thread]
Re: A83: Signed Division
In accordance with the prophecy, James Matthews uttered:
> 34, -32,767 are signed numbers.
> 65000, 64000 are unsigned numbers (you couldn't represent these with signed
> numbers).
Well, yes you could. You would need 17 bits though.
As for signed/unsigned maths:
Think of it this way: the binary number 11111111 means either 255 or -1. If
it is referred to as "8 bit signed" it represents the number -1. If it is
unsigned, or 16 bit signed (in which case we must assume that 8 zeroes prefix
it) it represents 255. If we add it to, say, five, we get 11111111 + 00000101
= 100000100, a 9-bit number. If this was 8 bit arithmetics, the 9th bit would
be placed in the carry flag and then ignored, so the answer would be
00000100, which is 4. So 5+(-1)=4, and 5+255=260, which is 4 (mod 256).
So when adding two numbers, it doesn't matter if they are signed or unsigned.
What is 5*(-2)? If we try to calculate 11111110*00000101, we get
11111110
* 101
----------
11111110
0
+ 11111110
--------------
10011110110
which is 1270. But 1270 = 246 (mod 256), which represents -10.
So apparently, when multiplyig two numbers, it doesn't matter if they are
signed or unsigned.
Or does it?
When we add two binary numbers, a and b, both 8-bit, we want the result to be
8 bit too. But when we multiply two binary numbers, c and d, both 8-bit we
need 16 bits for the result. That is because e.g. 100*100 won't fit into 8
bits, not even with the help of a carry bit.
And if the result needs to be 16 bit, then 5*(-2) would turn out to be 1270 =
1270 (mod 65536). But we also know that 5*(-2) = -(5*2) = -10 = 65526 (mod
65536). 1270 != 65526, we have a problem, and that problem is that to
calculate an 8bit*8bit=16bit expression with signed numbers, we must first
check whether the numbers are positive or negative, then do the
multiplication with the absolute value of the numbers, and then tidy up the
result with a correct sign afterwards. Which can be pretty cumbersome.
hth,
Linus
----------------------------------------------------------------------------
Why are manhole covers round? How many gas stations are there in the US?
-- Interview questions asked by Microsoft, 1992
----------------------------------------------------------------------------
lairfight@softhome.net, visit http://linusworld.cjb.net (PGP key available).
Follow-Ups:
References: