The Department of Computer Science & Engineering
cse@buffalo
STUART C. SHAPIRO: CSE 115 C

CSE 115
Introduction To Computer Science for Majors I
Lecture C
Lecture Notes #26
Stuart C. Shapiro
Spring, 2007


More on Number Representation

Fixed Point
Various ideas were tried, for example:
  1. Binary Coded Decimal (BCD). Each digit represented by its binary equivalent. For example, 35 in BCD is 0011 0101.

  2. Binary Numbers

    Using bits to represent integers:
    BinaryDecimal
    0000
    0011
    0102
    0113
    1004
    1015
    1106
    1117

    Positional notation
    Decimal: ones, tens, hundreds, ...
    Binary: ones, twos, fours, ...
    Octal: ones, eights, sixtyfours, ...
    Hexadecimal: ones, sixteens, two hundred fiftysixes, ...

    The two binary digits: 0, 1
    The eight octal digits: 0, 1, 2, 3, 4, 5, 6, 7
    The ten decimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    The fifteen hexadecimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f

    For conversion:
    If divide a number n by a base b, the remainder is the right-most digit, and the result is the rest of the number.
    Examples: 3597/10 = 359 R7     11011/2 = 1101 R1

    It works even if n is not expressed in base b:

    1035 / 2 = 517 R 1
     517 / 2 = 258 R 1
     258 / 2 = 129 R 0
     129 / 2 =  64 R 1
      64 / 2 =  32 R 0
      32 / 2 =  16 R 0
      16 / 2 =   8 R 0
       8 / 2 =   4 R 0
       4 / 2 =   2 R 0
       2 / 2 =   1 R 0
       1 / 2 =   0 R 1
    
    So 10352 = 10 000 001 011

Representation of Negative Integers
If use 8 bits to represent an integer, and want negatives as well as positives, need one bit to distinguish negatives from positive, leaving 7 bits for the magnitude.

With 7 bits, can represent 128 numbers, e.g. 0, ..., 127.
But what about negatives: -0, ..., -127?

First idea (sign + magnitude):

 3 = 00000011     0 = 00000000
-3 = 10000011    -0 = 10000000
Bad idea: addition is different; -0 is wasteful.

Second idea (biased):
use 00000000 for -127, 11111111 for 128.
That is, to represent n, use binary representation of 127 + n.

 3 = 10000010      0 = 01111111
-3 = 01111100     -0 = 01111111
Problem: 3 + -3 = 254. But see below:

Better idea: Two's Complement
To go from n to -n: complement bits, add 1, drop final carry

            0 = 00000000  1 = 00000001  3 = 00000011  4 = 00000100  7 = 00000111
complement      11111111      11111110      11111100      11111011      11111000
add 1       0 = 00000000 -1 = 11111111 -3 = 11111101 -4 = 11111100 -7 = 11111001
No -0, and it's reversible.
Try some addition:
n + -n          00000000      00000000      00000000      00000000      00000000

                4 = 00000100        -3 = 11111101
               -1 = 11111111        -4 = 11111100
                    --------             --------
               sum  00000011 = 3   sum   11111001 = -7

Representation of Floating Point
Beyond the binary point
Decimal positions: 102 101 100 . 10-1 10-2
Binary positions: 22 21 20 . 2-1 2-2

Retrieve decimal fractional digits by repeated multiplication by 10.

        .32054
       3.2054
       2.054
       0.54
       5.4
       4.
So retrieve binary fractional bits by repeated multiplication by 2.
        .1101
       1.101
       1.01
       0.1
       1.
Even if "bits" are in decimal notation.
        .25
       0.5
       1.
So, .2510 = .012

Try 0.110:

        .1
       0.2
       0.4
       0.8
       1.6
       1.2
       0.4  repeat...
So, .110 = .0001100110011...2

Machine Representation
IEEE 754 standard:
  1. Adjust exponent so mantissa is 1. ...
  2. Use left-most bit to represent sign of number.
  3. Use next several bits to represent exponent, biased.
  4. Use remaining bits to represent mantissa - 1, as a binary number.

Computers don't represent (all the) real numbers
Real numbers are dense: between any two real numbers there is a third real number.
Try in DrJava:
(4.9e-324 + 0.0)/2

First Previous Next

Copyright © 2007 by Stuart C. Shapiro. All rights reserved.

Last modified: Fri Nov 30 14:56:27 EST 2007
Stuart C. Shapiro <shapiro@cse.buffalo.edu>