Docstoc

Number Systems Negative Integers and Floating Point

Document Sample
Number Systems Negative Integers and Floating Point Powered By Docstoc
					                Department of Computer and Information Science,
                           School of Science, IUPUI
                                  Fall 2003




CSCI 230

Information Representation:
                  Negative and Floating Point Representation


           Dale Roberts, Lecturer
           IUPUI
           droberts@cs.iupui.edu




                                                                  Dale Roberts
       Negative Numbers in Binary
  Four different representation schemes are used
  for negative numbers
– Signed Magnitude
       Left most bit (LMB) is the sign bit :
          0 Þ positive (+)
          1 Þ negative (-)
       Remaining bits hold absolute magnitude
     Example:
         210 Þ 0000 0010b           Try, 1000 0100b = -410
        -210 Þ 1000 0010b

     Q: 0000 0000 = ?
        1000 0000 = ?


                                                    Dale Roberts
2. One’s Compliment
  – Left most bit is the sign bit :
     •   0 Þ positive (+)
     •   1 Þ negative (-)
  – The magnitude is complimented

  Example:
      210 Þ 0 000 0010b
     -210 Þ 1 111 1101b

  Exercise: try - 410 using 1’s compliment
                                             Solution:
  Q: 0000 0000 = ?                            410 = 0 000 0100   b
     1111 1111 = ?                           -410 = 1 111 1011   b




                                                        Dale Roberts
   Negative Numbers in Binary (cont.)
3. 2’s Compliment
   • Sign bit same as above
   • Magnitude is complimented first and a “1” is added to
     the complimented digits
   Example:
            210 Þ 0 000 0010b
1’s compliment Þ 1 111 1101b
             +              1
            -210 Þ 1 111 1110b
  Exercise: try -710 using 2’s compliment
                     710 Þ 0000 0111
                                      b
          1’s compliment Þ 1111 1000b
                         +           1
                      -710 Þ 1111 1001b

                                                   Dale Roberts
Negative Numbers in Binary (cont.)
Example: 7+(-3)               [hint]: A – B = A + (~B) +1

    710 = 0000 0111b
    310 = 0000 0011b
1’s complement
               1111 1100b
2’s complement
               1111 1101b º -310

       7+(-3) º       0000 0111
              +       1111 1101
                     1 1111 111 carry

              ignore 1 0000 0100 Þ 0000 0100 º 410




                                                     Dale Roberts
Three Representation of Signed Integer




                                Dale Roberts
       Negative Numbers in Binary (cont.)
4. Excess Representation                      Numbers
                                                                     Excess – 8
   – For a given fixed number of bits          Binary
                                               Value
                                                        Notation
                                                                       Value
     the range is remapped such that                0    0000            -8
     roughly half the numbers are                   1    0001            -7
     negative and half are positive.                2    0010            -6
                                                    3    0011            -5
                                                    4    0100            -4
   Example: (as left)                               5    0101            -3
                                                    6    0110            -2
      Excess – 8 notation for 4 bit numbers
                                                    7    0111            -1
                                                    8    1000             0
      Binary value = 8 + excess-8                   9    1001             1
                                                   10    1010             2
      value                                        11    1011             3
      MSB can be used as a sign bit,               12    1100             4
                                                   13    1101             5
      but                                          14    1110             6
          If MSB =1, positive (+ ve) number        15    1111             7
          If MSB =0, negative (- ve) number

                                                                   Dale Roberts
                  Fundamental Data Type
• With vs. without using sign bit
  For a 16 bit binary pattern:

  2 byte unsigned (Default type is int)                     2 byte int

 0000 0000 0000 0000 ( º 0D)                1000 0000 0000 0000 ( º -32768D º - 215 )
 0000 0000 0000 0001 ( º 1D )               1000 0000 0000 0001 ( º -32767D º - 215 +1)
 0000 0000 0000 0010 ( º 2D )                 ….
 ….                                         1111 1111 1111 1110 ( º - 2D )
                                            1111 1111 1111 1111 ( º - 1D )
 0111 1111 1111 1111 ( º 32767D º 215 -1)
                                            0000 0000 0000 0000 ( º 0D )
 1000 0000 0000 0000 ( º 32768D º 215)
                                            0000 0000 0000 0001 ( º 1D )
 ….                                         0000 0000 0000 0010 ( º 2D )
                                              ….
 1111 1111 1111 1111 (º 216 –1)             0111 1111 1111 1111 ( º 32767D º 215 -1)




                                                                                    Dale Roberts
                      Fundamental Data Type
  Four Data Types in C (assume 2’s complement, byte machine)
        Data Type              Abbreviation     Size (byte)              Range
char      char                                       1                 -128 ~ 127
          unsigned char                              1                   0 ~ 255
          int                                      2 or 4      -215 ~ 215-1 or -231 ~ 231-1
          unsigned int      unsigned               2 or 4       0 ~ 65535 or 0 ~ 232-1
int
          short int         short                    2              -32768 ~ 32767
          unsigned short    unsigned short           2                 0 ~ 65535
          int
          long int          long                     4                 -231 ~ 231-1
          unsigned long     unsigned long            4                  0 ~ 232-1
          int
       float                                         4
       double                                        8

 Note:          27 = 128, 215 =32768, 215 = 2147483648
                Complex and double complex are not available


                                                                                Dale Roberts
                   Fractional Numbers
 Examples: 456.7810 = 4 x 102 + 5 x 101 + 6 x 100 + 7 x 10-1+8 x 10-2
           1011.112          = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 + 1 x 2-1 + 1 x 2-2
                    = 8 + 0 + 2 + 1 + 1/2 + ¼
                    = 11 + 0.5 + 0.25 = 11.7510

Conversion from binary number system to
decimal system
 Examples: 111.112 = 1 x 22 + 1 x 21 + 1 x 20 + 1 x 2-1 + 1 x 2-2
                   = 4 + 2 + 1 + 1/2 + ¼ = 7.75 10
 Examples: 11.0112

                          2       1        0        -1      -2       -3
                          22     21        20        2-1      2-2
                          2-3
                          4      2         1        ½        ¼       1/8
                                  x        x                 x        x

                                                                           Dale Roberts
Conversion from decimal number system to binary
system
Examples:      7.7510 = (?)2

•  Conversion of the integer part: same as before – repeated division by 2
   7 / 2 = 3 (Q), 1 (R) Þ 3 / 2 = 1 (Q), 1 (R) Þ 1 / 2 = 0 (Q), 1 (R)        710 = 1112
2. Conversion of the fractional part: perform a repeated multiplication by 2 and extract
   the integer part of the result
   0.75 x 2 =1.50 Þ extract 1
   0.5 x 2 = 1.0 Þ extract 1                write in the same order        0.7510 = 0.112
   0.0               Þ stop
    \   Combine the results from integer and fractional part, 7.7510 = 111.112
How about choose some of

        4      2     1         1/2     1/4      1/8
                               =0.5   =0.25    =0.125


Examples: try 5.625B



                                                                                 Dale Roberts
                Fractional Numbers (cont.)
Exercise 1: Convert (0.625)10 to its binary form
    Solution:             0.625 x 2 = 1.25        Þ extract 1
                          0.25 x 2 = 0.5          Þ extract 0
                          0.5 x 2 = 1.0           Þ extract 1
                          0.0       Þ stop


         \ (0.625)10 = (0.101)2

Exercise 2: Convert (0.6)10 to its binary form
    Solution:     0.6 x 2 = 1.2     Þ extract 1
                    0.2 x 2 = 0.4   Þ extract 0
                    0.4 x 2 = 0.8   Þ extract 0
                    0.8 x 2 = 1.6   Þ extract 1
                    0.6 x 2 =       Þ


                    \ (0.6)10 = (0.1001 1001 1001 …)2



                                                                Dale Roberts
             Fractional Numbers (cont.)
Errors
  One source of error in the computations is due to back and
  forth conversions between decimal and binary formats
  Example: (0.6)10 + (0.6)10 = 1.210

   Since (0.6)10 = (0.1001 1001 1001 …)2
   Lets assume a 8-bit representation: (0.6)10 = (0 .1001 1001)2 , therefore
     0.6                          0.10011001
   + 0.6               Þ + 0.10011001
                                  1.00110010
   Lets reconvert to decimal system:
   (1.00110010)b
   = 1 x 20 + 0 x 2-1 + 0 x 2-2 + 1 x 2-3 + 1 x 2-4 + 0 x 2-5 + 0 x 2-6 + 1 x 2-7 + 0 x 2-
     8

   = 1 + 1/8 + 1/16 + 1/128 = 1.1953125
   \ Error = 1.2 – 1.1953125
         = 0.0046875

                                                                            Dale Roberts
Floating Point Number Representation
If   x is a real number then its normal form representation is:


       x = f • Base E
       where             f : mantissa
                         E: exponent
                                            exponent
       Example:          125.3210 = 0.12532 • 103
                                    mantissa
                         - 125.3210 = - 0.12532 • 103
                           0.054610 =   0.546 • 10 –1


     The mantissa is normalized, so the digit after the fractional point is non-zero.
     If needed the mantissa should be shifted appropriately to make the first digit
     (after the fractional point) to be non-zero & the exponent is properly adjusted.


                                                                           Dale Roberts
Example:
                          3
  134.1510 = 0.13415 x 10
                     -2
  0.002110 = 0.21 x 10

  101.11B =

  0.011B =

  AB.CDH=

  0.00ACH=




                              Dale Roberts
Assume we use 16-bit binary pattern for normalized binary
form based on the following convention (MSB to LSB)
    Sign of mantissa (±)= left most bit (where 0: +; 1: - )
    Mantissa (f)= next 11 bits
    Sign of exponent (±)= next bit (where 0: +; 1: - )
    Exponent (E) = next three bits

                                   ±E            f = 0.?1?2?3?4…?11 ?12…?15
       x = ± f • Base
                                                 E : converted to binary, b1b2b3
 MSB                                                                                  LSB

         ?1   ?2   ?3   ?4   ?5   ?6   ?7   ?8   ?9   ?10 ?11          b1   b2   b3
  +:0                                                           +:0
  - :1                                                          - :1

                                                                             Dale Roberts
Floating Point Number Representation
Question:
How the computer expresses the 16-bit approximation of
1110.111010111111 in normalized binary form using the
following convention
    Sign of mantissa = left most bit (where 0: +; 1: - )
    Mantissa = next 11 bits
    Sign of exponent = next bit (where 0: +; 1: - )
    Exponent = next three bits
Answer:
    Step 1: Normalization
                                                                        +4
      1110.111010111111 = + 0.1110111010111111 * 2
    Step 2: “Plant” 16 bits
                                                     sign    mantissa        sign exponent
                                                     1 bit    11 bits        1 bit  3 bits

      the 16 bit floating point representation is 0 11101110101 0 100

                                                                             Dale Roberts
 Question:
 Interpret the normalized binary number
             0111 0000 0000 1010          B

 using the convention mentioned
     Sign of mantissa = left most bit (where 0: +; 1: - )
     Mantissa = next 11 bits
     Sign of exponent = next bit (where 0: +; 1: - )
     Exponent = next three bits
  find its decimal equivalent.

Answer:
     0 11100000000 1 010         B
                                   = 0.111B * 2-2

                                                            Dale Roberts
 Floating Point Number Representation (cont.)
The 32 Bit Single Precision Floating Point Format for IBM 370
   Base = 16
   Exponent = Excess-64 notation (i.e., compute binary equivalent, then substrate 64)
   Sign = sign of number (0: positive, 1: negative)
   Mantissa = normalized fraction (i.e. first digital after ‘.’ is non-zero)

                        sign     exponent       mantissa
                        1 bit      7 bits        24 bits

Example: What is the value of the following point number?

              1 100 0010 1001 0011 1101 0111 1100 0010

    Sign = 1 Þ the number is negative
    Exponent (E) = 100 00102 = 6610 = 2 (substrate 64, because of Excess-64 )
    Mantissa (f ) = 1001 0011 1101 0111 1100 0010 = 93D7C2H
                                                                           E       2
    \ The above floating point number is: x = (sign) f • 16 = - 0.93D7C2 • 16

    x = - (9 x 16-1 + 3 x 16-2 + D x 16-3 + 7 x 16-4 + C x 16-5 + 2 x 16-6) • 16 2
     = - (9 x 161 + 3 x 160 + 13 x 16-1 + 7 x 16-2 + 12 x 16-3 + 2 x 16-4)
     = - (144+3 +0.8125+0.02734375 + 0.0029296875 + 0.000030517578125)
     = - 147.842803955078125


                                                                                 Dale Roberts
                 Acknowledgements
These slides where originally prepared by Dr. Jeffrey Huang, updated by Dale
Roberts.




                                                                    Dale Roberts

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:7/14/2013
language:English
pages:20