Lecture 2&3
Lecture 2&3
Lecture 2&3
https://www.commfront.com/pages/ascii-chart
Example (2)
• What do you see?
– A circle!
• 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
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
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
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)
• Processor
Mix in 3 eggs
Instructions
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
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
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