CS 2150 Roadmap

Data Representation

Program Representation

 
 
string
 
 
 
int x[3]
 
 
 
char x
 
 
 
0x9cd0f0ad
 
 
 
01101011
vertical red double arrow  
Objects
 
Arrays
 
Primitive types
 
Addresses
 
bits
           
 
Java code
 
 
C++ code
 
 
C code
 
 
x86 code
 
 
IBCM
 
 
hexadecimal
vertical green double arrow  
High-level language
 
Low-level language
 
Assembly language
 
Machine code

Positional Number Systems

  • Integers
    • 346 = 3*102 + 4*101 + 6*100
    • 346 = 28 + 26 + 24 + 23 + 21
      • =1*28+0*27+1*26+0*25+1*24+1*23+0*22+1*21+0*20
    • \( d_{n} d_{n-1} \ldots d_{0} = \sum_{i=0}^{n} d_{i} \cdot R^{i} \)
  • Reals
    • \( d_{n} d_{n-1} \ldots d_{0} . d_{-1} d_{-2} \ldots d_{-m} = \sum_{i=-m}^{n} d_{i} \cdot R^{i} \)

Conversion between bases

Radix R to decimal:

\( n = d_n R^n + \ldots + d_0 R^0 \)

 

Decimal to radix R:

\( \frac{n}{R} = d_n R^{n-1} + \ldots + d_1 R^0 \), remainder \( d_0 \)

Radix to Decimal

\( 42_5 \)

 

 

 

 

\( 121_3 \)

 

 

Decimal to Radix

\( 42_{10} \) to radix 5

 

 

 

 

\( 121_{10} \) to radix 11

 

 

Directions for getting 6

  1. Choose any regular accumulator (ie. Accumulator #9).
  2. Direct the Initiating Pulse to terminal 5i.
  3. The initiating pulse is produced by the initiating unit's Io terminal (usually plugged into Program Line 1-1) each time the Eniac is started. Simply connect a program cable from Program Line 1-1 to terminal 5i on this Accumulator.
  4. Set the Repeat Switch for Program Control 5 to 6.
  5. Set the Operation Switch for Program Control 5 to ADD.
  6. Set the Clear-Correct switch to C.
  7. Turn on and clear the Eniac.
  8. If there are random neons illuminated in the accumulators, press the "Initial Clear" button of the Initiating device
  9. Press the "Initiating Pulse Switch" that is located on the Initiating device.
  10. Stand back.

Binary Representation

n-bit binary number: \( b_{n-1} b_{n-2} b_{n-3} \ldots b_2 b_1 b_0 \)

Value = \( \sum_{i=0}^{n-1} b_i \cdot 2^i \)

 

Boolean arithmetic:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 0 carry 1

Maximum value is \( 2^n-1 \) -- but what should \( n \) be?

Two's complement (n=8)

  • 0
    • 0d = 00000000b
  • 1
    • 1d = 00000001b
  • 10 = 8+2
    • 10d = 00001010b
  • 100 = 64+32+4
    • 100d = 01100100b
  • 127 = 64+32+16+8+4+2+1
    • 127d = 01111111b
 
  • -1
    • +1d = 00000001b
    • -1d = 11111111b
  • -10
    • +10d = 00001010b
    • -10d = 11110110b
  • -100
    • +100d = 01100100b
    • -100d = 10011100b
  • -128
    • +128d = 10000000b
    • -128d = 10000000b

A bit of an aside...

This is an actual comment left by a graduating SEAS computer science major:

 

I can't count the number of times I was taught how to count in binary

 

So this material may appear again in the CS curriculum. Hopefully everybody will (eventually) learn it...

IEEE 754 Floating Point
Single Precision (32 bits)

  • 32 bits are split as follows:
    • bit 1: sign bit, 1 means negative (1 bit)
    • bits 2-9: exponent (8 bits)
    • bits 10-32: mantissa (23 bits)
  • Exponent values:
    • 0: zeros
    • 1-254: exponent+127
      • The value of 127 is called the exponent offset or the bias
    • 255: infinities, overflow, underflow, NaN

 

\( \text{value} = (1-2*\text{sign}) * (1 + \text{mantissa}) * 2^{\text{exponent}-127} \)

Mantissa

\( b_{1}b_{2}b_{3}b_{4}b_{5}b_{6}b_{7}b_{8}b_{9}b_{10}b_{11}b_{12}b_{13}b_{14}b_{15}b_{16}b_{17}b_{18}b_{19}b_{20}b_{21}b_{22}b_{23} \)

 

\( \text{mantissa} = 1.0 + \sum^{23}_{i=1} \frac{b_i}{2^i} \)

 

  • If the bits are all 1, what is the mantissa value?
    • 1 + (almost) 1 = (almost) 2: the maximum value for the mantissa
    • Actually about 1.9999999999999
  • Minimum value is all 0's
    • That's a mantissa of 1.0

Floating point rounding errors

  • 0.1 is stored (as a 32 bit float) as
    • mantissa = 100 1100 1100 1100 1100 1101
  • Or: \( \frac{1+\frac{1}{2^1}+\frac{1}{2^4}+\frac{1}{2^5}+\frac{1}{2^8}+\frac{1}{2^9}+\frac{1}{2^{12}}+\frac{1}{2^{13}}+\frac{1}{2^{16}}+\frac{1}{2^{17}}+\frac{1}{2^{20}}+\frac{1}{2^{21}}+\frac{1}{2^{23}}}{2^4} \)
     
  • Which is exactly equal to:
    • 0.100000001490116119384765625
    • But only the first 7 digits, .1000000, are printed
  • 0.1 (exactly) is a finite number in decimal, but repeating in binary

Floating Imprecision

  • 24 bits in a fixed-point register:
    \( 0.1 \Rightarrow \frac{1+\frac{1}{2^1}+\frac{1}{2^4}+\frac{1}{2^5}+\frac{1}{2^8}+\frac{1}{2^9}+\frac{1}{2^{12}}+\frac{1}{2^{13}}+\frac{1}{2^{16}}+\frac{1}{2^{17}}+\frac{1}{2^{20}}+\frac{1}{2^{21}}}{2^4} \\ = 209715 / 2097152 \\ = 0.099999904632568359375 \)
    • Error is 0.2/2097152 = 1/10485760
  • One hour = 3,600 seconds = 36,000 tenths of a second
    • At 1/10485760 time error per tenth of a second, that yields 0.0034 seconds of time error per hour
    • 100 hours: 0.34s
  • The Scud missile was traveling at 1,676 m/s (Mach 4.93)
    • 1,676 m/s * 0.34 seconds = 570 meters (1,870 feet)
    • The Patriot thus missed the target by over a half a km