Comp 150-001, TTh, 11:00-2:00, DH-339

Class 3

Negative numbers

This is a good time to remember that a byte holding the value 23 doesn't hold 10111; it holds 0001 0111.

How are we going to represent negative numbers? One approach would be to reserve the leftmost bit to hold the sign, 0 for + and 1 for -. Thus, -23 would be 1001 0111. Alas, this means that how we add depends very much on the sign bit. A simpler strategy (for hardware designers) is called 2's complement: flip all the bits, and add 1. This turns out to mean that signed addition is done exactly the same way as unsigned! This is a big win. (Logically, flipping all the bits means subtracting from 1111 1111; adding 1 means that we were really subtracting from 1 0000 0000; that is, subtracting from 0000 0000; that is, negating.)


Dividing into units: related to management of "memory"
    bytes
    words

Memory (RAM)
    binary integers
    floating-point and negative integers

Memory (disk)
    not directly ADDRESSABLE
    divided into sectors, clusters (physical v logical sectors)


floating point

Sign, mantissa, exponent.
See en.wikipedia.org/wiki/Double_precision
Image:IEEE_754_Double_Floating_Point_Format.svg
This is the IEEE 64-bit floating point format, mostly used by real hardware today.

The exponent is signed, leaving 10 bits for magnitude. Thus it can go to 21024, which is about 10300. Thus the largest (IEEE 64-bit) floating-point number representable is about 10300, and the smallest positive floating-point number is 10-300. By contrast, most calculators go up to 10100.


ASCII

Unicode

pictures

RGB and why it works

sound

WAV format: 16-bit (2-byte) samples, x 2 channels, 44,000 x/sec

mp3
    compressed due to a number of ingenious techniques. Some are mathematical (such as Fourier approximation); some are psychological (such as throwing away sound components that are believed inaudible). Note that if several frequencies are mixed together in one chord, the ear can hear them all. This is quite different from video, where if a beam of light contains ten different wavelengths, it can be replaced with a three-wavelength RGB approximation that is completely indistinguishable from the original.

Computers and Electricity

How do we represent bits with electricity?
simple gates: not, and, or, xor, nand, nor
truth tables
or gates are not trivial! Though they would be if electrons were more cooperative....




Python

functions: roots(a,b,c), binary(n),
    parameters
    return values versus side effects
    multiple functions make a program

while loops

Here are a few examples of numeric while loops. Do not forget the i=i+1 increment!

To find the sum of the numbers from 1 to N: (also look at for version)

	N = 100
i= 1
sum = 0
while i<=N:
sum = sum + i
i = i+1
The value of 1+2+...+N is now in variable sum.

Miller & Ranum example, p 30:
while not done:
    x = input("enter x: ")
    if x<0:
        done=True
    else:
        sum += x

What is this? It's not a function!
print sum
sum

To find the sum of the numbers in a list L:

    L=[2,3,5,7,11,13,17,19]
i=0
sum = 0
while i<len(L):
sum = sum + L[i]
i = i+1

To reverse a string S. The reversed string will be in the variable revS, at the end of the loop.

    S = "hello, world"
i = len(S)-1; # last position of S
revS = ""
while i>=0:
revS = revS + S[i]
i = i-1 # this time i is DECREMENTED