knerr cs21 notes...

back to schedule

WEEK02: numbers and strings
---------------------------------------------------------------
 F: QUIZ 1, binary numbers, ord/chr and ASCII, use of % operator

LAB2: due next Tuesday

- need to be able to specify different values:
    ints: 1 200 -3590
    floats: 4.6 -921.6  6.023x10^23
    characters: 'a' 'b' 'G'

 and we need to be able to do stuff with those values, like
 add and subtract them, store and recall them, or apply logical 
 operations to them (if x is greater than 0, do this)

- computers are good with two values: voltage or no voltage
- let's call these two values 1 and 0

- can we represent ints, floats, and characters with just 1's and 0's?

    0                 0
    1                 1        bit = binary digit
    2                10
    3                11
    4               100
    5               101
    6               110
    7               111
    8              1000
    9
   10
   11

- ASCII encoding for characters:

>>> ord('a')
97
>>> ord('b')
98
>>> chr(99)
'c'
>>> chr(ord('a') + 10)
'k'

- 32-bit integer has it's limits:

>>> x = 2147483647
>>> type(x)
<type 'int'>
>>> x = x + 1
>>> print x
2147483648
>>> type(x)
<type 'long'>

>>> 2**3
8
>>> 2**31
2147483648L

- floating point values: 1 bit for sign (+ or -)
                         8 bits for range (exponent)
                        23 bits for precision (fraction)

         example: 1.025 x 10^23
                 
             sign = +
             exponent = 23
             fraction = 025

   even floats have their limits:

>>> x = 2.0**1023
>>> print x
8.98846567431e+307
>>> x = x * 10
>>> print x
inf

   and we don't have infinite precision, so that's why you see
   things like this:

>>> 8.2
8.1999999999999993
>>> 1.0/3.0
0.33333333333333331

>>> x = 1000000
>>> dx = 0.000001
>>> dx * x
1.0
>>> for i in range(1000000):
...   x = x + dx
... 
>>> print x
1000001.00001

- we can do addition and stuff with binary numbers:

     1 + 2 = 3                     2 + 2 = 4

      001                            010
    + 010                          + 010
    -----                          -----
      011                            100

- how do we do this with electric circuits???

    > transistor = electrically-controlled switch
    > can combine transistors together to make AND, OR circuits

        A  B | A and B  |  A or B
        -------------------------
        0  0      0          0
        0  1      0          1
        1  0      0          1
        1  1      1          1

    > can combine AND and OR circuits together to make ADDER circuit

    > Pentium II processor (1998) has 7 million transistors!!
 
  ** take CS33 if you find this interesting!!

- why is the % operator useful???


          #  |  # % 7
         ------------
          4  |    4
          5  |    5
          6  |    6
          7  |    0
          8  |    1
          9  |    2
          .. |   ...
         12  |    5
         13  |    6
         14  |    0
         15  |    1


"""
show how to use % operator to loop back to the begining:

  today is Friday
  the next day is Saturday
  the next day is Sunday
"""

def main():

  daylist = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

  day = 5
  print "today is %s" % (daylist[day])

  for i in range(10):
    day = (day + 1) % 7
    print "the next day is %s" % (daylist[day])


main()


*** Bonus/Fun program...can you use % and ord/chr to do this?

$ python ccipher.py 

  This program does a cyclic shift on a phrase you enter.
  Please enter a phrase below and a shift value below.
  
phrase: abcde wxyz
 shift: 2

   >>>  cdefg yzab 

$ python ccipher.py 

  This program does a cyclic shift on a phrase you enter.
  Please enter a phrase below and a shift value below.
  
phrase: the jewels are in the garage 
 shift: 2

   >>>  vjg lgygnu ctg kp vjg ictcig