CS150 - Fall 2011 - Class 24

  • administrative
       - final exam (let me know by the end of this week if you'd like to switch)   
       - final exam review on Friday

  • number representation
       - when you type:

          >> x = 17
       
          how does the computer actually store this information, specifically the number 17?

       - if you look far enough down into the details of what's going on when you run your computer, almost everything is represented using binary, i.e. some combination of 1's and 0's
       - why binary?
          - a combination of ease of use and cost
          - basic idea: "1" electrical current is flowing, "0" current is not flowing
          - transistors can do built to do this efficiently, cheaply and at a extremely small scale
          - could do other representations, but this is what is most effective right now... (and has been for a long time)
       
  • decimal numbers
       - most commonly used number systems have a "base"
          - each digit in the number can range from 0...base-1
          - each digit in the number has an index, starting at the right-most digit with 0 and working up to the left
          - the contribution of the digit is:

             base^index * value

          - by summing up the contribution of all of the digits, we get the value of the number
       
       - what is the base for our numbering system?
          - 10

          - for example
             - 245 = 5*10^0 + 4*10^1 + 2*10^2
             - 80498 = 8*10^0 + 9*10^1 + 4*10^2 + 0*10^3 + 8*10^4

  • binary numbers
       - we can represent numbers using any base we want
       - binary numbers are numbers represented as base 2
          - digits can only be a 0 or a 1

          - for example, the following binary numbers:
             - 101 = 1*2^0 + 0*2^2 + 1*2^2 = 5
             - 11 = 1*2^0 + 1*2^1 = 3
             - 10111 = 1*2^0 + 1*2^1 + 1*2^2 + 0*2^3 + 1*2^4 = 23
             - 100001 = 1*2^0 + 1*2^5 = 33

  • adding binary numbers
       - we can add binary numbers, just like we can add decimal numbers
       - to do it by hand, think back to how you did it in elementary school...
          - start at the right-most digits
          - add them together
          - if the sum <= base
             - that's the value for that digit in the final sum
          - if the sum >= base
             - then carry a 1 over to the next column and subtract the base from the sum
             - the value for that digit is the sum - base
          - repeat for each digit
             - if there is a 1 carried over, also include that in the sum
       - binary addition works the same way:

    		  10111
    		+ 00101
    		  -----
    
    		     1
    		  10111
    		+ 00101
    		  -----
    		      0
    
    
    		    11
    		  10111
    		+ 00101
    		  -----
    		     00
    
    		   111
    		  10111
    		+ 00101
    		  -----
    		    100
    
    		   111
    		  10111
    		+ 00101
    		  -----
    		   1100
    
    
    		   111
    		  10111
    		+ 00101
    		  -----
    		  11100
    

       - we can check our answer
          - 10111 = 1 + 2 + 4 + 16 = 23
          - 101 = 1 + 4 = 5
          - 11100 = 4 + 8 + 16 = 28

  • subtracting in binary?
       - again, same idea as with decimal
          - work right to left
          - if the top digit > bottom digit
             - subtract the digits and store that as the answer
          - if the top digit < bottom digit
             - borrow 1 from the next number up (may have to keep looking up)
             - ...
          - ...

  • how do we represent negative numbers in binary?
       - remember, all we have is 1's and 0's (we can't do a negative sign '-')
       - one option...
          - indicate the sign with the most significant bit
       
             - 0101 = positive 101 = 5
             - 1101 = negative 101 = -5
          
          - any concerns with this approach?
             - two zeros (both a positive and a negative version)
                - 1000
                - 0000
             - turns out to be more work than other representations
                - have to check the sign bit when doing addition or subtraction
       - the way most computers do it: twos complement representation
          - http://en.wikipedia.org/wiki/Two's_complement

  • Course summarized
       - look at bbq.py code
       - look at url_basics.py code
          - we've come a long way!
          - you've learned how to program
       - 3 programming languages
          - Python
          - R
          - Matlab
       - Learned to interact with the file system through Terminal
       - 4827 lines of notes
          - assuming 34 lines per page: 141 pages on notes!
       - 1745 lines of code in the examples
          - ~126 different functions
       - you've written over 1000 lines of code yourself (and probably much more)
       - 2/3rds of the book
          - ~175 pages of reading
       - 21 problem sets