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
    • 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

Doing the last example by hand

  • Convert 42.125 to rational form: 42 1/8 = 337/8
    • Everything stays in rational form for the entire conversion process
  • Sign is 0 (it's positive)
  • Exponent:
    • Divide by the highest power of 2 that's less than num
    • In this case, that is 25 = 32
    • \( \frac{\frac{337}{8}}{2^5} = \frac{337}{8*2^5} = \frac{337}{256} \)
    • Exponent is 5+127 = 132 (1000 0100b)
  • Mantissa
    • First subtract 1: 337/256 - 1 = 337/256 - 256/256 = 81/256; we then encode 81/256 using powers of 1/2

Doing the last example by hand

  • Mantissa: encode it using powers of 1/2
    • To do this, we try to subtract increasing powers of 1/2 until we hit zero
      • From 81/256 we cannot subtract 1/2 (128/256)
      • From 81/256 we can sub 1/4 (64/256), yielding 17/256
      • From 17/256 we cannot subtract 1/8 (32/256)
      • From 17/256 we can sub 1/16 (16/256), yielding 1/256
      • From 1/256 we cannot subtract 1/32 (8/256)
      • From 1/256 we cannot subtract 1/64 (4/256)
      • From 1/256 we cannot subtract 1/128 (2/256)
      • From 1/256 we can sub 1/256 (1/256), yielding zero
    • Thus, the parts of the mantissa are 1/4 + 1/16 + 1/256
    • Encoded: 0101 0001 0000 0000 0000 000

Doing the last example by hand

  • The final encoded number in binary is: 0100 0010 0010 1000 1000 0000 0000 0000
    • The sign, exponent and mantissa are colored differently for clarity

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

Smaller Floating Point

  • 16-bit floating point representations
    • Minifloat: 1 sign, 5-bit exponent, 10-bit mantissa
    • Range from 2.98 * 10-8 to 65504
    • Your graphics card uses this (if you have a good one)
    • 40 billion floating point operations per second (3 Ghz Pentium = 12 billion)
nvidia logo