Lecture 2&3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Computer Operations

• Fact 1: Everything in a computer is a number


– Sure. Things like 102 and 3.9 are numbers
– But what about text and images and sound?
– Everything!
• Fact 2: Computers can only work with or "see"
1 or 2 numbers at a time.

• Humans process information differently


– Therein lies some of the difficultly of learning
programming
Example (1)
• What do you see?
– The letter 'a'!

• What does the computer see?


– A number; each text character is
coded to a number
• Example: Character map / Insert
symbol
Text Representation
• Most common
character code is
ASCII (UTF-8)
• Every character,
even non-printing,
characters have a
corresponding
numbers
– Decimal (base 10) /
Hexadecimal (base 16)

https://www.commfront.com/pages/ascii-chart
Example (2)
• What do you see?
– A circle!

• What does the computer see?


– Coordinate pairs of each "pixel"
– …or… (x,y)=(56,103)
(x,y)=(57,102)
– r = 120; origin = (10, 14) (x,y)=(59,101)
(x,y)=(60,100)
– Computer has to enumerate and visit
each location and color it black
Example (3)
• What do you see?
– A man's face!

• What does the computer see?


– Many numbers 0
64
0
64
0
64
0
0
(aka pixels) 128 192 192 0
192 192 128 64
– Value corresponds
Individual
Pixels

to color Image taken f rom the photo "Robin Jeffers at Ton


House" (1927) by Edward Weston
The Connection with Mathematics
Output
Pixel

• Brightness
– Each pixel value is
increased/decreased by a constant Input
amount Pixel

– Pnew = Pold + B
• B > 0 = brighter + Contrast
• B < 0 = less bright
• Contrast
– Each pixel value is multiplied by a
constant amount
- Brightness Original + Brightness
– Pnew = C*Pold + k
• C > 1 = more contrast
• 0 < C < 1 = less contrast
• Same operations performed on
all pixels - Contrast
"Enough" is NOT enough
• As we program we must be explicit
– Example: drawing the circle on the screen
• Being general is not sufficient; we must be explicit!
– Imagine a recipe for cinnamon rolls that simply read:
• Mix and bake the following: butter, that white powdery baking
substance, eggs, just enough sugar, and cinnamon. Enjoy!
– How much of each, how much is "enough", how long, in
what order?
• We will try to work on some of discrete math skills
that help us explicitly define and analyze our
programs
Information Representation
• All information is represented as sequences of
1's and 0's
• Kinds of information
– Numbers
– Text
– Instructions
– Sound
– Image/Video
Main Point: All of these forms of information are represented
as numbers in a computer and manipulated as such.
Why 1’s and 0’s
• Modern computer chips are
made from millions of tiny
transistors built on a chip of
silicon (usually)
• A transistor is an electronic
device that acts like a switch
– It can be on or off
– This leads to only 2 values (high or
low voltage) in computer hardware Transistor Acts as a Switch
– 1’s and 0’s are arbitrary symbols
representing high or low voltage High Voltage
+5V 1
• A single 1 or 0 is known as a bit on +12V
off or

Low Voltage
0V
0
-12V
This Photo by Unknown Author is licensed under CC BY-SA
Starting With Numbers
0 or 1
• A single bit can only represent 1 and 0 A bit

• To represent more than just 2 values


we need to use
combinations/sequences of many bits
Two bit Combinations
– A byte is defined as a group 8-bits
– A word varies in size but is usually 01000001
32-bits An example byte

0101110 11010001 10110101 01110111


An example word
Finite Size
• When we humans solve arithmetic
problems, we can just write as many
digits as we want
– If we limited our numbers to 3 digits,
our range would be limited to:

• Computers store bits in fixed-size


units (8-bits, 16-bits, 32-bits, etc.)
1 0 1 1 0 0 1 0
– This limits the range of numbers we
can generate and store
• Given n-bits, we can make 2n values
This Photo by Unknown Author is licensed under CC BY-SA
Unique Numbers
• Computers represent binary numbers using a
fixed number of bits
• Given a fixed number of bits, n, what is the
range of numbers we can make?
If n=1 bit: If n=2 bits: If n=3 bits:
0 =0 0 0 =0 0 0 0 =0
1 =1 0 1 =1 0 0 1 =1
1 1 0 =2 0 1 0 =2 Given n bits, 2n
1 1 =3 0 1 1 =3 numbers can be
21 = 2 values 1 0 0 =4 made
2 1 1 0 1 =5
1 1 0 =6
22 = 4 values
1 1 1 =7

4 2 1

23 = 8 values
Interpreting Binary Strings
• Given a string of 1’s and 0’s, you need to know the
representation system being used, before you can
understand the value of those 1’s and 0’s.
• Information (value) = Bits + Context (System)

01000001 = ?
Unsigned
Binary system ASCII
Instructions system

6510 ‘A’ASCII
"add 1"
Unsigned Binary Number System
• Humans use the decimal number system
– Based on number 10
– 10 digits: [0-9]
• Because computer hardware uses digital
signals with 2 values, computers use the
binary number system
– Based on number 2
– 2 binary digits (a.k.a bits): [0,1]
Number System Theory
• The written digits have implied place values
• Place values are powers of the base (decimal = 10)
• Place value of digit to left of decimal point is 100 and ascend
from there, negative powers of 10 to the right of the decimal
point
• The value of the number is the sum of each digit times its
implied place value
Most Least
Significant Significant
Digit (MSD) Digit (LSD)
base

(852.7)10= 8*102 + 5*101 + 2*100 + 7*10-1

place values
digits
Binary Number System
• Place values are powers of 2
• The value of the number is the sum of each bit times
its implied place value (power of 2)

base

(110.1)2 =
Binary Number System
• Place values are powers of 2
• The value of the number is the sum of each bit times
its implied place value (power of 2)
Most Least
Significant Significant Bit
Bit (MSB) (LSB)
base

(110.1)2 = 1 * 22 + 1 * 21 + 0 * 20 + 1 * 2-1

place values
bits

(110.1)2 = 1*4 + 1*2 + 1*.5 = 4+2+.5 = 6.510


Powers of 2
• Might help to memorize the powers of 2
20 = 1
21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128 1024 512 256 128 64 32 16 8 4 2 1
28 = 256
29 = 512
210 = 1024
Interpreting Binary Strings
• Information (65) = Bits + Context (Unsigned Binary System)

01000001 = ?
Unsigned ASCII
Binary system Instructions system

6510 ‘A’ASCII
"add 1"
Sign
• Is there any limitation if we only use the powers of
some base as our weights?
– Can't make negative numbers
• What if we change things
– How do humans represent negative numbers?
– Can we do something similar?

64 32 16 8 4 2 1

128 64 32 16 8 4 2 1
C Integer Data Types
– Integer Types (signed by default… unsigned with optional
leading keyword)

C Type C Type (Unsigned) Bytes Bits Signed Range Unsigned


(Signed) Range
char unsigned char 1 8 -128 to +127 0 to 255
short unsigned short 2 16 -32768 to +32767 0 to 65535
int unsigned int 4 32 -2 billion to 0 to 4 billion
+2 billion
long unsigned long 8 64 -8*1018 to +8*1018 0 to 16*1018
Text
• Text characters are usually represented with
some kind of binary code (mapping of
character to a binary number such as 'a' =
01100001 bin = 97 dec)
• ASCII = Traditionally an 8-bit code
– How many combinations (i.e. characters)?
– English only
• UNICODE = 16-bit code
– How many combinations?
– Most languages w/ an alphabet
• In C/C++ a single printing/text character
must appear between single-quotes (')
– Example: 'a', '!', 'Z'
http://www.theasciicode.com.ar/
Text Strings
• To represent words and sentences we can use
a string of characters
– C++ uses double-quote (") to group the characters
that are part of a string
• Example:
– "Hello\n"
– Each character is converted to ASCII equivalent
• 'H' = 72, 'e' = 101, …
• '\n' = Newline or Line Feed (LF) = Represents the non-printing
character "Enter/Return" and moves the cursor to the start of the
next line
DATA STORAGE & COMPUTER
MEMORY
Computer Components
Combine 2c. Flour

• Processor
Mix in 3 eggs
Instructions

– Executes the program


and performs all the Data
Processor
operations (Reads instructions,
operates on data)
• Main Memory
– Stores data and
Processor
program (instructions)
Software
– RAM = read and write Program
but volatile (lose values
when power off) Program
Input (Instructions) Output
• Let's look more at Devices OS
(upon
Data
(Operands)
Devices

memory starting
the app)
Memory
(RAM)
Data
Memory
• Set of cells that each store a group of bits
(usually, 1 byte = 8 bits)
• Unique address assigned to each cell Address Data

0 11010010
– Used to reference the value in that location
1 01001011
• Analogy: Safe-deposit or mail boxes 2 10010000
– Each has an identifying number and a value 3 11110100
stored inside 4 01101000
– The value can be an instruction, a number, a 5 11010001
character, etc. (You the programmer must …
know what to expect and how to interpret it!)

1023 00001011

Memory
Device
Memory Operations
• Memories perform 2 operations
0 11010010
1 01001011
2 Addr.
– Read: retrieves data value in a 10010000 Data
2 10010000

particular location (specified using 3 11110100

the address) 4 01101000


11010001
• You, the programmer, must know what 5

type (integer, character, etc.) that data …


Read Control
is.
– Write: changes data in a location 1023 00001011
to a new value
A Read Operation
• To perform these operations a
set of address, data, and control 0 11010010

inputs/outputs are used 5 Addr.


1
2
01001011
10010000
– Note: A group of wires/signals is 00000110 Data 3 11110100
referred to as a ‘bus’ 4 01101000

– Thus, we say that memories have 5 00000110



an address, data, and contr ol bus.
Write Control

1023 00001011

A Write Operation
One At a Time
• Recall that while we see the image of a man, the computer
"sees" a collection of numbers (aka pixels)?
• Now we can understand why
– Every number is stored as bits in memory
– Memory can only be accessed one data value at a time
• This limitation of accessing one value at a time leads to a
fundamental issue of programming: How do we break abstract
tasks into a sequence of "1 at a time" operations? 0
Data
0000 0000
0 0 0 0 1 0000 0000

64 64 64 0 Addr. 2 0000 0000


3 0000 0000
128 192 192 0
4 0100 0000
Individual 192 192 128 64
5 0100 0000
Pixels
6 0100 0000
7 0000 0000

Image taken f rom the photo "Robin Jeffers at Ton 8 1000 0000
House" (1927) by Edward Weston

Exercise
• Show how "cs 102" would be
stored in the memory below
– Use decimal to represent each byte
• How do we indicate the string is
done ("terminated")
– With special NULL character (i.e. 0)

0
1
2
3
4
5
6 …
7
Additional Resources

https://www.youtube.com/watch?v=wgbV6DLVezo

You might also like