Hello World! An Invitation to Computer Science

Lecture Slides: Binary Arithmetic

Monday, February 18, 2008


Key ideas from last time


Hexadecimal numbers: Base 16

                  hex                           hex
  -------------------------------------------------
       0      0    0             8      1000      8
       1      1    1             9      1001	  9
       2     10    2            10      1010	  A
       3     11    3            11      1011	  B
       4    100    4            12      1100	  C
       5    101    5            13      1101 	  D
       6    110    6            14      1110	  E
       7    111    7            15      1111	  F

compact, easy (for humans) to read
simple to convert binary ⇔ hex


Real computers are finite


Binary arithmetic


Binary addition example

   13            1101
+  17        +  10001
-----        --------
   30           11110

Binary multiplication example

 
   5          101
x  6       x  110 
----       ------
  30            0
             1010
          + 10100
          -------
            11110

Shifting left and right

examples:
2310 = 101112 = 1x24 + 0x23 + 1x22 + 1x21 + 1x20
4610 = 1011102 = 1x25 + 0x24 + 1x23 + 1x22 + 1x21 + 0x20
1110 = 10112 = 1x23 + 0x22 + 1x21 + 1x20


It's all 1s and 0s


Bitmaps

 ***    00111          
   *    00001
 ***    00111    →  0011100001001110000100111
   *    00001
 ***    00111