Hello World! An Invitation to Computer Science

Lecture Slides: Binary Numbers

Thursday, February 14, 2008


DON'T PANIC!


Key ideas from last time


What are computing agents made of?

computing agents should be simple and substrate neutral:
  it does not matter what it is made out of

thus, standardized binary representations:
  we want simplest possible way of representing information


Bits


External v. internal representations


Positional number systems


Numbers as lists of digits

  3705 =   3000    +   700                +    5 
       = 3 x 1000  +  7 x 100  +  0 x 10  +  5 x 1
       = 3 x 103   +  7 x 102  +  0 x 101  +  5 x 100

power as column number, starting at 0, reading right to left

number as list of digits, backwards
  3705 as L1, L2, L3, L4: 5, 0, 7, 3


Binary numbers

  base ten   base two           base ten   base two
  -------------------------------------------------
       0        0                  8         1000
       1        1                  9         1001
       2       10                 10         1010
       3       11                 11         1011
       4      100                 12         1100
       5      101                 13         1101 
       6      110                 14         1110
       7      111                 15         1111

From binary to decimal

first method:

From binary to decimal

second method:
  input list of bits 
  d ← 0
  i ← 1
  while i ≤ n
    d ← d * 2
    d ← d + Bi
    i ← i + 1
  output d

From decimal to binary

first method:

From decimal to binary

second method:
  input base-10 number d
  if d is 0
     B is list with just one item on it: 0
  else 
     create a list B with nothing on it
     x ← d
     while x > 0
         let q, r be quotient and remainder of x / 2
         add r onto front of list B
         x ← q
  output list B