PDA

Click to See Complete Forum and Search --> : SAM and Two's compliment...


Benjamin
Dec 16th, 2000, 06:48 AM
Can somebody please explain these to me?

wednessday in Computer science, the teacher was talking about Sign And Magnitude, and Two's Compliment(for Binary Numbers) but I didn't understand it all the way... could somebody please explain it?



Thanks,
Dennis

Sam Finch
Dec 16th, 2000, 07:30 AM
Sign and Magnitude's pretty simple, for an n bit number bit n-1 (the highest order bit) represents the sign of the number, and the other bits represent the magnitude, for example

01111111 represents 127
11111111 represents -127

00100111 represents 40
10100111 represents -40

00000000 represents 0
10000000 represents -0

the trouble with this system is addin numbers becomes quite complicated if one or both is -ve, we're also wasting a number because we can represent + and - 0, so we get the 2s complement system.




for the 2s complement system every bit represents a value bit 0 represents 1, bit 1 represents 2, bit x represents 2^x, all exept the highest order bit bit n, this represents -2^n. so the value of a number is given by this algorithm


binary number sxxxxxxx (s and x are bits, s is named
differently to distinguish it
from the other bits)

split into 2 0xxxxxxx
s0000000


find the value of the 2 binary numbers

X
S

and subtract X-S

as the maximum value for x is one less than the value of -S (when bit s is 1) the value of a string of 1s is always -1 and by decrementing x we can represent all -ve numbers down to S.


this seems like quite a lot of work to get one extra number, plus we can't negate numbers quite so easily, (with SAM we just flip the HO bit to negate the number)

but it turns out negating numbers isn't that hard

imagine a binary number T and T's complement !T what is T + !T?

well for any bit in T the corresponding bit in !T will be the oppisite, so the 2 bits add up to 1 with no carry's, so we get a string of 1s, which is -1


so we get this

T + !T = -1

!T = -1 - T

!T + 1 = -T


so to negate a 2's complement number all we have to do is add 1 to it's complement.


Adding 2's complement numbers is also easy, you just add them like unsigned numbers and amazingly you get the right answer. (it's quite easy to work out why, little chalenge for ya)

So for the slight difficulty in negating not only do we get an extra -ve number but it makes adding easier and faster, multiplication's about the same.

Benjamin
Dec 16th, 2000, 10:51 AM
Thanks Sam! :)