CS255 Syllabus
Intro to "tens complement encoding"
• Encoding: a mapping of one system into another system

Comment: encoding is like a kind of "secret word" game that kids like to play where they replace one letter by another. For example, one common code used by kids is "+1 psoition", so that B means A, C means B, and so on (A means Z). So a secret massage "Ifmmp" means "Hello".

• Example encoding: Morse code

encode letters in English alphabet using short and long pulses

```Example:

. . .      is S
- - -      is O
```

• Remember that numeric value is something intrinsic and does not depends on the system of representation: click here.

We now introduce the 10's complement encoding to represent signed values

• We define the following "Odometer code":

• 3 digits odometer
• The odometer encoding:
```Odometer reading:   500  ... 996 997 998 999 000 001 002 003 ...   499
-----------------    +--------+---+---+---+---+---+---+---+---------+
Value represented: -500  ... -4  -3  -2  -1   0   1   2   3  ...   499
```

• Negative values are represented

• Values greater than 499 and less -500 cannot be represented ("out of range")

• Operations can produce values that are out of range. This condition is called overflow.

When overflow occurs, erroneous results will be produced

To effectively use a code, you need to be able to: (1) encode and (2) decode

So: to use 10s complement code, we need to know how to convert a value to 10s complement and vice versa

• Convert a value v to its 3 digit 10's complement encoding:

• If value v is positive, just write the number out in 3 digits
• If value v is negative, compute 1000 + v and write the result out in 3 digits

Example:

 ``` v = 3 3 digit 10's complement representation: 003 v = 103 3 digit 10's complement representation: 103 v = -3 3 digit 10's complement representation: 1000 - 3 = 997 v = -103 3 digit 10's complement representation: 1000 - 103 = 897 ```

• Convert a 3 digit 10's complement encoding c to a signed value

• If the encoding c begins with 0, 1, 2, 3 or 4, it is a positive value and the value is "face value" (without the leading 0's)
• If the encoding c begins with 5, 6, 7, 8 or 9, it is a negative value and the value is equal to 1000 - c

Example:

 ``` code c = 413 -> it is a positive number the value = 413 code c = 613 -> it is a negative number... Compute: 1000 - 613 = 387 the value = -387 ```

• Properties of 10's complement encoding:

• Only one representation for ZERO (check for yourself)
• Operations are "natural" - see examples below

```               Values        3 digit 10's compl repr
positive           5                 005
values          +  9               + 009
-----              ------
14                 014  -> represents 14

positive +         5                 005
negative        + -9               + 991
-----              ------
-4                 996  -> represents -4

negative +        -5                 995
positive        +  9               + 009
-----              ------
4                 004  -> represents 4

negative          -5                 995
values          + -9               + 991
-----              ------
-14                 986  -> represents -14
```

• Subtracting 10's complement numbers:

```               Values        3 digit 10's compl repr
Subtract 2
positive           5                 005
values          -  9               - 009
-----              ------
-4                 996  -> represents -4

Subtract
positive -         5                 005
negative        - -9               - 991
-----              ------
14                 014  -> represents 14

Subtract
negative -        -5                 995
positive        -  9               - 009
-----              ------
-14                 986  -> represents -14

Subtract 2
negative          -5                 995
values          - -9               - 991
-----              ------
4                 004  -> represents 4
```