Author Message Sign magnitude add & sub

What I want to do, it's to add or sub (depend on the 'operator' bit) to
number with a bit indicating their sign. I know you will said use 2s
complement, but I can't. I get this number from float numbers (the mantissa
and the sign bit).

Let's called the two values Am and Bm (mantissa A & B), and As,Bs (sign of
the mantissa).
I taught I could convert each negative value (based on the sign bit) to 2s
complement number. Then base on the 'operator' bit, perform an addition or
substaction. Until here, I'm okay. Where I get confused, it's to know when I
have a real overflow and if the result is positive or negative. Because if I
have a real overflow, I must shift the result one place to the right.

By 'real overflow' I mean this: Ex: (in 2s complement)

5 + 3 = 0101 + 0011 = 1000 (which is equal -8)
or
-7 + -2 = 1001 + 1100 =  1 0111 (which is equal 7 (positive value) if we

It's this kind of problem I want to trap. The last example should give -9,
but we don't have enough bits. The answer give a positive number.
Anyway, if you can help me, I'll appreciate.

B.T.W, I read in a book on computer arithmetic algorithms that in "general",
when adding two number of opposite sign, no overflow occur, and with the
same sign, overflow can occur.

I don't really like the term "in general". It's is true? Can I put that in
my head that never an 'real' overflow will appen? (I know that sometime one
can append, but we can discard it because it still hold the real result).

Thu, 12 Jun 2003 08:59:00 GMT  Sign magnitude add & sub
I think I found a solution, read this and tell me what you think.

My real problem, is that I can't represent all the possible number of a sign
magnitude system to 2s complement system.
For example, if a have 8 bits + 1 sign bit, the range is -255 <= X <= 255.
But in 2s complement, the range is only -128 <= X <= 127. I can't represent,
for example +140 from sign magintude to 2s complement. To resolve the
problem, I just have to extend to 9 bits, so know (in 2'compl) I have the
full range -256 <= X <= 255. So now I can freely convert between the two
system, perform the appropriate operation and check for overflow without

Quote:
> What I want to do, it's to add or sub (depend on the 'operator' bit) to
> number with a bit indicating their sign. I know you will said use 2s
> complement, but I can't. I get this number from float numbers (the
mantissa
> and the sign bit).

> Let's called the two values Am and Bm (mantissa A & B), and As,Bs (sign of
> the mantissa).
> I taught I could convert each negative value (based on the sign bit) to 2s
> complement number. Then base on the 'operator' bit, perform an addition or
> substaction. Until here, I'm okay. Where I get confused, it's to know when
I
> have a real overflow and if the result is positive or negative. Because if
I
> have a real overflow, I must shift the result one place to the right.

> By 'real overflow' I mean this: Ex: (in 2s complement)

> 5 + 3 = 0101 + 0011 = 1000 (which is equal -8)
> or
> -7 + -2 = 1001 + 1100 =  1 0111 (which is equal 7 (positive value) if we

> It's this kind of problem I want to trap. The last example should give -9,
> but we don't have enough bits. The answer give a positive number.
> Anyway, if you can help me, I'll appreciate.

> B.T.W, I read in a book on computer arithmetic algorithms that in
"general",
> when adding two number of opposite sign, no overflow occur, and with the
> same sign, overflow can occur.

> I don't really like the term "in general". It's is true? Can I put that in
> my head that never an 'real' overflow will appen? (I know that sometime
one
> can append, but we can discard it because it still hold the real result).

Thu, 12 Jun 2003 14:23:18 GMT  Sign magnitude add & sub
On Sun, 24 Dec 2000 00:59:00 GMT, "Reginald Jean Louis"

Quote:

>What I want to do, it's to add or sub (depend on the 'operator' bit) to
>number with a bit indicating their sign. I know you will said use 2s
>complement, but I can't. I get this number from float numbers (the mantissa
>and the sign bit).

>Let's called the two values Am and Bm (mantissa A & B), and As,Bs (sign of
>the mantissa).
>I taught I could convert each negative value (based on the sign bit) to 2s
>complement number.

You can, but why can't you stay in sign magnitude for everything? The
conversion is expensive. For n-bit numbers:

if((int)int_signmag & 2**(n-1))   // original number negative?
int_2scomp = -2**(n-1) - (int)int_signmag;
else
int_2scomp = (int)int_signmag;

Quote:
>Then base on the 'operator' bit, perform an addition or
>substaction. Until here, I'm okay. Where I get confused, it's to know when I
>have a real overflow and if the result is positive or negative. Because if I
>have a real overflow, I must shift the result one place to the right.

>By 'real overflow' I mean this: Ex: (in 2s complement)

>5 + 3 = 0101 + 0011 = 1000 (which is equal -8)
>or
>-7 + -2 = 1001 + 1100 =  1 0111 (which is equal 7 (positive value) if we

>It's this kind of problem I want to trap. The last example should give -9,
>but we don't have enough bits. The answer give a positive number.
>Anyway, if you can help me, I'll appreciate.

If you're working in 2's complement, you have 2 choices.

(1) add/subtract two n-bit numbers using only an n-bit
adder/subtractor. In this case, an overflow has occurred if the
carry/borrow into the sign bit is not equal to the carry/borrow out of
the sign bit.

(2) sign-extend both numbers into (n+1) bits, and then use an (n+1)
subtraction will never overflow.

Quote:
>B.T.W, I read in a book on computer arithmetic algorithms that in "general",
>when adding two number of opposite sign, no overflow occur, and with the
>same sign, overflow can occur.

>I don't really like the term "in general". It's is true? Can I put that in
>my head that never an 'real' overflow will appen? (I know that sometime one
>can append, but we can discard it because it still hold the real result).

This is always true, for any standard binary representation. This is
obvious for additions with the same sign, and the opposite-number
proof is simple:

(1) You get the most negative answer by adding the smallest positive
number (x) and the largest negative number (y). x is always >= 0, so
the result is always >= y.

(2) You get the most positive answer by adding the largest positive
number (x) and the smallest negative number (y). y is always <= -0, so
the result is always <= x.

Quote:
> I think I found a solution, read this and tell me what you think.
> My real problem, is that I can't represent all the possible number of a sign
> magnitude system to 2s complement system.
> For example, if a have 8 bits + 1 sign bit, the range is -255 <= X <= 255.
> But in 2s complement, the range is only -128 <= X <= 127. I can't represent,
> for example +140 from sign magintude to 2s complement. To resolve the
> problem, I just have to extend to 9 bits, so know (in 2'compl) I have the
> full range -256 <= X <= 255. So now I can freely convert between the two
> system, perform the appropriate operation and check for overflow without

You've got this the wrong way around. For n-bit numbers, you can
always represent a sign-magnitude number in 2's complement. However,
you can't necessarily represent the 2's complement version as
sign-magnitude, and you would then need to extend the sign-magnitude
number to (n+1) bits. The reason for this is simple: sign-magnitude is
inefficient, and uses two bit patterns to represent zero (+zero and
-zero). 2's complement uses all bit patterns, and so one of them can't
be represented in sign-magnitude. Your example above is comparing
9-bit sign-magnitude to 8-bit 2's complement.

Anyway, why don't you do your arithmetic in sign magnitude? I haven't