|
by George Martin
Start ę The
Unsigned Modifier in Action ę The Signed
Notation ę A Lesson in Subtraction
ę Sources and PDF
THE SIGNED NOTATION
If you want to work with negative numbers,
you need another notation called twoęs complement notation. Table
3 illustrates the notation needed for an 8-bit register.
|
0111 1111
0111 1110
0111 1101
. . . . .
0000 0010
0000 0001
0000 0000
1111 1111
1111 1110
. . . .
1000 0011
1000 0010
1000 0001
1000 0000
|
127
126
125
. . . . .
2
1
0
ę1
ę2
. . . .
ę125
ę126
ę127
ę128
|
| Table 3ęWith twoęs
complement notation, itęs tricky characterizing some numbers
in an 8-bit register. |
This notation is called signed in C,
and itęs the default mode if you donęt use a modifier. And, because
the hardware doesnęt know the difference, the binary addition still
takes place. The notation is just that, a notation. It gives something
the programmer can use.
Note that this twoęs complement notation
is linear. With addition, each larger value is a result of adding
one to the smaller value, and for subtraction, the converse is true.
In fact, the computeręs hardware uses twoęs complement notation when
performing subtraction. The subtrahend is converted to twoęs complement
and then added to the minuend.
To convert a number to its negative,
invert all the bits and add one (see Table 4). Look back at Table
3 and do the twoęs complement conversions. Isnęt zero interesting?
|
Original
0000 0010
0000 0001
0000 0000
1111 1111
1111 1110
|
Decimal
2
1
0
ę1
ę2
|
Invert
1111 1101
1111 1110
1111 1111
0000 0000
0000 0001
|
Invert +1
1111 1110
1111 1111
0000 0000
0000 0001
0000 0010
|
| Table
4ęWith twoęs complement notation, you get the negative by inverting
the bits and adding one. |
Twoęs complement notation forms the basis
of signed and unsigned notation. Simply tell the compiler which representation
the variable will take so the compiler can generate the proper assembly
language instructions, but the CPU does not change its operation.
PREVIOUS
NEXT
Circuit Cellar provides up-to-date information for engineers. Visit
www.circuitcellar.com for
more information and additional articles.
For subscription information, call (860) 875-2199, subscribe@circuitcellar.com
or subscribe online.
ęCircuit Cellar, the Magazine for Computer Applications. Posted with
permission. |