0% found this document useful (0 votes)
10 views68 pages

Module01-02 Intro Bits-Bytes

The document outlines the introductory modules for a course on Computer Systems at the University of Pennsylvania, focusing on the motivation for the course, course structure, and the fundamentals of binary numbers. It emphasizes understanding computer abstractions and the relationship between hardware and software, along with practical programming in C. Key topics include binary representation, arithmetic, and the implications of finite width in computer architecture.

Uploaded by

sridharkshenoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views68 pages

Module01-02 Intro Bits-Bytes

The document outlines the introductory modules for a course on Computer Systems at the University of Pennsylvania, focusing on the motivation for the course, course structure, and the fundamentals of binary numbers. It emphasizes understanding computer abstractions and the relationship between hardware and software, along with practical programming in C. Key topics include binary representation, arithmetic, and the implications of finite width in computer architecture.

Uploaded by

sridharkshenoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

CIS 2400 / CIT 5930

Introduction to
Computer Systems
University of Pennsylvania
School of Engineering and Applied Science
Computer and Information Systems Department
Dr. Thomas Farmer

MODULE 01/02: Course Intro & Binary #s


Introduction to Computer Systems

Overview of Module 01/02

• Section 1:
• Motivation for taking this course
• What is an abstraction in Computer Science?

• Section 2:
• Course Structure
• Logistics
• Keys to Success

• Section 3:
• Bits & Bytes…The world of binary #s!

Property of Penn Engineering 2


Introduction to Computer Systems

SECTION 1

COURSE MOTIVATION AND ABSTRACTIONS

Property of Penn Engineering 3


Introduction to Computer Systems

Welcome!!…What is this Course About?!

• Any sufficiently advanced technology is indistinguishable from magic.


• Arthur C. Clarke, "Profiles of The Future” (Clarke's 3rd law)

• No magic
• Computers should not be magic to computer scientists!

• Bottom up approach
• Computing systems from transistors on up
• Many of you have already seen them from the middle (Java)

• Not about “design”


• How things work, not how they work well

Property of Penn Engineering 4


Introduction to Computer Systems

Welcome!!…What is this Course About?!

• Look “under the hood” to see how a computer works


• Explore the interface between hardware and software

• With this knowledge you can:


• Write better, more efficient software
• Design better hardware
• Appreciate the abstractions that are built on top of these
• foundations

Property of Penn Engineering 5


Introduction to Computer Systems

Abstractions

• Abstractions are one of the principal tools that computer scientists use to handle
complexity
• Non-CS example: Taking a taxi to the airport!

• Some examples of abstractions in Computer Science


• High Level abstractions
• SQL, functional programming languages
• Mid Level abstractions
• Java, C#, Python, C
• Low Level models
• Assembly language, ISA
• Device Level
• gates, memories
• Device Physics
• memresistors, quantum dots.

Property of Penn Engineering 6


Introduction to Computer Systems

SECTION 2
COURSE STRUCTURE, LOGISTICS, KEYS TO SUCCESS!

Property of Penn Engineering 7


Introduction to Computer Systems

Course Structure

• 1/3 of the course – spent on hardware


• Transistors → Logic Gates → Memory → 16 bit CPU!

• 1/3 of the course – spent on programming our CPU


• Machine Language → Assembly Language

• 1/3 of the course – spent on programming in C


• Assembly Language → C Language
• How are C constructs compiled into Assembly language
• How Memory is used & managed in C: Stack vs. Heap

• Every week, we’ll go up one level of abstraction: bits up to CPU to programming!

Hardware Assembly C-
Language Property of Penn Engineering 8
Introduction to Computer Systems

Why C?

• C is the ultimate high-level, low-level language


• High level enough that you can write large scale programs in It
• Low level enough that you can reach down and twiddle the bits

• C is the most popular programming language of all time


• Invented in 1972 at AT&T Bell Laboratories (the google/facebook of its day!)
• Every OS is coded at its core in C: Mac OS / Linux / C
• If you learn C, you can learn any language!

C-
Language
? Property of Penn Engineering 9
Introduction to Computer Systems

Keys To Success: One zombie – no problem

Property of Penn Engineering 12


Introduction to Computer Systems

Lots of zombies at the same time - problem

Property of Penn Engineering 13


Introduction to Computer Systems

SECTION 3
BITS, BYTES, THE BINARY WORLD!

Property of Penn Engineering 14


Introduction to Computer Systems

What Is a Bit?

• A “bit” is a basic unit of information in a computer


• The word “bit” is a contraction of two words:
• BIT = “Binary digit”

Binary (base two): Digital (discrete):


• Two symbols/states/values • Finite number of symbols
• 0 and 1 (or letters ‘O’ and ‘I’) • Opposite is “analog” (continuous)
• Think: ON / OFF switch! • Think: Dimmer switch!

 What can a bit represent?  Names for groups of bits:


• Just 1 bit can represent 2 things: 0 or 1 • 1 bit – just a “bit”
• 2-bits can represents 4 things: 00, 01, 10, 11 • 4 bits together – known as a “nibble”
• In general: n-bits → 2n possible things • 8 bits together – called a “byte”
• A “word” in a computer – machine dependent!
➢ # of bits computer can process in one operation

Property of Penn Engineering 15


Introduction to Computer Systems

Why Use a Binary System?

• Computers are electrical at base…


• Electrical: Operates by controlling the flow of electrons

• Components in electrical circuits have two easily recognizable states:


• Absence of voltage and presence of voltage (OFF…ON)
• Define one of these as 0, other as 1

• Alternative: define multiple discrete values in voltage range


• Problem: circuits would become much more complex

Property of Penn Engineering 16


Introduction to Computer Systems

What Does “Binary Digital


System” Imply?
• Everything is sequences of 0s and 1s
• Text – characters, strings, …
• Numbers – integers, reals, …
• Images – pixels, colors, shapes, …
• Sounds
• Instructions

• The fact that instructions are also “just 0s and 1s” makes a computer universal: programs are just
another kind of data!

Property of Penn Engineering 17


Introduction to Computer Systems

SECTION 3: Part 2

WEIGHTED POSITIONAL
REPRESENTATION

Property of Penn Engineering 18


Introduction to Computer Systems

Representation of Integer in
Binary
• To represent an integer in a binary system, we use:

weighted positional representation

• We use this in our normal decimal world too!


• For example…a decimal number you are used to: “329”
• “3” is worth 300, because of its position (most significant)
• “9” is only worth 9 (least significant)

base-10 most least base-2


(decimal) 329 significant 101 significant (binary)
102 101 100 22 21 20

3x100 + 2x10 + 9x1 = 329 1x4 + 0x2 + 1x1 = 5

Easy to convert a binary # to decimal!


• Notice: 101 in binary, is actually 5 in decimal
Property of Penn Engineering 19
Introduction to Computer Systems

Converting a Decimal Integer to


Binary Integer
On the last slide we saw how to convert a binary # to decimal. What about the other way?

There are a couple of techniques, this is probably the easiest one!

1. Start with a decimal #, as an example: X=104

2. Divide by two – keep the remainder; it’s the least significant bit!

3. Keep dividing by two until answer is zero, recording remainders from right to left

X = 104decimal 104/2 = 52 r0 0th bit


52/2 = 26 r0 1st bit
26/2 = 13 r0 2nd bit
13/2 = 6 r1 3rd bit
6/2 = 3 r0 4th bit
3/2 = 1 r1 5th bit
1/2 = 0 r1 6th bit
X= 0 1 1 0 1 0 0 0 binary

I’ve added a leading 0 to make this a full “byte” Property of Penn Engineering 20
Introduction to Computer Systems

SECTION 3: Part 3

BINARY ARITHMETIC

Property of Penn Engineering 21


Introduction to Computer Systems

Binary # Table
Using Weighted Positional Representation
• This table shows the all of the “unsigned” numbers we can make with 2-bits & 3-bits

2-bit Binary| Decimal 3-bit Binary | Decimal


0 0 0 0 0 0 0
0 1 1 0 0 1 1
1 0 2 0 1 0 2
1 1 3 0 1 1 3
1 0 0 4
1 0 1 5
1 1 0 6
1 1 1 7

Property of Penn Engineering 22


Introduction to Computer Systems

Binary Arithmetic

• Binary addition – just like base-10!


• Add from right to left, propagating carry

carry

10010 (18) 10010 (18) 01111 (15)

+ 01001 (9) + 01011 (11) + 00001 (1)

11011 (27) 11101 (29) 10000 (16)

Subtraction, multiplication, division also similar to base-10


Nice implementation of this in hardware

Property of Penn Engineering 23


Introduction to Computer Systems

Binary Multiplication

• Recall how we multiply numbers in decimal


• In binary, the process is the same, except we don’t need to memorize our times tables!
• Note: n-bit multiplication produces 2n-bit results
• In this example: Multiplying 2 “4-bit” numbers in binary produced an “8-bit” number

• Binary Multiplication uses the “shift and add” technique


• We’ll implement this in hardware using a “shifter” and an “adder”

1310 11012
x 1210 x 11002
--------- -------
26 0000
+ 130 00000
--------- 110100
15610 + 1101000
----------
100111002

Property of Penn Engineering 24


Introduction to Computer Systems

SECTION 3: Part 3

WIDTH AND OVERFLOW

Property of Penn Engineering 25


Introduction to Computer Systems

Finite Width and Overflow


(For Positive #s and Zero)
• In math…Integers have infinite width
• There are an infinite number of them

• In a computer…Integers have finite width


• As defined by their architecture
• Limited by hardware circuits themselves
• 64-bit these days (264 integers)

• Overflow: when operation result is outside type’s range


• Example: 15 + 1 with 4-bit integers (16 needs 5 bits, 10000)
• This example: our computer has a 4-bit width, and we’ve gone past it!

1111 (15)
overflow + 0001 (1)
(carry-out)
10000 (16)

Property of Penn Engineering 26


Introduction to Computer Systems

Finite Width and Programming


Languages
• On a real computer, each memory storage location can only store a finite number of bits
• For example, we can talk about a 16 bit machine, a 32 bit machine or a 64 bit machine
• The fact that the actual storage locations are limited, caps the size of the numbers that we can store and
manipulate

• These limitations also show up in programming languages; different basic types have different sizes
• Some basic types in C
• char – typically 8 bits
• short int – typically 16 bits
• int – typically 32 bits
• long int – typically 64 bits

• Note these sizes are not guaranteed and can change on different architectures

Property of Penn Engineering 27


Introduction to Computer Systems

How Overflow Can Be Handled


on a Computer
• In general: the sum of 2 n-bit numbers can have n+1-bits

• And the product of 2 n-bit numbers can have 2n-bits


• Think about this in decimal
• What to do?

• Addition: architecture remembers overflow (carry-out) bit


• If you care, can ask whether previous operation overflowed…
• … or set a mode in which overflowed operations raise exceptions
• Few programs care …
• Often modulo (i.e., “wrap-around”) addition is desired behavior

• Multiplication: architecture stores upper n-bits in register


• Can use these if you want

Property of Penn Engineering 28


Introduction to Computer Systems

Terminology so Far…

• 1 bit = a bit

• 4 bits = a nibble

• 8 bits = a byte

• Overflow
• For unsigned numbers…when width of data goes beyond the width of the machine
• 4-bit machine, cannot hold a 5-bit number!

• In binary #’s
• Positive #s and zero are referred to as “unsigned #s”
• Because they don’t require a +/- sign, we are safe to assume they are positive!

• Next we’ll discuss “signed” binary #s

Property of Penn Engineering 29


Introduction to Computer Systems

SECTION 3: Part 4

REPRESENTATION OF NEGATIVE
INTEGERS IN BINARY (2C)

Property of Penn Engineering 30


Introduction to Computer Systems

How Do We Represent Negative


Integers in Binary?
• 0 and positive #’s are easy to store in a computer
• As you have seen!

• Negative #’s aren’t as obvious


• In math, we simply put the “-” sign out front
• But in a computer, a system where we only have 0 or 1, this is not as easy to do!

• The first development in computing history was “sign-magnitude”


• Same idea as in Math class: place a “-” sign in front of a number
• But, instead of a negative sign, a “1” was used…for example:
• 0001 is “+1”
• 1001 is “-1”….the leading # indicates the sign of the number
• Seemed ok except for two problems:
• 0000 and 1000…two representations for 0!!
• Adding and subtracting these signed magnitude numbers didn’t work at all!

Property of Penn Engineering 31


Introduction to Computer Systems

Negative Numbers in Binary:


1's Complement
• The next idea was One’s Complement (1C)

• Technique
• Take the positive representation of a binary # and “flip” AKA “complement” each bit

• Example
• Assuming a 4-bit wide computer
• +5 in 1’s complement would be: 0101 (you must pad w/ leading 0)
• -5 in 1’s complement would be: 1010 (you simply flip each digit)

• Good features
• Easy to do, addition works better 0101 + 1010 = 1111 (which is 0)
• The leading 1 indicated a negative number (like sign-magnitude)

• Problem with 1C
• Still two representations of 0: 0000 … AND … 1111

Property of Penn Engineering 32


Introduction to Computer Systems

Negative Numbers in Binary:


2's Complement
• The way we do it today…is Two’s Complement (2C)

• Technique to convert a positive # to its negative representation (& vice-versa!)


• Take the positive representation of a binary # and “flip” AKA “complement” each bit (1C)
• Next, add “1” to the resulting “complemented” bits
• Then “throw away” any resulting carry-out past the width of the machine

• Example
• Assuming a 4-bit wide computer
• +5 in 2’s complement would be: 0101 (you must pad w/ leading 0)
• -5 in 1’s complement would be: 1010 (recall we flipped each bit)
• -5 in 2’s complement would be: 1011 (we add “1” to get the “2s” complement)
• If there had been a carry-out…we would only have stored only the lower 4-bits
• This means we “throw away” any resulting carry-out past the width of the machine
• Notice: this means overflow is going to get a new definition for 2C numbers

Property of Penn Engineering 33


Introduction to Computer Systems

4-Bit 2C as an Example

• The table shows all the 2C numbers we can make with 4-bits (16 possibilities)
• All of the positive numbers have 0 in the MSB, all of the negative numbers have 1 in the MSB
• Range of an n-bit number: –2n-1 through 2n–1 – 1
• Most negative number (–2n–1) has no positive counterpart

0 0 0 0 0 1 0 0 0 –8
0 0 0 1 1 1 0 0 1 –7
0 0 1 0 2 1 0 1 0 –6
0 0 1 1 3 1 0 1 1 –5
0 1 0 0 4 1 1 0 0 –4
0 1 0 1 5 1 1 0 1 –3
0 1 1 0 6 1 1 1 0 –2
0 1 1 1 7 1 1 1 1 –1
Note: most CPU architectures today use 2C representation
Property of Penn Engineering 34
Introduction to Computer Systems

SECTION 3: Part 5

BINARY ARITHMETIC WITH TWO’S


COMPLIMENT (2C)

Property of Penn Engineering 35


Introduction to Computer Systems

Why Use 2C?

• It solves the problems of sign-magnitude and 1C


• There is only 1 representation of 0
• Computers can add “positive” and “negative” numbers together
• Without using different hardware to do so…
• This is actually the main reason we use 2C
• The “leading 1” still indicates the # is negative…handy for programmers, and for hardware designers too!

• It has an additional bonus!


• We don’t need to make “subtractor/borrower” hardware in a computer!
• If we simply make a circuit that can generate the 2's complement of a number (a negator), then we can re-use
our adder circuit to perform addition!
• Ex: 5 – 4….is the same as 5 + (-4) !!!

Property of Penn Engineering 36


Introduction to Computer Systems

Addition in 2C

• The 2C representation is convenient because it makes ‘regular’ addition work for both positive and
negative numbers:

0101 (5) 0110 (6)


+ 1011 (-5) + 1010 (-6)
10000 (0) 10000 (0)

0111 (7) 1110 (-2)


+ 1011 (-5) + 1101 (-3)
10010 (2) 11011 (-5)

When performing 2's complement addition,


computers “throw away” the classic overflow bit!
Only store the lower n-bits of the result

Property of Penn Engineering 37


Introduction to Computer Systems

Why Does This Work?

• Consider adding two 2C numbers:


2C representation of (X+Y)

(2n + X) + (2n + Y) = 2n + (2n + (X+Y))

Extra overflow bit (discarded)

n – represents the width of your


machine

Property of Penn Engineering


38
Introduction to Computer Systems

Subtraction Simplified with 2C

• Performing subtraction with 2C numbers is simple:


• We negate the second number and add it to the first

• The operation: (X – Y) becomes: X + (-Y)

• We know how to negate and we know how to add, so subtraction comes for free!

0101 (5) 0101 (5)


- 0011 (3) + 1101 (-3)
10010 (2)

1011 (-5) 1011 (-5)


- 1010 (-6) + 0110 (6)
10001 (1)

Property of Penn Engineering 39


Introduction to Computer Systems

SECTION 3: Part 6

OVERFLOW WITH TWO’S COMPLIMENT


(2C)

Property of Penn Engineering 40


Introduction to Computer Systems

How Do You Know If You Have


an Overflow in 2C?
• For unsigned numbers (0 or positive integers)
• Any addition that produces an ‘extra bit’ is an overflow

• For 2C signed numbers


• Sometimes addition or subtraction produce an extra bit – this is not necessarily a problem
• Arithmetic overflow can occur when you are adding 2 positive or 2 negative numbers – in
this case if the sign of the result is different from the sign of the addends you have an
arithmetic overflow
• (this is the key to determining overflow condition in 2C)

• What is overflow in 2C then?


• If you add 2 positive #’s and get a negative result or
• If you add 2 negative #’s and get a positive result

Property of Penn Engineering 41


Introduction to Computer Systems

Examples of Overflow in 4-bit 2C

0111 (7) 1110 (-2)


+ 0101 (5) + 1001 (-7)
1100 (-4 ??) 10111 (+7 ??)

Not traditional overflow, With 2C, overflow isn’t just when


but wrong answer! number is carried out past MSB

• Just remember…overflow is still a problem in 2C


• It is possible to add two numbers such that the result does not fit in the 2C representation

• As a programmer you need to be aware of this and guard against arithmetic operations that could
lead to overflow
• How? In a programming language you must be aware of a “types” width and what the largest positive & negative
#s you can store!!

Property of Penn Engineering 42


Introduction to Computer Systems

SECTION 3: Part 7

TECHNIQUES TO COVERT TO/FROM 2C

Property of Penn Engineering 43


Introduction to Computer Systems

Techniques for Converting #s


Into 2C
• Easiest (the same way actual hardware does it):
• Example: -6 in decimal
• Determine “width of machine” – usually a given in problem
• For this example, let’s use 4-bits
• Convert +6 to binary (see last section): 0110
• Notice I’ve “padded” extra 0’s to make it 4-bits wide
• To “negate” or convert to 2C form, flip the bits and add 1:
• 0110…flipped: 1001
• Add 0001: 1010
• And we’re done, 1010 is (-6) in 2C form!

• The same technique works to bring the number back to decimal!


• 1010…flipped: 0101
• Add 0001: 0110
• 0110 in binary is simply 6, BUT it’s negative: so -6

Property of Penn Engineering 44


Introduction to Computer Systems

Another Technique to Convert


to 2's Complement
0101 (5) 1011 (–5) 0000 (0)
1010 (~) 0100 (~) 1111 (~)
+ 1 + 1 + 1
1011 (–5) 0101 (5) 0000 (0)

• The examples above used the technique we’ve already discussed

• Another option…a simple pencil-and-paper shortcut (for converting #’s to 2C)


• copy bits from right to left up to and including the first ‘1’
• flip remaining bits

0110 (6) 0101 (5) 1011 (-5) 1100 (-4) 0000 (0)
10 (copy) 1 (copy) 1 (copy) 100 (copy) 0000 (copy)
1010 (flip) 1011 (flip) 0101 (flip) 0100 (flip) 0000 (flip)

Property of Penn Engineering 45


Introduction to Computer Systems

3rd Technique to Convert


Numbers into 2C
• Given a 2C number X, to negate it (produce the 2C representation), we subtract it from 2 n
• In practice this is equivalent to flipping all the bits in X and adding 1 to the result
• This works for both positive and negative numbers

• As an example, if we have a 4-bit width, 24 = 10000


• We then subtract the number we wish to negate: X, from 2n
• The result is the 2C representation for X (alternatively called the negated version of X)

10000 (24) 10000 (24)

- 0101 (5) - 1010 (-6)

1011 (-5) 0110 (6)

Works for positive to negative As well as negative to positive

Property of Penn Engineering 46


Introduction to Computer Systems

Terminology So Far…

• 1 bit = a bit

• 4 bits = a nibble

• 8 bits = a byte

• In binary #’s
• Positive #s and zero are referred to as “unsigned #s”
• Because they don’t require a +/- sign, we are safe to assume they are positive!
• Positive #s (with sign), 0 and, Negative #s are called “signed #s”

• Overflow w/ unsigned #s…


• When you get an extra bit past the width of your machine

• Overflow w/ signed #s…


• When adding 2 positive #s you get a negative or when adding 2 negative #s you get a positive!

Property of Penn Engineering 47


Introduction to Computer Systems

SECTION 3: Part 8

SIGN EXTENSION

Property of Penn Engineering 48


Introduction to Computer Systems

Sign Extension

• Used when adding numbers of different lengths


• For unsigned #s it's very easy…we simply pad #’s with 0s
• Example 5 + 2:

510 = 0000 01012 (8 bit 2C) Notice in this example, we intuitively


210 = 000000102 (4 bit 2C) “padded” MSB with 0s?
+ ----------------------------------
0000 01112 = 710 Fancy way to say it:
We “replicated” or extended the sign bit!
• Example 5 – 2, which is done using: 5 + (-2)
• For negative #s it's also easy, we must remember to “replicate” the sign bit first

510 = 0000 01012 (8 bit 2C) 510 = 0000 01012 (8 bit 2C)
-210 = 11102 (4 bit 2C) -210 = 1111 11102 (4 bit 2C)
+ ---------------------------------- + ----------------------------------
0001 00112 = 1910 0000 00112 = +310
Wrong answer: 5-2 is not 19! Right answer!
We forgot to sign extend! We sign extended the 4-bit 2C

Property of Penn Engineering 49


Introduction to Computer Systems

Sign Extension

• When performing arithmetic operations on numbers of different lengths we must sign extend
numbers before we perform operations so that both numbers are the same length
• Rules:
• If a number is positive (leading 0), pad with 0s to meet length
• If a number is negative (leading 1), pad with 1s to meet length

• Why does it work?


• For negative numbers 2(n+1) = 2n + 2n
• So: 2(n+1) – X = 2n + (2n – X) = 2n + (2C version of (-X))
• This means that we can form the n+1 bit 2C number by taking the n bit 2C number and adding another 1
bit in the highest position (2n)

Property of Penn Engineering 50


Introduction to Computer Systems

SECTION 3: Part 9

THE ART OF ENCODING: MAPPING THE


PHYSICAL WORLD INTO OUR BINARY WORLD

Property of Penn Engineering 51


Introduction to Computer Systems

Use Binary #’s to Represent Any


Type of Information
• When data doesn’t neatly convert into numbers…we “encode” the data instead
• Decimal integers…easy to convert into binary
• But what about letters?? What about Colors, Sound, etc?

• “Encoding” data simply means an agreed upon “mapping” of data from one representation to another

• At some point, it is the choice of an engineer to define the encoding of data between two forms

• Let’s take the alphabet as an “easy” example

Property of Penn Engineering 52


Introduction to Computer Systems

How Could We Represent


Letters with #s?
aka: How do we encode the alphabet?
• We would need 26 different #s Letter
Numeric
Letter
Numeric
Encoding Encoding
• Easy to map: A→1 and Z→26 A 1 N 14
• 1-26 are decimal #s B 2 O 15
C 3 P 16
D 4 Q 17
E 5 R 18
F 6 S 19
G 7 T 20
H 8 U 21
I 9 V 22
J 10 W 23
K 11 X 24
L 12 Y 25
M 13 Z 26
Property of Penn Engineering 53
Introduction to Computer Systems

How Could We Represent


Letters with #s?
• What about a binary representation? A 0
• 1 bit, can only get us so far! B 1

• What if we use 2 bits?


A 00
• 2 bits, 4 possible combinations: 22 = 4
B 01
• We need more bits to encode all 26 letters!
C 10
D 11

Property of Penn Engineering 54


Introduction to Computer Systems

How Could We Represent


Letters with #s?
• To get 26 possible representations, we need at Binary Binary
Letter Letter
least 26 possible combinations: Encoding Encoding
• 5 bits will do the trick! A 00000 N 01101
• 25 = 32 B 00001 O 01110
• But we will have 6 extra slots… C 00010 P 01111
D 00011 Q 10000
• What about lowercase letters?
E 00100 R 10001
• 26+26 = 52 possibilities
F 00101 S 10010
• 26 = 64
G 00110 T 10011
• That could work, but what about symbols?
H 00111 U 10100
• !@#$%^&*
I 01000 V 10101
J 01001 W 10110
K 01010 X 10111
L 01011 Y 11000
M 01100 Z 11001

Property of Penn Engineering 55


Introduction to Computer Systems

ASCII Encoding (7-Bit Encoding)


Letter ASCII Code Binary Letter ASCII Code Binary

a 097 01100001 A 065 01000001


ASCII b 098 01100010 B 066 01000010
• American Standard Code for Information c 099 01100011 C 067 01000011
d 100 01100100 D 068 01000100
Interchange e 101 01100101 E 069 01000101
• 27=128 combinations f 102 01100110 F 070 01000110
g 103 01100111 G 071 01000111
• Standard encoding, developed in the 1960’s h 104 01101000 H 072 01001000
• Didn’t take into account international i 105 01101001 I 073 01001001
standards! j 106 01101010 J 074 01001010
k 107 01101011 K 075 01001011
• Notice: only need 7-bits, but pad to a byte l 108 01101100 L 076 01001100
m 109 01101101 M 077 01001101

UNICODE n
o
110
111
01101110
01101111
N
O
078
079
01001110
01001111
• Today we use UNICODE p 112 01110000 P 080 01010000
q 113 01110001 Q 081 01010001
• Multi-byte encoding r 114 01110010 R 082 01010010
• 232 possibilities! s 115 01110011 S 083 01010011
• ASCII is a 8-bit subset of UNICODE (UTF-8) t 116 01110100 T 084 01010100
u 117 01110101 U 085 01010101
v 118 01110110 V 086 01010110
w 119 01110111 W 087 01010111
x 120 01111000 X 088 01011000
y 121 01111001 Y 089 01011001
z 122 01111010 Z 090 01011010

Property of Penn Engineering 56


Introduction to Computer Systems

LIGHTS, CAMERA, ACTION…


LET’S ENCODE SOUND, PICTURES, & VIDEO!

Property of Penn Engineering 57


Introduction to Computer Systems

Representing Sound in Binary

• Microphones are an example of ‘transducers’ that


convert sound waves to electric signals (e.g. a
voltage)

• Sounds (speech, music, anything we can here)


are carried through the air by compressing and
expanding air molecules

• Microphones have diaphragms that move in


response to those compressions and expansions
in the air.
• The diaphragm is attached to a coil of wire that
can move around a permanent magnet. If you’ve
heard of Michael Faraday, you’ll recall his famous
experiment where he moved a magnet through A
coil of wire and “induced a current”
• When the diaphragm moves in response to the
sound wave, a current is induced in the coil,
which is an electrical equivalent of the incoming
sound wave!
microphone

Property of Penn Engineering 58


Introduction to Computer Systems

Representing Sound in Binary


• We can take the electrical output of a
microphone (e.g. a voltage) and convert
the signal to discrete values
Voltage
• Imagine the x-axis is ‘time’ and we measure
the voltage consistently every 1ms and
generate the following coordinate pairs:
5V
4.8V
4V
• (0,0), (1ms, 1V), (2ms, 2.5V), (3ms, 4V), (4ms, 5V),
3.8V etc.
2.5V • This is called ‘sampling’, which turns
2.4V something continuous (like this sine wave)
into something discrete
1V
.5V
Time
• We know we sampled every 1ms, so we can
throw away the x values:
• 0,1,2.5,4,5, etc.

• Since these are just #’s, we can easily


convert them to binary!
• Our computer can work with binary!
• If we wish to reconstruct this sine wave
someday, we just reverse the process and
Want a better approximation? Increase the ‘sample output it through a ‘speaker’ (which is a
microphone run backwards!
rate’, which means taking samples more often. We just
need more bits to store the result! Property of Penn Engineering 59
Introduction to Computer Systems

Representing Images/Video,
Graphics in Binary
• Pixel representation
• Breaking image down into discrete points… X=0,y=25
6
• Apply a coordinate system
• At each point determine intensity of the
color
• RGB

• Convert these numbers to binary


representation

• More detail will need


• More pixels
• More numbers
• More bits!

X=256,y=
X=0,y=0
0

Property of Penn Engineering 60


Introduction to Computer Systems

SECTION 3: Part 10

REPRESENTING NUMBERS IN
HEXADECIMAL

Property of Penn Engineering 61


Introduction to Computer Systems

Hexadecimal (Base-16) Notation


Binary Hex Decimal Binary Hex Decimal
0000 0 0 1000 8 8
0001 1 1 1001 9 9
0010 2 2 1010 A 10
0011 3 3 1011 B 11
0100 4 4 1100 C 12
0101 5 5 1101 D 13
0110 6 6 1110 E 14
0111 7 7 1111 F 15

• More compact and convenient than binary (base-2)


• Fewer digits: four bits per hex digit → less error prone
• Just a notation, not a different machine representation
• Most languages (including C and LC-4) parse hex constants
• Sometimes hex numbers preceded with x or 0x
• x07 (would be hex #)
• 0x07 (another way to indicate hex #s)
• 07 (would simply be a decimal #)
Property of Penn Engineering 62
Introduction to Computer Systems

Hexadecimal (Base-16) Conversion


Binary Hex Decimal Binary Hex Decimal
0000 0 0 1000 8 8
0001 1 1 1001 9 9
0010 2 2 1010 A 10
0011 3 3 1011 B 11
0100 4 4 1100 C 12
0101 5 5 1101 D 13
0110 6 6 1110 E 14
0111 7 7 1111 F 15

• Any binary number (unsigned or signed) can be converted to HEX by translating the nibbles:
• 01101101 = 0110 1101 = x6D
• 0011011110101110 = 0011 0111 1010 1110 = x37AE

• Converting from HEX to binary is just the opposite:


• x7F = 0111 1111

Property of Penn Engineering 63


Introduction to Computer Systems

Fun with Hex Numbers

• 0xDEADC0DE

• 0xBA5EBA11

• 0xB01DFACE

• 0xBADA55

Property of Penn Engineering 64


Introduction to Computer Systems

SECTION 3: Part 11

REPRESENTING FRACTIONS IN BINARY

Property of Penn Engineering 65


Introduction to Computer Systems

Fixed-Point

• How can we represent fractions?


• “binary point” separate positive from negative powers of two
• Analogous to “decimal point”
• 2C addition and subtraction still work
• If binary points are aligned (“fixed-point”)

20 = 1 2-1 = 0.5
21 = 2 2-2 = 0.25
22 = 4 2-3 = 0.125
00101000.101 (40.625) One problem…how can
+ 11111110.110 (-1.25) you represent a binary
point when you only have
00100111.011 (39.375) 0 and 1 in a computer???

Property of Penn Engineering 66


Introduction to Computer Systems

Floating-Point

• Recall scientific notation:


• In decimal: 40.625 can also be represented as: 4.0625 x 101
• We can use scientific notation with binary #s too…
• 00101000.101 using scientific notation is: 1.01000101 x 25
• An interesting note about binary scientific notation…the number before the decimal point is ALWAYS “1”

• We can see a way to “standardize” fractions in binary if we use scientific notation:

+1.01000101 x 25 exponent
sign

fraction

We need a way to represent the 3 components: sign, fraction, and exponent

Property of Penn Engineering 67


Introduction to Computer Systems

Floating-Point Standard
IEEE 754
• The IEEE defines the representation of a 32-bit signed floating point number N as follows

1 8-bits 23-bits
N= S exponent fraction

32 bits

• Where N = (-1)S × 1.fraction × 2exponent-127


• “127” is known as the bias
• It allows us to have a very big range for the exponent (from -127 to +128)
• Allowing us to represent very big and very small #s w/o using 2C for the exponent

Property of Penn Engineering 68


Introduction to Computer Systems

Floating-Point Standard
Example
1 8-bits 23-bits
N= S exponent fraction

• Example: N = 11000001011010000000000000000000

• The most significant bit is S = 1


• The next 8-bits are the exponent: 10000010 = 130decimal
• The next 23-bits are the fraction: 11010000000000000000000

• Now we plug it into the formula for the standard:


N = (-1)S × 1.fraction × 2exponent-127
N = (-1)1 × 1.11010000000000000000000 × 2130-127
= -1 × 1.11010000000000000000000 × 23
= -1 × 1110.1
This number is always treated
14 1 x 2-1 = 0.5 as “unsigned”, as “S” contains the sign
N = -14.5
Property of Penn Engineering 69
Introduction to Computer Systems

Floating-Point Standard
Information & Approximation
• The IEEE 754 Floating Point Standard:
• Single precision: 32-bits – variable type: float
• Double precision: 64-bits – variable type: double

Number line
IEEE 754 Floating point numbers

 Floating point numbers can only represent a small handful of the infinitely infinite set
of real numbers
 Almost all operations with floating point numbers are approximate because of
roundoff errors
 Because of the fact that numbers are stored with a finite number of bits on the
computer floating point operations are inherently approximate
 As a programmer you must be aware of these limitations!

Property of Penn Engineering 70

You might also like