remove "melody" to transcriptions section

master
Tait Hoyem 2 years ago
parent c0fe503a87
commit fa1b27113f

@ -1,88 +0,0 @@
---
layout: simple
---
# CMPT 225 - Introduction
## CMPT: 225, D100 --- Data Structures & Programming
## Graded Work
* 4 Assignments, 32%
* Programming & Written Components
* Programs:
* In C++
* running on ESIL Linux machines
* using command line tools
* 3 Tests, 33%
* In class
* 1 Final Exam, 35%
## Lectures
* Slides + audio recording will be posted <em>after</em> lectures.
## Labs
* There are <em>help times</em> (first labs are <em>next week</em>)
## Instructor Office Hours
* Will be primarily online --- times TBA.
## Instructor Contact
* Via Canvas or email (for email: write from your SFU account, use subject "225: ...")
## Some Basic Rules
* In class, do not distract others.
* In class, no taking photos/videos/recording
* Help each other, but submit your own work.
* Masks are mandatory in class:
* may be removed while asking a question
* if you cannot wear a mask, please email me
* Please stay on "your" side of the table
* and stay 6' away if you don't have a mask on
## Course Content
* Algorithms: processes that operate on data
* Programs: implementations of algorithms
* Data in programs: stored in data structures
* simple D.S.: variables
* most algorithms required compound D.S.s: e.g. arrays, lists, ...
* most non-trivial applications require non-trivial D.S.s
* Data Type: collection of values + operations on these values
* Multiple data types:
* Cleaner/simpler slg. design
* better code
* Abstract Data Type: defined by values + operations without reference to how things are implemented
* ADTs provide an abstract view of data:
* let us reason about algorithms at a high level, ignoring implementation details
* Good ADT choices reduce program complexity & increase likelihood of success ( which is good).
* a[i] vs. a(i)
## We will look at:
* fundamental ADTs
* fundamental data structures (to implement them)
* sorting algorithms
* with attention to:
* correctness
* efficiency/speed
* how to implement
* how to choose (for an application)
## Pre-regs:
* Basic Programming (e.g. CMPT 125)
* Discrete Math (e.g. MACM 101)
(students get these in many different ways)
## To Do
* Read chapters 1 + 3 of text
* Lear about CSIL
## End

@ -1,357 +0,0 @@
---
layout: simple
---
# Stacks & Queues - 1
CMPT 225, Fall 2021, Lecture 2
## Stack
* ADT that stores a collection of objects/values
* Insertions and removals follow last-in-first-out pattern
* Fundamental Operations:
* push: inserts an element on the "top"
* pop: removes + returns the top element
e.g.
Stack index|Value
push a
Stack index|Value
0|a
push b
Stack index|Value
0|b
1|a
push c
Stack index|Value
0|c
1|b
2|a
pop
Stack index|Value
0|b
1|a
* conventient optional operations
* size: return # of elements on stack // encapsulation
* empty: check for emptiness // better than "size=0?"
* top: return top element, but don't remove it // better than x = pop(); push(x) then use x
## Algorithm Applications
* parasing/evaluation/transformation of expressions
* speech recognition coding/decoding
* shared network access control
* programming languages & execution
* Postscript, Forth, the call stack
* things we do daily with computers
## Parenthasies Checking
Parenthasies: `(()(((()())()()(()))`
A diagram showing:
For every left parenthasies, add one to a counter.
For every right parthenthasies, remove one from the counter.
If the counter is not zero by the end of the sequence, there are too many on one side of the parenthasies.
Can be done by counting unmached left parenthasies:
Successful example:
Character|Counter
|0
(|1
(|2
)|1
(|2
(|3
)|2
)|1
)|0
Yay!|0
Failed example:
Character|Country
|0
(|1
(|2
)|1
(|2
)|1
|Oh no it's not zero!
Another fail:
Character|Counter
|0
(|1
)|0
(|1
)|0
)|-1
|Negative 1 this time!
Do this one yourself:
Character|Counter
(|
)|
)|
(|
But consider multiple grouping symbols: `(\{\{)`, `(\{)\{`, `(\{[( ... ]`.
Counting is not enough.
## Parenthasies Checking
Diagram showing failure on the last character of this sequence: `(()((()()))()(())))`
Can be done by counting unmatched left parenthasies:
Character|Counter
(|1
(|2
)|1
(|2
(|3
)|2
)|1
)|0
Example 2:
Character|Counter
(|1
(|2
)|1
(|2
)|1
Example 3:
Character|Counter
(|1
)|0
(|1
)|0
)|-1
Example 4:
Character|Counter
(|1
)|0
)|-1
(|0
But consider multiple grouping symbols:
* sequence `(\{\})` will and should work
* sequence `(\{)\}` will work and should not
* sequence `(\{[( ...` ???
Counting is not enough.
## Stack-based Algorithm for Checking Grouping Symbols
Pseudo-code:
```
I is an input stream of symbols.
S is a new empty stack.
while there are signals to read from I {
c is next symbol from I
if c is a left grouping signal {
push c on S
} else if c is a right grouping signal{
if s in empty { report error and stop }
d is pop S
if c and d do not match { report error and stop }
endif // if c is not a grouping symbol, ignore it
end while
if S is not empty { report error and exit }
report ok
```
Diagram showing how each set of parenthasies are matched, innermost first torwards the outermost.
## Stack: Partially-Filled Array Implementation
Variables:
* A - an array of stack elements
* capacity - size of array
* top - index in array of top stack element
* -1 if stack is empty
A starts out as:
Index|0|1|2|...|capacity-1
Value|a|b|c|...|
* Capacity = &lt;size of A&gt;
* top = 2
push e
A now equals
Index|0|1|2|3|4|...|capacity-1
Value|a|b|c|d|e|...|
* capacity = &lt;size of A&gt;
* top = 3
## The Queue ADT
A container that stores a collection of items with insertion and removal in "first-in-first-out" order.
Stack: a digram showing circled being placed on top of eachother, pushing circles into the tube, then popping them out from the same size (the top). Like a pringles can of elements.
Queue: a diagram showing circles being unqueued in on the left and dequeued on the right. Like waiting in line for food.
Basic operations:
* enqueue: add an item to the back of a list
* dequeue: remove & return the item at the front of the list
## Example: Palindrome Checking
Sequence `aba` should succeed.
Sequence `aab` should not.
In an array: a diagram showing the following pattern with the variable `i` set to the leftmost element and `j` set to the rightmost element.
Values|a|b|c|...|s|b|a
With a stack and queue:
1. read symbols, starting each in a stack and a queue
2. while the stack, queue are not empty:
* pop and element from the stack + delete an element from the queue
* if different -> not a palindrome
3. palindrome
## Queue: Array Implementations
Enqueue: a,b,c,d.
Front is now a, last is now d.
Values|a|b|c|d|...
Dequeue returns and deletes first element.
Front is now b, last is now d.
Values|?|b|c|d|...
Many enquques/dequeues later:
Values|s|...|m|n|o
Front element is now `m`, last element is now s.
Values|?|t|u|v|w|...
A similar slide is here, but I can't see the differnece.
Variables:
* array A is array of size capacity
* front is 0
* back is 0
* size is 0
* capacity = &lt;size of array&gt;
Pseudo-code:
```
array A = array of size capacity // holds queue contents
front = 0 // index in A of front element, if queue not empty
back = 0 // index where next unqueued element will go
size = 0 // number of elements in queue
capacity = <size of array> // ???(can't read) queue size supported
enqueue(x) { // adds x to the back of the queue
// requires size<capacity>
A[back] = x
size = size + 1
back = (back + 1) % capacity
}
dequeue() { // removes front element of queue and returns it
// required size > 0
temp = A[front]
front = (front + 1) % capacity
size = size - 1
return temp
}
```
Is `size == front - back`?
Diagram showing only true if front and back haven't been switched around due to wrapping around back to the beginning o the array.
## "Circular Array" Queue Example
* size = 0
* front = 0
* back = 0
Values|empty|empty|empty|empty|empty|empty
enqueue: a,b,c,d
* front = 0
* back = 4
Values|a|b|c|d|empty|empty
dequque 3 times
* front = 3
* back = 4
Values|empty|empty|empty|d|empty|empty
enqueue e,f,g,h
* front = 3
* back = 2
Values|g|h|empty|d|e|f
unqueue j
* size = 6
* font = 3
* back = 3
Values|g|h|j|d|e|f
dequeue 3 times
* back = 3
* front = 0
Values|g|h|j|empty|empty|empty
dequeue 3 times
* back = 3
* front = 3
Values|empty|empty|empty|empty|empty|empty
## End

@ -1,149 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-225---introduction">CMPT 225 - Introduction</h1>
<h2 id="cmpt-225-d100--data-structures--programming">CMPT: 225, D100 — Data Structures &amp; Programming</h2>
<h2 id="graded-work">Graded Work</h2>
<ul>
<li>4 Assignments, 32%
<ul>
<li>Programming &amp; Written Components</li>
<li>Programs:
<ul>
<li>In C++</li>
<li>running on ESIL Linux machines</li>
<li>using command line tools</li>
</ul>
</li>
</ul>
</li>
<li>3 Tests, 33%
<ul>
<li>In class</li>
</ul>
</li>
<li>1 Final Exam, 35%</li>
</ul>
<h2 id="lectures">Lectures</h2>
<ul>
<li>Slides + audio recording will be posted <em>after</em> lectures.</li>
</ul>
<h2 id="labs">Labs</h2>
<ul>
<li>There are <em>help times</em> (first labs are <em>next week</em>)</li>
</ul>
<h2 id="instructor-office-hours">Instructor Office Hours</h2>
<ul>
<li>Will be primarily online — times TBA.</li>
</ul>
<h2 id="instructor-contact">Instructor Contact</h2>
<ul>
<li>Via Canvas or email (for email: write from your SFU account, use subject “225: …”)</li>
</ul>
<h2 id="some-basic-rules">Some Basic Rules</h2>
<ul>
<li>In class, do not distract others.</li>
<li>In class, no taking photos/videos/recording</li>
<li>Help each other, but submit your own work.</li>
<li>Masks are mandatory in class:
<ul>
<li>may be removed while asking a question</li>
<li>if you cannot wear a mask, please email me</li>
</ul>
</li>
<li>Please stay on “your” side of the table
<ul>
<li>and stay 6 away if you dont have a mask on</li>
</ul>
</li>
</ul>
<h2 id="course-content">Course Content</h2>
<ul>
<li>Algorithms: processes that operate on data</li>
<li>Programs: implementations of algorithms</li>
<li>Data in programs: stored in data structures
<ul>
<li>simple D.S.: variables</li>
<li>most algorithms required compound D.S.s: e.g. arrays, lists, …</li>
<li>most non-trivial applications require non-trivial D.S.s</li>
</ul>
</li>
<li>Data Type: collection of values + operations on these values</li>
<li>Multiple data types:
<ul>
<li>Cleaner/simpler slg. design</li>
<li>better code</li>
</ul>
</li>
<li>Abstract Data Type: defined by values + operations without reference to how things are implemented</li>
<li>ADTs provide an abstract view of data:
<ul>
<li>let us reason about algorithms at a high level, ignoring implementation details</li>
</ul>
</li>
<li>Good ADT choices reduce program complexity &amp; increase likelihood of success ( which is good).</li>
<li>a[i] vs. a(i)</li>
</ul>
<h2 id="we-will-look-at">We will look at:</h2>
<ul>
<li>fundamental ADTs</li>
<li>fundamental data structures (to implement them)</li>
<li>sorting algorithms</li>
<li>with attention to:
<ul>
<li>correctness</li>
<li>efficiency/speed</li>
<li>how to implement</li>
<li>how to choose (for an application)</li>
</ul>
</li>
</ul>
<h2 id="pre-regs">Pre-regs:</h2>
<ul>
<li>Basic Programming (e.g. CMPT 125)</li>
<li>Discrete Math (e.g. MACM 101)</li>
</ul>
<p>(students get these in many different ways)</p>
<h2 id="to-do">To Do</h2>
<ul>
<li>Read chapters 1 + 3 of text</li>
<li>Lear about CSIL</li>
</ul>
<h2 id="end">End</h2>
<footer>
</footer>
</div>
</main>
</body>
</html>

@ -1,88 +0,0 @@
---
layout: simple
---
# CMPT 225 - Introduction
## CMPT: 225, D100 --- Data Structures & Programming
## Graded Work
* 4 Assignments, 32%
* Programming & Written Components
* Programs:
* In C++
* running on ESIL Linux machines
* using command line tools
* 3 Tests, 33%
* In class
* 1 Final Exam, 35%
## Lectures
* Slides + audio recording will be posted <em>after</em> lectures.
## Labs
* There are <em>help times</em> (first labs are <em>next week</em>)
## Instructor Office Hours
* Will be primarily online --- times TBA.
## Instructor Contact
* Via Canvas or email (for email: write from your SFU account, use subject "225: ...")
## Some Basic Rules
* In class, do not distract others.
* In class, no taking photos/videos/recording
* Help each other, but submit your own work.
* Masks are mandatory in class:
* may be removed while asking a question
* if you cannot wear a mask, please email me
* Please stay on "your" side of the table
* and stay 6' away if you don't have a mask on
## Course Content
* Algorithms: processes that operate on data
* Programs: implementations of algorithms
* Data in programs: stored in data structures
* simple D.S.: variables
* most algorithms required compound D.S.s: e.g. arrays, lists, ...
* most non-trivial applications require non-trivial D.S.s
* Data Type: collection of values + operations on these values
* Multiple data types:
* Cleaner/simpler slg. design
* better code
* Abstract Data Type: defined by values + operations without reference to how things are implemented
* ADTs provide an abstract view of data:
* let us reason about algorithms at a high level, ignoring implementation details
* Good ADT choices reduce program complexity & increase likelihood of success ( which is good).
* a[i] vs. a(i)
## We will look at:
* fundamental ADTs
* fundamental data structures (to implement them)
* sorting algorithms
* with attention to:
* correctness
* efficiency/speed
* how to implement
* how to choose (for an application)
## Pre-regs:
* Basic Programming (e.g. CMPT 125)
* Discrete Math (e.g. MACM 101)
(students get these in many different ways)
## To Do
* Read chapters 1 + 3 of text
* Lear about CSIL
## End

@ -1,864 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="stacks--queues---1">Stacks &amp; Queues - 1</h1>
<p>CMPT 225, Fall 2021, Lecture 2</p>
<h2 id="stack">Stack</h2>
<ul>
<li>ADT that stores a collection of objects/values</li>
<li>Insertions and removals follow last-in-first-out pattern</li>
<li>Fundamental Operations:
<ul>
<li>push: inserts an element on the “top”</li>
<li>pop: removes + returns the top element</li>
</ul>
</li>
</ul>
<p>e.g.</p>
<table>
<tbody>
<tr>
<td>Stack index</td>
<td>Value</td>
</tr>
</tbody>
</table>
<p>push a</p>
<table>
<tbody>
<tr>
<td>Stack index</td>
<td>Value</td>
</tr>
<tr>
<td>0</td>
<td>a</td>
</tr>
</tbody>
</table>
<p>push b</p>
<table>
<tbody>
<tr>
<td>Stack index</td>
<td>Value</td>
</tr>
<tr>
<td>0</td>
<td>b</td>
</tr>
<tr>
<td>1</td>
<td>a</td>
</tr>
</tbody>
</table>
<p>push c</p>
<table>
<tbody>
<tr>
<td>Stack index</td>
<td>Value</td>
</tr>
<tr>
<td>0</td>
<td>c</td>
</tr>
<tr>
<td>1</td>
<td>b</td>
</tr>
<tr>
<td>2</td>
<td>a</td>
</tr>
</tbody>
</table>
<p>pop</p>
<table>
<tbody>
<tr>
<td>Stack index</td>
<td>Value</td>
</tr>
<tr>
<td>0</td>
<td>b</td>
</tr>
<tr>
<td>1</td>
<td>a</td>
</tr>
</tbody>
</table>
<ul>
<li>conventient optional operations
<ul>
<li>size: return # of elements on stack // encapsulation</li>
<li>empty: check for emptiness // better than “size=0?”</li>
<li>top: return top element, but dont remove it // better than x = pop(); push(x) then use x</li>
</ul>
</li>
</ul>
<h2 id="algorithm-applications">Algorithm Applications</h2>
<ul>
<li>parasing/evaluation/transformation of expressions</li>
<li>speech recognition coding/decoding</li>
<li>shared network access control</li>
<li>programming languages &amp; execution
<ul>
<li>Postscript, Forth, the call stack</li>
</ul>
</li>
<li>things we do daily with computers</li>
</ul>
<h2 id="parenthasies-checking">Parenthasies Checking</h2>
<p>Parenthasies: <code class="language-plaintext highlighter-rouge">(()(((()())()()(()))</code></p>
<p>A diagram showing:
For every left parenthasies, add one to a counter.
For every right parthenthasies, remove one from the counter.
If the counter is not zero by the end of the sequence, there are too many on one side of the parenthasies.</p>
<p>Can be done by counting unmached left parenthasies:</p>
<p>Successful example:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td> </td>
<td>0</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>(</td>
<td>3</td>
</tr>
<tr>
<td>)</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>Yay!</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>Failed example:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Country</td>
</tr>
<tr>
<td> </td>
<td>0</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td> </td>
<td>Oh no its not zero!</td>
</tr>
</tbody>
</table>
<p>Another fail:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td> </td>
<td>0</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>)</td>
<td>-1</td>
</tr>
<tr>
<td> </td>
<td>Negative 1 this time!</td>
</tr>
</tbody>
</table>
<p>Do this one yourself:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td>(</td>
<td> </td>
</tr>
<tr>
<td>)</td>
<td> </td>
</tr>
<tr>
<td>)</td>
<td> </td>
</tr>
<tr>
<td>(</td>
<td> </td>
</tr>
</tbody>
</table>
<p>But consider multiple grouping symbols: <code class="language-plaintext highlighter-rouge">(\{\{)</code>, <code class="language-plaintext highlighter-rouge">(\{)\{</code>, <code class="language-plaintext highlighter-rouge">(\{[( ... ]</code>.
Counting is not enough.</p>
<h2 id="parenthasies-checking-1">Parenthasies Checking</h2>
<p>Diagram showing failure on the last character of this sequence: <code class="language-plaintext highlighter-rouge">(()((()()))()(())))</code></p>
<p>Can be done by counting unmatched left parenthasies:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>(</td>
<td>3</td>
</tr>
<tr>
<td>)</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>Example 2:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
</tbody>
</table>
<p>Example 3:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>)</td>
<td>-1</td>
</tr>
</tbody>
</table>
<p>Example 4:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>)</td>
<td>-1</td>
</tr>
<tr>
<td>(</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>But consider multiple grouping symbols:</p>
<ul>
<li>sequence <code class="language-plaintext highlighter-rouge">(\{\})</code> will and should work</li>
<li>sequence <code class="language-plaintext highlighter-rouge">(\{)\}</code> will work and should not</li>
<li>sequence <code class="language-plaintext highlighter-rouge">(\{[( ...</code> ???</li>
</ul>
<p>Counting is not enough.</p>
<h2 id="stack-based-algorithm-for-checking-grouping-symbols">Stack-based Algorithm for Checking Grouping Symbols</h2>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>I is an input stream of symbols.
S is a new empty stack.
while there are signals to read from I {
c is next symbol from I
if c is a left grouping signal {
push c on S
} else if c is a right grouping signal{
if s in empty { report error and stop }
d is pop S
if c and d do not match { report error and stop }
endif // if c is not a grouping symbol, ignore it
end while
if S is not empty { report error and exit }
report ok
</code></pre></div></div>
<p>Diagram showing how each set of parenthasies are matched, innermost first torwards the outermost.</p>
<h2 id="stack-partially-filled-array-implementation">Stack: Partially-Filled Array Implementation</h2>
<p>Variables:</p>
<ul>
<li>A - an array of stack elements</li>
<li>capacity - size of array</li>
<li>top - index in array of top stack element
<ul>
<li>-1 if stack is empty</li>
</ul>
</li>
</ul>
<p>A starts out as:</p>
<table>
<tbody>
<tr>
<td>Index</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td></td>
<td>capacity-1</td>
</tr>
<tr>
<td>Value</td>
<td>a</td>
<td>b</td>
<td>c</td>
<td></td>
<td> </td>
</tr>
</tbody>
</table>
<ul>
<li>Capacity = &lt;size of A&gt;</li>
<li>top = 2</li>
</ul>
<p>push e</p>
<p>A now equals</p>
<table>
<tbody>
<tr>
<td>Index</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td></td>
<td>capacity-1</td>
</tr>
<tr>
<td>Value</td>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
<td></td>
<td> </td>
</tr>
</tbody>
</table>
<ul>
<li>capacity = &lt;size of A&gt;</li>
<li>top = 3</li>
</ul>
<h2 id="the-queue-adt">The Queue ADT</h2>
<p>A container that stores a collection of items with insertion and removal in “first-in-first-out” order.</p>
<p>Stack: a digram showing circled being placed on top of eachother, pushing circles into the tube, then popping them out from the same size (the top). Like a pringles can of elements.</p>
<p>Queue: a diagram showing circles being unqueued in on the left and dequeued on the right. Like waiting in line for food.</p>
<p>Basic operations:</p>
<ul>
<li>enqueue: add an item to the back of a list</li>
<li>dequeue: remove &amp; return the item at the front of the list</li>
</ul>
<h2 id="example-palindrome-checking">Example: Palindrome Checking</h2>
<p>Sequence <code class="language-plaintext highlighter-rouge">aba</code> should succeed.
Sequence <code class="language-plaintext highlighter-rouge">aab</code> should not.</p>
<p>In an array: a diagram showing the following pattern with the variable <code class="language-plaintext highlighter-rouge">i</code> set to the leftmost element and <code class="language-plaintext highlighter-rouge">j</code> set to the rightmost element.</p>
<table>
<tbody>
<tr>
<td>Values</td>
<td>a</td>
<td>b</td>
<td>c</td>
<td></td>
<td>s</td>
<td>b</td>
<td>a</td>
</tr>
</tbody>
</table>
<p>With a stack and queue:</p>
<ol>
<li>read symbols, starting each in a stack and a queue</li>
<li>while the stack, queue are not empty:
<ul>
<li>pop and element from the stack + delete an element from the queue</li>
<li>if different -&gt; not a palindrome</li>
</ul>
</li>
<li>palindrome</li>
</ol>
<h2 id="queue-array-implementations">Queue: Array Implementations</h2>
<p>Enqueue: a,b,c,d.
Front is now a, last is now d.</p>
<table>
<tbody>
<tr>
<td>Values</td>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td></td>
</tr>
</tbody>
</table>
<p>Dequeue returns and deletes first element.
Front is now b, last is now d.</p>
<table>
<tbody>
<tr>
<td>Values</td>
<td>?</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td></td>
</tr>
</tbody>
</table>
<p>Many enquques/dequeues later:</p>
<table>
<tbody>
<tr>
<td>Values</td>
<td>s</td>
<td></td>
<td>m</td>
<td>n</td>
<td>o</td>
</tr>
</tbody>
</table>
<p>Front element is now <code class="language-plaintext highlighter-rouge">m</code>, last element is now s.</p>
<table>
<tbody>
<tr>
<td>Values</td>
<td>?</td>
<td>t</td>
<td>u</td>
<td>v</td>
<td>w</td>
<td></td>
</tr>
</tbody>
</table>
<p>A similar slide is here, but I cant see the differnece.</p>
<p>Variables:</p>
<ul>
<li>array A is array of size capacity</li>
<li>front is 0</li>
<li>back is 0</li>
<li>size is 0</li>
<li>capacity = &lt;size of array&gt;</li>
</ul>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>array A = array of size capacity // holds queue contents
front = 0 // index in A of front element, if queue not empty
back = 0 // index where next unqueued element will go
size = 0 // number of elements in queue
capacity = &lt;size of array&gt; // ???(can't read) queue size supported
enqueue(x) { // adds x to the back of the queue
// requires size&lt;capacity&gt;
A[back] = x
size = size + 1
back = (back + 1) % capacity
}
dequeue() { // removes front element of queue and returns it
// required size &gt; 0
temp = A[front]
front = (front + 1) % capacity
size = size - 1
return temp
}
</code></pre></div></div>
<p>Is <code class="language-plaintext highlighter-rouge">size == front - back</code>?</p>
<p>Diagram showing only true if front and back havent been switched around due to wrapping around back to the beginning o the array.</p>
<h2 id="circular-array-queue-example">“Circular Array” Queue Example</h2>
<ul>
<li>size = 0</li>
<li>front = 0</li>
<li>back = 0</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
</tr>
</tbody>
</table>
<p>enqueue: a,b,c,d</p>
<ul>
<li>front = 0</li>
<li>back = 4</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>empty</td>
<td>empty</td>
</tr>
</tbody>
</table>
<p>dequque 3 times</p>
<ul>
<li>front = 3</li>
<li>back = 4</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>d</td>
<td>empty</td>
<td>empty</td>
</tr>
</tbody>
</table>
<p>enqueue e,f,g,h</p>
<ul>
<li>front = 3</li>
<li>back = 2</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>g</td>
<td>h</td>
<td>empty</td>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
</tbody>
</table>
<p>unqueue j</p>
<ul>
<li>size = 6</li>
<li>font = 3</li>
<li>back = 3</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>g</td>
<td>h</td>
<td>j</td>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
</tbody>
</table>
<p>dequeue 3 times</p>
<ul>
<li>back = 3</li>
<li>front = 0</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>g</td>
<td>h</td>
<td>j</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
</tr>
</tbody>
</table>
<p>dequeue 3 times</p>
<ul>
<li>back = 3</li>
<li>front = 3</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
</tr>
</tbody>
</table>
<h2 id="end">End</h2>
<footer>
</footer>
</div>
</main>
</body>
</html>

@ -1,357 +0,0 @@
---
layout: simple
---
# Stacks & Queues - 1
CMPT 225, Fall 2021, Lecture 2
## Stack
* ADT that stores a collection of objects/values
* Insertions and removals follow last-in-first-out pattern
* Fundamental Operations:
* push: inserts an element on the "top"
* pop: removes + returns the top element
e.g.
Stack index|Value
push a
Stack index|Value
0|a
push b
Stack index|Value
0|b
1|a
push c
Stack index|Value
0|c
1|b
2|a
pop
Stack index|Value
0|b
1|a
* conventient optional operations
* size: return # of elements on stack // encapsulation
* empty: check for emptiness // better than "size=0?"
* top: return top element, but don't remove it // better than x = pop(); push(x) then use x
## Algorithm Applications
* parasing/evaluation/transformation of expressions
* speech recognition coding/decoding
* shared network access control
* programming languages & execution
* Postscript, Forth, the call stack
* things we do daily with computers
## Parenthasies Checking
Parenthasies: `(()(((()())()()(()))`
A diagram showing:
For every left parenthasies, add one to a counter.
For every right parthenthasies, remove one from the counter.
If the counter is not zero by the end of the sequence, there are too many on one side of the parenthasies.
Can be done by counting unmached left parenthasies:
Successful example:
Character|Counter
|0
(|1
(|2
)|1
(|2
(|3
)|2
)|1
)|0
Yay!|0
Failed example:
Character|Country
|0
(|1
(|2
)|1
(|2
)|1
|Oh no it's not zero!
Another fail:
Character|Counter
|0
(|1
)|0
(|1
)|0
)|-1
|Negative 1 this time!
Do this one yourself:
Character|Counter
(|
)|
)|
(|
But consider multiple grouping symbols: `(\{\{)`, `(\{)\{`, `(\{[( ... ]`.
Counting is not enough.
## Parenthasies Checking
Diagram showing failure on the last character of this sequence: `(()((()()))()(())))`
Can be done by counting unmatched left parenthasies:
Character|Counter
(|1
(|2
)|1
(|2
(|3
)|2
)|1
)|0
Example 2:
Character|Counter
(|1
(|2
)|1
(|2
)|1
Example 3:
Character|Counter
(|1
)|0
(|1
)|0
)|-1
Example 4:
Character|Counter
(|1
)|0
)|-1
(|0
But consider multiple grouping symbols:
* sequence `(\{\})` will and should work
* sequence `(\{)\}` will work and should not
* sequence `(\{[( ...` ???
Counting is not enough.
## Stack-based Algorithm for Checking Grouping Symbols
Pseudo-code:
```
I is an input stream of symbols.
S is a new empty stack.
while there are signals to read from I {
c is next symbol from I
if c is a left grouping signal {
push c on S
} else if c is a right grouping signal{
if s in empty { report error and stop }
d is pop S
if c and d do not match { report error and stop }
endif // if c is not a grouping symbol, ignore it
end while
if S is not empty { report error and exit }
report ok
```
Diagram showing how each set of parenthasies are matched, innermost first torwards the outermost.
## Stack: Partially-Filled Array Implementation
Variables:
* A - an array of stack elements
* capacity - size of array
* top - index in array of top stack element
* -1 if stack is empty
A starts out as:
Index|0|1|2|...|capacity-1
Value|a|b|c|...|
* Capacity = &lt;size of A&gt;
* top = 2
push e
A now equals
Index|0|1|2|3|4|...|capacity-1
Value|a|b|c|d|e|...|
* capacity = &lt;size of A&gt;
* top = 3
## The Queue ADT
A container that stores a collection of items with insertion and removal in "first-in-first-out" order.
Stack: a digram showing circled being placed on top of eachother, pushing circles into the tube, then popping them out from the same size (the top). Like a pringles can of elements.
Queue: a diagram showing circles being unqueued in on the left and dequeued on the right. Like waiting in line for food.
Basic operations:
* enqueue: add an item to the back of a list
* dequeue: remove & return the item at the front of the list
## Example: Palindrome Checking
Sequence `aba` should succeed.
Sequence `aab` should not.
In an array: a diagram showing the following pattern with the variable `i` set to the leftmost element and `j` set to the rightmost element.
Values|a|b|c|...|s|b|a
With a stack and queue:
1. read symbols, starting each in a stack and a queue
2. while the stack, queue are not empty:
* pop and element from the stack + delete an element from the queue
* if different -> not a palindrome
3. palindrome
## Queue: Array Implementations
Enqueue: a,b,c,d.
Front is now a, last is now d.
Values|a|b|c|d|...
Dequeue returns and deletes first element.
Front is now b, last is now d.
Values|?|b|c|d|...
Many enquques/dequeues later:
Values|s|...|m|n|o
Front element is now `m`, last element is now s.
Values|?|t|u|v|w|...
A similar slide is here, but I can't see the differnece.
Variables:
* array A is array of size capacity
* front is 0
* back is 0
* size is 0
* capacity = &lt;size of array&gt;
Pseudo-code:
```
array A = array of size capacity // holds queue contents
front = 0 // index in A of front element, if queue not empty
back = 0 // index where next unqueued element will go
size = 0 // number of elements in queue
capacity = <size of array> // ???(can't read) queue size supported
enqueue(x) { // adds x to the back of the queue
// requires size<capacity>
A[back] = x
size = size + 1
back = (back + 1) % capacity
}
dequeue() { // removes front element of queue and returns it
// required size > 0
temp = A[front]
front = (front + 1) % capacity
size = size - 1
return temp
}
```
Is `size == front - back`?
Diagram showing only true if front and back haven't been switched around due to wrapping around back to the beginning o the array.
## "Circular Array" Queue Example
* size = 0
* front = 0
* back = 0
Values|empty|empty|empty|empty|empty|empty
enqueue: a,b,c,d
* front = 0
* back = 4
Values|a|b|c|d|empty|empty
dequque 3 times
* front = 3
* back = 4
Values|empty|empty|empty|d|empty|empty
enqueue e,f,g,h
* front = 3
* back = 2
Values|g|h|empty|d|e|f
unqueue j
* size = 6
* font = 3
* back = 3
Values|g|h|j|d|e|f
dequeue 3 times
* back = 3
* front = 0
Values|g|h|j|empty|empty|empty
dequeue 3 times
* back = 3
* front = 3
Values|empty|empty|empty|empty|empty|empty
## End

@ -1,168 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="memory--pointers-1">Memory &amp; Pointers 1</h1>
<p>CMPT-225, Fall 2021</p>
<h2 id="computer-memory">Computer Memory</h2>
<ul>
<li>A sequence of locations</li>
<li>Indexed by address: 0,1,2,…</li>
<li>Each location stores a data <em>byte</em></li>
<li>Processor can <em>read</em> or <em>write</em> the byte at each address.</li>
<li>Regions of memory are allocated to processes <em>as needed</em>, according to some scheme.</li>
</ul>
<p>Diagram displaying “Code + Data for running part of OS” at the start of memory, “Free memory” in the middle, and “Code + Data for running user processes” and the end.</p>
<h2 id="variables--memory">Variables &amp; Memory</h2>
<p>A variable is (roughly) and names &amp; tagged collection of bytes:</p>
<p>Diagram showing:</p>
<ul>
<li>“int x; x = 6” taking 4 bytes of memory,</li>
<li>“char c; c = x;” taking 1 byte of memory</li>
<li>
<p>“bool b; b = true;” taking 1 byte of memory</p>
</li>
<li>So, <em>at run time</em>, each variable has an <em>address</em> in memory.</li>
<li>In C, C++ we can:
<ul>
<li>access the address of a variable</li>
<li>access a variable or memory location by its address</li>
<li>declare variables by storing addresses (pointers).</li>
</ul>
</li>
</ul>
<h2 id="addresses--pointers---by-example">Addresses &amp; Pointers - By Example</h2>
<ul>
<li>“int i = 5;”
<ul>
<li>allocate space for an <em>int</em>,</li>
<li>name the space “i”,</li>
<li>store 5 there</li>
</ul>
</li>
<li>“int *p;”
<ul>
<li>allocate space for an address,</li>
<li>name it p,</li>
<li>record its type as “pointer to int”</li>
</ul>
</li>
<li>“p = ∧i;”
<ul>
<li>&amp;i” is the address of “i”</li>
<li>store ∧i in p</li>
<li>so, p becomes a <em>pointer to i</em></li>
</ul>
</li>
<li>“cout « i;”
<ul>
<li>outputs the value stored in i, <em>5</em></li>
</ul>
</li>
<li>“cout « p;”
<ul>
<li>outputs the address of i: 0xbffffbbc</li>
</ul>
</li>
<li>“cout « *p;”
<ul>
<li>”*” <em>dereferences</em> p. That is, *p is the value <em>pointed to by p</em>. In this case <em>5</em></li>
</ul>
</li>
</ul>
<p>Diagram of memory:</p>
<table>
<tbody>
<tr>
<td>Address</td>
<td>Name</td>
<td>Value</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><span id="point">254</span></td>
<td>i</td>
<td>5</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>923</td>
<td>p</td>
<td><a href="#point">254</a></td>
</tr>
</tbody>
</table>
<p>Second diagram of memory:</p>
<table>
<tbody>
<tr>
<td>Address</td>
<td>Name</td>
<td>Value</td>
<td>Size</td>
</tr>
<tr>
<td>0x6fffbe</td>
<td>i</td>
<td>5</td>
<td>4 bytes</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>???</td>
<td>p</td>
<td>0x6fffbe</td>
<td>4 bytesa</td>
</tr>
</tbody>
</table>
<p>Slide showing program code: see pointers.c</p>
<p>Slide showing output: see output.txt</p>
<p>Slide showing same program with different highlights.</p>
<p>Slide showing same output with different highlights.</p>
<h2 id="end">End</h2>
<footer>
</footer>
</div>
</main>
</body>
</html>

@ -1,78 +0,0 @@
---
layout: simple
---
# Memory & Pointers 1
CMPT-225, Fall 2021
## Computer Memory
* A sequence of locations
* Indexed by address: 0,1,2,...
* Each location stores a data *byte*
* Processor can *read* or *write* the byte at each address.
* Regions of memory are allocated to processes *as needed*, according to some scheme.
Diagram displaying "Code + Data for running part of OS" at the start of memory, "Free memory" in the middle, and "Code + Data for running user processes" and the end.
## Variables & Memory
A variable is (roughly) and names & tagged collection of bytes:
Diagram showing:
* "int x; x = 6" taking 4 bytes of memory,
* "char c; c = 'x';" taking 1 byte of memory
* "bool b; b = 'true';" taking 1 byte of memory
* So, *at run time*, each variable has an *address* in memory.
* In C, C++ we can:
* access the address of a variable
* access a variable or memory location by its address
* declare variables by storing addresses (pointers).
## Addresses & Pointers - By Example
* "int i = 5;"
* allocate space for an *int*,
* name the space "i",
* store 5 there
* "int \*p;"
* allocate space for an address,
* name it p,
* record its type as "pointer to int"
* "p = &and;i;"
* "&i" is the address of "i"
* store &and;i in p
* so, p becomes a *pointer to i*
* "cout << i;"
* outputs the value stored in i, *5*
* "cout << p;"
* outputs the address of i: 0xbffffbbc
* "cout << \*p;"
* "\*" *dereferences* p. That is, \*p is the value *pointed to by p*. In this case *5*
Diagram of memory:
Address|Name|Value
...|...|...
<span id="point">254</span>|i|5
...|...|...
923|p|<a href="#point">254</a>
Second diagram of memory:
Address|Name|Value|Size
0x6fffbe|i|5|4 bytes
...|...|...|...
???|p|0x6fffbe|4 bytesa
Slide showing program code: see pointers.c
Slide showing output: see output.txt
Slide showing same program with different highlights.
Slide showing same output with different highlights.
## End

@ -1,38 +0,0 @@
main() begin
XXXX = 111111111
XXXX is stored at &XXXX = 0xfffff40750e0
YYYY is a pointer to XXXX: YYYY = 0xfffff40750e0
* dereferences the pointer: *YYYY = 111111111
Array AAAA can be accessed with array notaions:
AAAA[0] = 222222222
AAAA[1] = 333333333
AAAA[2] = 444444444
Array variable AAAA is a pointer to A[0]: AAAA = 0xfffff40750f8
So, dereferencing AAAA should give us A[0]: *AAAA = 222222222
Adding 1 to an int pointer makes it point to the next int
AAAA = 0xfffff40750f8
AAAA+1 = 0xfffff40750fc
*(AAAA+1) = 333333333
We can look at contents of a chunk of memory:
Peeking at the memory in the neighbourhood of &XXXX, we see:
Address Contents in Hex Contents in Decimal
0xfffff40750fc: 13de4355 = 333333333
0xfffff40750f8: d3ed78e = 222222222
0xfffff40750f4: ffff = 65535
0xfffff40750f0: f40750e0 = -200847136
0xfffff40750ec: ffff = 65535
0xfffff40750e8: f40750e8 = -200847128
0xfffff40750e4: 6 = 6
0xfffff40750e0: 69f6bc7 = 111111111
0xfffff40750dc: ffff = 65535
0xfffff40750d8: a478f4e0 = -1535576864
0xfffff40750d4: aaaa = 43690
0xfffff40750d0: c46a13e0 = -999681056
0xfffff40750cc: ffff = 65535
0xfffff40750c8: a478f538 = -1535576776
0xfffff40750c4: ffff = 65535
main() ends

@ -1,43 +0,0 @@
/* a small program to demonstrate C++ addresses, pointers and arrays */
#include <iostream> /* Tait: missing name for import */
#include <iomanip> /* Tait: missing name from import */
using namespace std;
int main(){
cout << "main() begin\n";
int XXXX = 111111111;
cout << "XXXX = " << XXXX << endl;
cout << "XXXX is stored at &XXXX = " << &XXXX << endl;
int * YYYY = &XXXX;
cout << "YYYY is a pointer to XXXX: YYYY = " << YYYY << endl;
cout << "* dereferences the pointer: *YYYY = " << *YYYY << endl;
int AAAA[3] = { 222222222, 333333333, 444444444 };
cout << "Array AAAA can be accessed with array notaions: " << endl;
cout << " AAAA[0] = " << AAAA[0] << endl;
cout << " AAAA[1] = " << AAAA[1] << endl;
cout << " AAAA[2] = " << AAAA[2] << endl << endl;
cout << "Array variable AAAA is a pointer to A[0]: AAAA = " << AAAA << endl;
cout << "So, dereferencing AAAA should give us A[0]: *AAAA = " << *AAAA << endl << endl;
cout << "Adding 1 to an int pointer makes it point to the next int" << endl;
cout << "AAAA = " << AAAA << endl;
cout << "AAAA+1 = " << (AAAA+1) << endl;
cout << "*(AAAA+1) = " << *(AAAA+1) << endl << endl;
cout << "We can look at contents of a chunk of memory:" << endl;
cout << "Peeking at the memory in the neighbourhood of &XXXX, we see: " << endl << endl;
cout << "Address Contents in Hex Contents in Decimal " << endl;
int * p = (&XXXX)+7;
for (int i = 0; i < 15; i++) {
cout << p << ": " << setw(8) << hex << *p << " = " << setw(11) << dec << *p << endl;
p -= 1;
}
cout << "main() ends" << endl;
}

@ -1,97 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="/assets/css/katex.css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="vector-implementation-basics">Vector Implementation Basics</h1>
<ul>
<li>CMPT 225</li>
<li>Fall 2021</li>
<li>Lecture 4</li>
</ul>
<h2 id="example-using-a-simple-vector-class">Example Using a Simple “Vector” Class</h2>
<p>Take a look at the <a href="./simple_vector.cpp">simple_vector.cpp</a> file.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="y1">y</span></td>
<td>float</td>
<td>(empty)</td>
</tr>
<tr>
<td><span id="x1">x</span></td>
<td>int</td>
<td>5</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>p</td>
<td>int *</td>
<td><a href="#x1">address</a></td>
</tr>
<tr>
<td>g</td>
<td>int *</td>
<td><a href="#y1">p+1</a></td>
</tr>
</tbody>
</table>
<p>What is *g?</p>
<h2 id="implementation-in-ivectorh">Implementation in <em>IVector.h</em></h2>
<p>See <a href="./IVector.h">IVector.h</a></p>
<h2 id="templates">Templates</h2>
<ul>
<li>Often, we have algorithms that will work on many data types, with few or no changes.</li>
<li>In <em>strongly typed</em> languages, we need a way to produce “generic” codecode that can work on different types in different places.</li>
<li>In C++, <em>templates</em> let us write generic code.</li>
<li>A template function or class defineition has a <em>placeholder</em> for one or more data types that is instanciated at compile time.</li>
<li>The instanciation may be different at different places in the same code.</li>
</ul>
<h2 id="test-program">Test Program</h2>
<p>See <a href="./test_program.cpp">test_program.cpp</a></p>
<p>See <a href="./simple_vector2.cpp">simple_vector2.cpp</a></p>
<h2 id="tvector-is-a-templated-version-of-ivector">TVector is a templated version of IVector</h2>
<p>See <a href="./TVector.h">TVector.h</a></p>
<p>See <a href="./Vector.h">Vector.h</a></p>
<p>See <a href="./TestVector.cpp">TestVector.cpp</a></p>
<footer>
</footer>
</div>
</main>
</body>
</html>

@ -1,51 +0,0 @@
---
layout: simple
math: true
---
# Vector Implementation Basics
* CMPT 225
* Fall 2021
* Lecture 4
## Example Using a Simple "Vector" Class
Take a look at the [simple_vector.cpp](./simple_vector.cpp) file.
Name|Type|Value
---|---|---
<span id="y1">y</span>|float|(empty)
<span id="x1">x</span>|int|5
...|...|...
p|int \*|[address](#x1)
g|int \*|[p+1](#y1)
What is \*g?
## Implementation in *IVector.h*
See [IVector.h](./IVector.h)
## Templates
* Often, we have algorithms that will work on many data types, with few or no changes.
* In *strongly typed* languages, we need a way to produce "generic" code--code that can work on different types in different places.
* In C++, *templates* let us write generic code.
* A template function or class defineition has a *placeholder* for one or more data types that is instanciated at compile time.
* The instanciation may be different at different places in the same code.
## Test Program
See [test_program.cpp](./test_program.cpp)
See [simple_vector2.cpp](./simple_vector2.cpp)
## TVector is a templated version of IVector
See [TVector.h](./TVector.h)
See [Vector.h](./Vector.h)
See [TestVector.cpp](./TestVector.cpp)

@ -1,84 +0,0 @@
/*Transcriber's note: these first two lines of ifndef and define are added to make compilation work.*/
#ifndef _H_IVECTOR
#define _H_IVECTOR
#include <iostream>
class IVector
{
public:
// Constructor
IVector(int initSize=0)
: theSize{initSize}, theCapacity{initSize+10}
{
objects = new int(theCapacity);
}
// Destructor
~IVector()
{delete [] objects;}
// Check for emptyness
bool empty() const {return size() == 0;}
// Return size of list
int size() const {return theSize;}
// Access the element at a given index
// This is the non-const version, so you can change the element.
int& operator[](int index)
{
return objects[index];
}
// Access the element at a given index
// This is the const version, for accessing the value only
const int& operator[](int index) const
{
return objects[index];
}
// Increase the capacity (i.e., array size)
void reserve(int newCapacity)
{
if(newCapacity>theSize){
int *newArray = new int[newCapacity];
for (int k=0;k<theSize;++k){
newArray[k] = std::move(objects[k]);
}
theCapacity = newCapacity;
std::swap(objects, newArray);
delete [] newArray;
}
}
// add a new element to end of the list
void push_back(const int& x)
{
if(theSize==theCapacity) reserve(2*theCapacity+1);
objects[theSize++]=x;
}
// remove the last element from the list
void pop_back()
{
--theSize;
}
// Print out the size and contents of the list
void display() const
{
std::cout << "size=" << theSize << std::endl;
for(int i=0;i<theSize;++i){
std::cout<<"["<<i<<"]"<<"="<<objects[i]<<std::endl;
}
}
private:
int theSize;
int theCapacity;
int* objects; // The array is of type int.
// In C, C++ a variable of type int array is just a pointer to an int.
};
#endif

@ -1,87 +0,0 @@
/*
Transcriber's note: these first two lines of ifndef and define are added to make compilation work.
Note 2: all changed lines are labled with "//CHANGE" (without the quotes)
*/
#ifndef _H_TVECTOR
#define _H_TVECTOR
#include <iostream>
template <typename Object>//CHANGE
class TVector
{
public:
// Constructor
TVector(int initSize=0)
: theSize{initSize}, theCapacity{initSize+10}
{
objects = new Object(theCapacity);//CHANGE: annotation: in IVector object(...) was int(...)
}
// Destructor
~TVector()
{delete[] objects;}
// Check for emptyness
bool empty() const {return size() == 0;}
// Return size of list
int size() const {return theSize;}
// Access the element at a given index
// This is the non-const version, so you can change the element.
Object& operator[](int index)//CHANGE
{
return objects[index];
}
// Access the element at a given index
// This is the const version, for accessing the value only
const Object& operator[](int index) const //CHANGE
{
return objects[index];
}
// Increase the capacity (i.e., array size)
void reserve(int newCapacity)
{
if(newCapacity>theSize){
Object *newArray = new int[newCapacity]; //CHANGE
for (int k=0;k<theSize;++k){
newArray[k] = std::move(objects[k]);
}
theCapacity = newCapacity;
std::swap(objects, newArray);
delete [] newArray;
}
}
// add a new element to end of the list
void push_back(const Object& x)//CHANGE
{
if(theSize==theCapacity) reserve(2*theCapacity+1);
objects[theSize++]=x;
}
// remove the last element from the list
void pop_back()
{
--theSize;
}
// Print out the size and contents of the list
void display() const
{
std::cout << "size=" << theSize << std::endl;
for(int i=0;i<theSize;++i){
std::cout<<"["<<i<<"]"<<"="<<objects[i]<<std::endl;
}
}
private:
int theSize;
int theCapacity;
Object* objects;//CHANGE
};
#endif

@ -1,48 +0,0 @@
#include "Vector.h"
#include <iostream>
#include <algorithm>
using namespace std;
void print(const Vector<Vector<int>> arr)
{
int N=arr.size();
for(int i=0;i<N;++i)
{
cout<<"arr["<<i<<"]:";
for(int j=0;j<arr[i].size();++j)
cout<<" "<<arr[i][j];
cout<<endl;
}
}
class CompareVector
{
public:
bool operator()(const Vector<int>& lhs, const Vector<int>& rhs)const
{return lhs.size()<rhs.size();}
};
int main()
{
const int N=20;
Vector<Vector<int>> arr(N);
Vector<int> v;
for(int i=N-1;i>0;--i)
{
v.push_back(i);
arr[i]=v;
}
print(arr);
clock_t start=clock();
std::sort(begin(arr),end(arr),CompareVector{});
clock_t end=clock();
cout<<"Sorting time: "<<(end-start)<<endl;
print(arr);
return 0;
}

@ -1,119 +0,0 @@
// transcriber's note: include is added from slide because otherwise it will not compile
#include <utility>
#include <stdexcept>
template <typename Object>
class Vector
{
public:
explicit Vector(int initSize=0)
: theSize{initSize}, theCapacity{initSize + SPARE_CAPACITY}
{objects = new Object[theCapacity];}
Vector(const Vector& rhs)
: theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, objects{nullptr}
{
objects = new Object[theCapacity];
for(int k=0;k<theSize;++k){
objects[k] = rhs.objects[k];
}
}
Vector& operator=(const Vector& rhs)
{
Vector copy = rhs;
std::swap(*this,copy);
return *this;
}
~Vector()
{delete[] objects;}
Vector(Vector&& rhs)
: theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, objects{rhs.objects}
{
rhs.objects=nullptr;
rhs.theSize=0;
rhs.theCapacity=0;
}
Vector& operator=(Vector&& rhs)
{
std::swap(theSize,rhs.theSize);
std::swap(theCapacity,rhs.theCapacity);
std::swap(objects,rhs.objects);
return *this;
}
//stacky stuff
void push_back(Object x)
{
if(theSize==theCapacity)
reserve(2*theCapacity+1);
objects[theSize++]=std::move(x);
}
void pop_back()
{
if(empty())
// transcriber's note: the original line here was the following, however, it was changed to compile
// throw UnderflowException();
throw std::underflow_error::underflow_error;
--theSize;
}
const Object& back() const
{
if(empty())
// See note on line 58
throw std::underflow_error::underflow_error;
return objects[theSize-1];
}
// transcriber's note: these three functions added to make compilation work
const int size() const
{
return theSize;
}
Object& operator[](int index)
{
return objects[index];
}
const Object& operator[](int index) const
{
return objects[index];
}
bool empty() const {return size() == 0;}
void reserve(int newCapacity)
{
if(newCapacity>theSize){
Object *newArray = new int[newCapacity]; //CHANGE
for (int k=0;k<theSize;++k){
newArray[k] = std::move(objects[k]);
}
theCapacity = newCapacity;
std::swap(objects, newArray);
delete [] newArray;
}
}
typedef Object* iterator;
typedef const Object* const_iterator;
iterator begin()
{return &objects[0];}
const_iterator begin() const
{return &objects[0];}
iterator end()
{return &objects[size()];}
const_iterator end() const
{return &objects[size()];}
static const int SPARE_CAPACITY=2;
private:
int theSize;
int theCapacity;
Object* objects;
};

@ -1,22 +0,0 @@
#include "IVector.h"
#include <iostream>
using namespace std;
int main()
{
const int N = 20;
IVector v; // make an int vector
v.display(); // prints its contents
// Stores N ints in the Vector
for (int i=0; i<N; ++i)
{
v.push_back(i);
}
// print the contents
v.display();
return 0;
}

@ -1,22 +0,0 @@
#include "TVector.h"
#include <iostream>
using namespace std;
int main()
{
const int N = 20;
TVector<int> v; // make an int vector
v.display(); // prints its contents
// Stores N ints in the Vector
for (int i=0; i<N; ++i)
{
v.push_back(i);
}
// print the contents
v.display();
return 0;
}

@ -1,46 +0,0 @@
//
// Test Program for Basic Stack Class
//
#include <iostream> // for I/O facilities
using namespace std;
#include "MinimalStack.h" // basic_stackj declaration
int main(int argc, char* const argv[]){
cout << "\n\nMinimalStack Template Class Test Proceedure - START\n\n";
// Make some stacks, and verify that empty() says they are empty.
MinimalStack<int> s1;
MinimalStack<float> s2;
cout<<"s1.isEmpty() = "<<s1.isEmpty()<<"\n";
cout<<"s2.isEmpty() = "<<s2.isEmpty()<<"\n";
// Put some things on them
cout<<"s1.push("<<1<<")\n";
s1.push(1);
cout<<"s1.push("<<2<<")\n";
s1.push(2);
cout<<"s2.push("<<1.5<<")\n";
s2.push(1.5);
cout<<"s2.push("<<2.5<<")\n";
s2.push(2.5);
// Verify that isEmpty() reports that they are empty
// and that the right things are on top
cout<<"s1.isEmpty()="<<s1.isEmpty()<<"\n";
cout<<"s1.top()="<<s1.top()<<"\n";
cout<<"s2.isEmpty()="<<s2.isEmpty()<<"\n";
cout<<"s2.top()="<<s2.top()<<"\n";
// Empty them and verify that isEmpty() again reports that they are empty.
while(!s1.isEmpty()){
cout<<"s1.pop()="<<s1.pop()<<"\n";
}
cout<<"s1.isEmpty()="<<s1.isEmpty()<<"\n";
while(!s2.isEmpty()){
cout<<"s2.pop()="<<s2.pop()<<"\n";
}
// transcriber's note: the following lines are not contained on the slide, but I am assuming they are supposed to be there.
cout<<"s2.isEmpty()="<<s2.isEmpty()<<"\n";
return 0;
}

@ -1,364 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="/assets/css/katex.css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="dynamic-data-structures---linked-lists">Dynamic Data Structures - Linked Lists</h1>
<p>Transcribers note:
Each row in the Value|Pointer/Link tables represents a node.
Despite the fact that the link/pointer takes you to the “value” column, in reality it is taking you to the node “row”, its not just a pointer to the value.</p>
<h2 id="dynamic-data-structures">Dynamic Data Structures</h2>
<ul>
<li>Basic types (e.g. int, char, bool, …), and arrays of these store a <em>fixed</em> amount of data.</li>
<li>We want implementations of ADTs like stacks + queues to <em>grow &amp; shrink</em> (their memory use) <em>as needed</em>.
<ul>
<li>e.g. like Vector, Array-something? (cant read), String classes</li>
</ul>
</li>
</ul>
<p>Basic Idea:</p>
<p>Store data in a collection of (simple) objects.
add/delete these as needed; link them all together to make the main object.</p>
<h2 id="linked-lists">Linked Lists</h2>
<ul>
<li>A sequence of simple objects (nodes): each storing one datum (plus a link…) linked together in a chain</li>
<li>E.g., to store the list &lt;3, 5, 7&gt;:</li>
</ul>
<table>
<thead>
<tr>
<th>Datum</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="z1">3</span></td>
<td><a href="#z2">link</a></td>
</tr>
<tr>
<td><span id="z2">5</span></td>
<td><a href="#z3">link</a></td>
</tr>
<tr>
<td><span id="z3">7</span></td>
<td>link to nowhere</td>
</tr>
</tbody>
</table>
<ul>
<li>These objects have no names, (in contrast to declared values)
<ul>
<li>we access them by following links
<ul>
<li>in Java, references (implemented as pointers)</li>
<li>in C++, pointers</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Need one named place to start like so:</p>
<p>A normal variable of type “pointer to a node”: <a href="#y1">First</a></p>
<table>
<thead>
<tr>
<th>Data</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="y1">3</span></td>
<td><a href="#y2">pointer</a></td>
</tr>
<tr>
<td><span id="y2">5</span></td>
<td><a href="#y3">pointer</a></td>
</tr>
<tr>
<td><span id="y3">7</span></td>
<td>null pointer</td>
</tr>
</tbody>
</table>
<h2 id="implementing-a-stack-with-a-linked-list-by-example">Implementing a Stack with a Linked List (By example)</h2>
<p>Code to run, followed by the current stack.</p>
<h3 id="stack-s"><code>stack s;</code></h3>
<p><label for="tof">top or front</label>
<span id="tof">node</span></p>
<h3 id="spush3"><code>s.push(3)</code></h3>
<p><a href="#x1">front</a></p>
<table>
<thead>
<tr>
<th>Data</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="x1">3</span></td>
<td>null pointer</td>
</tr>
</tbody>
</table>
<h3 id="etc">etc…</h3>
<p></p>
<h3 id="spush5"><code>s.push(5)</code></h3>
<p><a href="#w1">front</a></p>
<table>
<thead>
<tr>
<th>Data</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="w1">5</span></td>
<td><a href="#w2">pointer</a></td>
</tr>
<tr>
<td><span id="w2">8</span></td>
<td><a href="#w3">pointer</a></td>
</tr>
<tr>
<td><span id="w3"></span></td>
<td>… (<a href="#w4">pointer</a>)</td>
</tr>
<tr>
<td><span id="w4">3</span></td>
<td>null pointer</td>
</tr>
</tbody>
</table>
<h3 id="spush7"><code>s.push(7)</code></h3>
<p><a href="#v1">front</a></p>
<table>
<tbody>
<tr>
<td>Value</td>
<td>Pointer</td>
</tr>
<tr>
<td><span id="v1">7</span></td>
<td><a href="#v2">pointer</a></td>
</tr>
<tr>
<td><span id="v2">5</span></td>
<td><a href="#v3">pointer</a></td>
</tr>
<tr>
<td><span id="v3">8</span></td>
<td><a href="#v4">pointer</a></td>
</tr>
<tr>
<td><span id="v4"></span></td>
<td></td>
</tr>
</tbody>
</table>
<p>To perform the push(7):</p>
<ol>
<li>Make a new node to store the 7</li>
<li>modify links to insert it correctly</li>
</ol>
<p>Original:</p>
<p><a href="#u1">front</a></p>
<table>
<thead>
<tr>
<th>Data</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="u1">5</span></td>
<td><a href="#u2">pointer</a></td>
</tr>
<tr>
<td><span id="u2">8</span></td>
<td></td>
</tr>
</tbody>
</table>
<p>After operation:</p>
<p><a href="#t1">front</a> now points at node with value 7; node with value 7s pointer now points to the <a href="#u1">old front</a></p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="t1">7</span></td>
<td><a href="#t2">pointer</a></td>
</tr>
<tr>
<td><span id="t2">5</span></td>
<td><a href="#t3">pointer</a></td>
</tr>
<tr>
<td><span id="t3">8</span></td>
<td></td>
</tr>
</tbody>
</table>
<h3 id="spop"><code>s.pop()</code></h3>
<p><a href="#s1">front</a></p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="s1">5</span></td>
<td><a href="#s2">pointer</a></td>
</tr>
<tr>
<td><span id="s2">8</span></td>
<td></td>
</tr>
</tbody>
</table>
<p>How to perform the pop():</p>
<ol>
<li>Change the “top” link</li>
<li>Return the old top node</li>
</ol>
<p>Linked list after running operation (note that two links go to the same node):</p>
<p><a href="#r0">top</a> now points to second row</p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="r1">7</span> (return this value)</td>
<td><a href="#r0">pointer</a></td>
</tr>
<tr>
<td><span id="r0">5</span></td>
<td><a href="#r2">pointer</a></td>
</tr>
<tr>
<td><span id="r2">8</span></td>
<td></td>
</tr>
</tbody>
</table>
<ul>
<li>Caveat 1: dont lose the old top value</li>
<li>Caveat 2: dont ignore the old top node (it still consumes space)</li>
</ul>
<h3 id="improved-pop">“Improved” pop():</h3>
<ol>
<li>Store the old top value in “temp”</li>
<li>make <em>top</em> link to the new top node</li>
<li><em>free</em> the space for the old top node</li>
<li>return temp</li>
</ol>
<h2 id="the-list-class-a-doubly-linked-list-implementation-of-a-list">The List Class (A doubly-linked list implementation of a list)</h2>
<p>See <a href="./list.cpp">list.cpp</a></p>
<p>&lt;…5,6,7,…&gt; in a double linked list:</p>
<table>
<thead>
<tr>
<th>Prev</th>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td><span id="d0">5</span></td>
<td><a href="#d1">next</a></td>
</tr>
<tr>
<td><a href="#d0">prev</a></td>
<td><span id="d1">6</span></td>
<td><a href="#d2">next</a></td>
</tr>
<tr>
<td><a href="#d1">prev</a></td>
<td><span id="d2">7</span></td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="end">End</h2>
<footer>
</footer>
</div>
</main>
</body>
</html>

@ -1,160 +0,0 @@
---
layout: simple
math: true
---
# Dynamic Data Structures - Linked Lists
Transcriber's note:
Each row in the Value\|Pointer/Link tables represents a node.
Despite the fact that the link/pointer takes you to the "value" column, in reality it is taking you to the node "row", it's not just a pointer to the value.
## Dynamic Data Structures
* Basic types (e.g. int, char, bool, ...), and arrays of these store a *fixed* amount of data.
* We want implementations of ADTs like stacks + queues to *grow & shrink* (their memory use) *as needed*.
* e.g. like Vector, Array-something? (can't read), String classes
Basic Idea:
Store data in a collection of (simple) objects.
add/delete these as needed; link them all together to make the main object.
## Linked Lists
- A sequence of simple objects (nodes): each storing one datum (plus a link...) linked together in a chain
- E.g., to store the list &lt;3, 5, 7&gt;:
Datum|Link
---|---
<span id="z1">3</span>|[link](#z2)
<span id="z2">5</span>|[link](#z3)
<span id="z3">7</span>|link to nowhere
- These objects have no names, (in contrast to declared values)
- we access them by following links
- in Java, references (implemented as pointers)
- in C++, pointers
Need one named place to start like so:
A normal variable of type "pointer to a node": [First](#y1)
Data|Link
---|---
<span id="y1">3</span>|[pointer](#y2)
<span id="y2">5</span>|[pointer](#y3)
<span id="y3">7</span>|null pointer
## Implementing a Stack with a Linked List (By example)
Code to run, followed by the current stack.
### <code>stack s;</code>
<label for="tof">top or front</label>
<span id="tof">node</span>
### <code>s.push(3)</code>
<a href="#x1">front</a>
Data|Pointer
---|---
<span id="x1">3</span>|null pointer
### etc...
...
### <code>s.push(5)</code>
[front](#w1)
Data|Pointer
---|---
<span id="w1">5</span>|[pointer](#w2)
<span id="w2">8</span>|[pointer](#w3)
<span id="w3">...</span>|... ([pointer](#w4))
<span id="w4">3</span>|null pointer
### <code>s.push(7)</code>
[front](#v1)
Value|Pointer
<span id="v1">7</span>|[pointer](#v2)
<span id="v2">5</span>|[pointer](#v3)
<span id="v3">8</span>|[pointer](#v4)
<span id="v4">...</span>|...
To perform the push(7):
1. Make a new node to store the 7
2. modify links to insert it correctly
Original:
[front](#u1)
Data|Pointer
---|---
<span id="u1">5</span>|[pointer](#u2)
<span id="u2">8</span>|...
After operation:
[front](#t1) now points at node with value 7; node with value 7's pointer now points to the [old front](#u1)
Value|Pointer
---|---
<span id="t1">7</span>|[pointer](#t2)
<span id="t2">5</span>|[pointer](#t3)
<span id="t3">8</span>|...
### <code>s.pop()</code>
[front](#s1)
Value|Pointer
---|---
<span id="s1">5</span>|[pointer](#s2)
<span id="s2">8</span>|...
How to perform the pop():
1. Change the "top" link
2. Return the old top node
Linked list after running operation (note that two links go to the same node):
[top](#r0) now points to second row
Value|Pointer
---|---
<span id="r1">7</span> (return this value)|[pointer](#r0)
<span id="r0">5</span>|[pointer](#r2)
<span id="r2">8</span>|...
* Caveat 1: don't lose the old top value
* Caveat 2: don't ignore the old top node (it still consumes space)
### "Improved" pop():
1. Store the old top value in "temp"
2. make *top* link to the new top node
3. *free* the space for the old top node
3. return temp
## The List Class (A doubly-linked list implementation of a list)
See <a href="./list.cpp">list.cpp</a>
&lt;...5,6,7,...&gt; in a double linked list:
Prev|Value|Next
---|---|---
...|<span id="d0">5</span>|[next](#d1)
[prev](#d0)|<span id="d1">6</span>|[next](#d2)
[prev](#d1)|<span id="d2">7</span>|...
## End

@ -1,22 +0,0 @@
#include <utility> // not in original, needed for compilation
template <typename Object>
class List
{
private:
// the basic doubly linked list node
// Nested inside a list, can be public
// because the node itself is private
struct Node
{
Object data; // (annotation: list element)
Node *prev; // (annotation: pointer to next node)
Node *next; // (annotation: pointer to previous node)
Node(const Object& d = Object{}, Node* p = nullptr, Node* n = nullptr)
: data{d}, prev{p}, next{n} {}
Node(Object&& d, Node* p = nullptr, Node* n = nullptr)
: data{std::move(d)}, prev{p}, next{n} {}
};
}; // not in original, needed for compilation

@ -1,710 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="/assets/css/katex.css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-225call-stack--heap-memory">CMPT 225Call Stack &amp; Heap Memory</h1>
<h2 id="the-call-stack">The call stack</h2>
<ul>
<li>Suppose a function <code class="language-plaintext highlighter-rouge">A</code> calls a function <code class="language-plaintext highlighter-rouge">B</code>, which calls <code class="language-plaintext highlighter-rouge">C</code>.</li>
<li>During execution control passes from (the code for) <code class="language-plaintext highlighter-rouge">A</code>, to <code class="language-plaintext highlighter-rouge">B</code>, then to <code class="language-plaintext highlighter-rouge">C</code>.</li>
<li>The execution of <code class="language-plaintext highlighter-rouge">C</code> ends, control must return to <code class="language-plaintext highlighter-rouge">B</code> and then to <code class="language-plaintext highlighter-rouge">A</code>.</li>
</ul>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mo></mo><mi>A</mi><mo></mo><mi>B</mi><mo></mo><mi>C</mi><mo></mo><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi></mrow><annotation encoding="application/x-tex">
... \leftrightarrow A \leftrightarrow B \leftrightarrow C \leftrightarrow ...
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathdefault">A</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.05017em;">B</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.07153em;">C</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.10556em;vertical-align:0em;"></span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span></span></span></span></span></p>
<ul>
<li>At each function call, the system records where control should return to be <em>pushing an activation record on the call stack</em></li>
<li>The call stack also records all local variables, including the arguments to the function call.</li>
</ul>
<h2 id="call-stack-illustration">Call Stack Illustration</h2>
<p>Code is in <a href="./call_stack.cpp">call_stack.cpp</a></p>
<p>Call stack for this process:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Data</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>main</td>
<td>a=1, b=0</td>
</tr>
<tr>
<td>f(2)</td>
<td>c=2, d=3, e=8</td>
</tr>
<tr>
<td>g(4)</td>
<td>i=4</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>g</td>
<td>&lt;code for g&gt;</td>
</tr>
<tr>
<td>f</td>
<td>&lt;code for f&gt;</td>
</tr>
<tr>
<td>main</td>
<td>&lt;code for main&gt;</td>
</tr>
</tbody>
</table>
<h2 id="stack-code">Stack Code</h2>
<p>Picture of code, which can be found in <a href="./stack_pointers.cpp">stack_pointers.cpp</a></p>
<p>Picture of code output, which can be found in <a href="stack_pointers_output.txt">stack_pointers_output.txt</a></p>
<h2 id="dynamic-memory-or-heap">Dynamic Memory <em>or</em> Heap</h2>
<ul>
<li>Vairables declared in functions are stored on the <em>call stack</em></li>
<li>These variables:
<ul>
<li>are of fixed size</li>
<li>are destoryed when the function they are defined in terminates</li>
</ul>
</li>
<li>We often want a function <code class="language-plaintext highlighter-rouge">f</code> to create data that can be used after it returns.
<ul>
<li>In particular, dynamic data structures require this!</li>
</ul>
</li>
<li>This data is stored in “the heap”, a region of memory that is allocated dynamically as needed!</li>
</ul>
<h2 id="in-c">In C++:</h2>
<ul>
<li>Basic (or primitive) types can be stored on the call stack <em>or</em> on the heap.</li>
<li>Objects (e.g. instances of classes) can be stored on the call stack <em>or</em> on the heap.</li>
<li>Variables declared in functions are on the stack</li>
<li>Allocation on the heap is denoted by “new”.</li>
</ul>
<h2 id="ex-basic-types-on-call-stack--heap">Ex: Basic Types on Call Stack &amp; Heap</h2>
<p>Code snippet below:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>f(){
int n; //n is on stack
n=6;
int * np;//np is on stack
np = new int;// new int is stored in heap
*np = 7; // np points to the location.
}
</code></pre></div></div>
<h2 id="ex-basic-types-on-call-stack--heap-1">Ex: Basic Types on Call Stack &amp; Heap</h2>
<p>See code above.</p>
<p>Call stack:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<tr>
<td>n</td>
<td>6</td>
<td> </td>
</tr>
<tr>
<td>np</td>
<td><a href="#h1">pointer</a></td>
<td><code class="language-plaintext highlighter-rouge">np</code> is a pointer to the location on the heap.</td>
</tr>
</tbody>
</table>
<p>Heap:</p>
<table>
<thead>
<tr>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>...</td>
</tr>
<td><span id="h1">7</span></td>
<tr>
<td>...</td>
</tr>
</tbody>
</table>
<p>Note: When <code class="language-plaintext highlighter-rouge">f</code> ends, <code class="language-plaintext highlighter-rouge">np</code> is gone (the stack is popped), but the space it pointed to <em>is not</em>.</p>
<h2 id="class-instances-on-heap--stack-combines-notes-on-two-slides">Class Instances on Heap &amp; Stack (combines notes on two slides)</h2>
<p>Code snippet:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">List</span> <span class="o">*</span> <span class="nf">f</span><span class="p">(){</span>
<span class="n">List</span> <span class="n">L</span><span class="p">;</span>
<span class="n">List</span> <span class="o">*</span> <span class="n">Lp</span><span class="p">;</span>
<span class="n">Lp</span> <span class="o">=</span> <span class="k">new</span> <span class="n">List</span><span class="p">();</span>
<span class="k">return</span> <span class="n">Lp</span><span class="p">;</span> <span class="c1">// returns pointer</span>
<span class="p">}</span>
<span class="n">main</span><span class="p">(){</span>
<span class="p">...</span>
<span class="n">List</span> <span class="o">*</span> <span class="n">lst</span> <span class="o">=</span> <span class="n">f</span><span class="p">();</span> <span class="c1">// lst becomes a pointer to the list object</span>
<span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Call Stack:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Values</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>lst</td>
<td><a href="#o2">pointer to heap</a></td>
<td>in main</td>
</tr>
<tr>
<td>L</td>
<td>&lt;List Object&gt;</td>
<td>entire List object; in function <code class="language-plaintext highlighter-rouge">f</code> (destroyed when f ends)</td>
</tr>
<tr>
<td>Lp</td>
<td><a href="#o2">pointer to &lt;List Object&gt;</a></td>
<td>in function <code class="language-plaintext highlighter-rouge">f</code> (this <em>pointer</em> is also destroyed when f exits); annotation: this instanciates the list class</td>
</tr>
</tbody>
</table>
<p>Heap:</p>
<table>
<tbody>
<tr>
<td>Value</td>
<td>Notes</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td><span id="o2">&lt;List Object&gt;</span></td>
<td>entire list object</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="accessing-instance-members-in-c-combination-of-notes-in-two-slides">Accessing Instance Members in C++ (combination of notes in two slides)</h2>
<p>Suppose a class <em>Store</em> with:</p>
<ul>
<li>a data member <code class="language-plaintext highlighter-rouge">x</code>. (an int)</li>
<li>a function <code class="language-plaintext highlighter-rouge">put(v)</code> that stores <code class="language-plaintext highlighter-rouge">v</code> in <code class="language-plaintext highlighter-rouge">x</code>.</li>
<li>a function <code class="language-plaintext highlighter-rouge">get()</code> that returns the value of <code class="language-plaintext highlighter-rouge">x</code>.</li>
</ul>
<p>Consider this code fragment:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">f</span><span class="p">(){</span>
<span class="p">...</span>
<span class="n">Store</span> <span class="n">s1</span><span class="p">;</span>
<span class="n">s1</span><span class="p">.</span><span class="n">put</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span>
<span class="n">y</span><span class="o">=</span><span class="n">s1</span><span class="p">.</span><span class="n">get</span><span class="p">();</span><span class="c1">//y=5</span>
<span class="p">...</span>
<span class="n">Store</span> <span class="o">*</span><span class="n">s2</span><span class="p">;</span>
<span class="n">s2</span><span class="o">=</span><span class="k">new</span> <span class="n">Store</span><span class="p">();</span><span class="c1">// (annotation: XX)</span>
<span class="n">s2</span><span class="p">.</span><span class="n">put</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span><span class="c1">// (annotation: X)</span>
<span class="n">y</span><span class="o">=</span><span class="n">s2</span><span class="p">.</span><span class="n">get</span><span class="p">()</span><span class="c1">// (annotation: X)</span>
<span class="p">...</span>
<span class="o">*</span><span class="n">s2</span><span class="p">.</span><span class="n">put</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span><span class="c1">// (annotation: X)</span>
<span class="n">y</span><span class="o">=*</span><span class="n">s2</span><span class="p">.</span><span class="n">get</span><span class="p">();</span><span class="c1">// (annotation: X)</span>
<span class="p">...</span>
<span class="p">(</span><span class="o">*</span><span class="n">s2</span><span class="p">).</span><span class="n">put</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span><span class="c1">// (annotation: check mark)</span>
<span class="n">y</span><span class="o">=</span><span class="p">(</span><span class="o">*</span><span class="n">s2</span><span class="p">).</span><span class="n">get</span><span class="p">();</span><span class="c1">// (annotation: check mark)</span>
<span class="p">...</span>
<span class="n">s2</span><span class="o">-&gt;</span><span class="n">put</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span> <span class="c1">// equiv. to (*s).put(5)// (annotation: check mark)</span>
<span class="n">y</span><span class="o">=</span><span class="n">s2</span><span class="o">-&gt;</span><span class="n">get</span><span class="p">();</span> <span class="c1">// equiv. to y=(*s).get()// (annotation: check mark)</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Call Stack:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>s2</td>
<td><a href="#pts">pointer to &lt;Store Object&gt;</a></td>
<td> </td>
</tr>
<tr>
<td>s1</td>
<td>&lt;Store Object [x=5]&gt;</td>
<td> </td>
</tr>
</tbody>
</table>
<p>Heap:</p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="pts">&lt;Store Object [x=5]&gt;</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="using-the-textbook-list-class">Using the Textbook List Class</h2>
<p>See <a href="./textbook_list.cpp">textbook_list.cpp</a>
and <a href="./List.h">List.h</a>.</p>
<p>Call stack during the run of the program:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>N</td>
<td>5</td>
<td>within main</td>
</tr>
<tr>
<td>lst</td>
<td>&lt;List Object [size=5, head=0, tail=4]&gt;</td>
<td>within main</td>
</tr>
</tbody>
</table>
<p>The heap is empty.</p>
<p>See “doubly linked list” example in lecture 05.</p>
<h2 id="the-list-class">The List Class</h2>
<p>Two images of the doubly linked list slides from lecture 05 with a big red X written overtop each image.</p>
<h2 id="the-list-class---constructor">The List Class - Constructor</h2>
<p>Code snippet:</p>
<pre><code class="language-C++">private:
int theSize;
Node *head;
Node *tail;
void init()
{
theSize = 0;
head = new Node;
tail = new Node;
head-&gt;next = tail;
tail-&gt;prev = head;
}
};
</code></pre>
<p>After constructor:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Next</th>
<th>Value</th>
<th>Prev</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="t0">head</span></td>
<td><a href="#t1">pointer</a></td>
<td>nullptr</td>
<td>nullptr</td>
</tr>
<tr>
<td><span id="t1">tail</span></td>
<td>nullptr</td>
<td>nullptr</td>
<td><a href="#t0">pointer</a></td>
</tr>
</tbody>
</table>
<p>Code snippet form <a href="./List.h">List.h</a>:</p>
<pre><code class="language-C++">Node(const Object&amp; d = Object{}, Node* p = nullptr, Node* n = nullptr)
: data{d}, prev{p}, next{n} {}
</code></pre>
<h2 id="the-list-class---the-iterators">The List Class - The iterators</h2>
<ul>
<li>Data member (Node* current)
<ul>
<li>a pointer to a Node. (the list iterators are implemented with pointers.)</li>
</ul>
</li>
<li>Constructors:
<ul>
<li>turn a pointer into an iterator:
<ul>
<li><code class="language-plaintext highlighter-rouge">iterator(Node\* p): const_iterator{p}{}</code></li>
<li><code class="language-plaintext highlighter-rouge">const_iterator(Node\* p): current{p}</code></li>
</ul>
</li>
</ul>
</li>
<li>function:
<ul>
<li><code class="language-plaintext highlighter-rouge">iterator end(){ return iterator(tail}</code>
<ul>
<li>turns the tail pointer into the iter to “end”. (???cant read completely)</li>
<li>it corresponds to “just past the end” of the list</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="the-list-class---the-push_back-function">The List Class - the push_back function</h2>
<pre><code class="language-C++">// add an element to the tail end of the list
// end() is the end iterator
// x is the element to add
void push_back(const Object&amp; x){insert(end(), x);}
iterator insert(iterator itr, const Object&amp; x){
Node *p = itr.current; // turns the iterator into a pointer
++theSize; //increments size variable
return iterator(p-&gt;prev=p-&gt;prev-&gt;next=new Node(x, p-&gt;prev, p)) // turns the pointer into an iterator
// p-&gt;prev=p-&gt;prev-&gt;next=new Node(x, p-&gt;prev, p)) does two things:
// 1. Makes a new node `N`
// 2. stores a pointer to `N` in p-&gt;prev and it p-&gt;prev-&gt;next
}
</code></pre>
<h2 id="the-list-class---inserting-the-first-element-combination-of-notes-from-two-slides">The List Class - Inserting the first element (combination of notes from two slides)</h2>
<p>Code snippet:</p>
<pre><code class="language-C++">// itr is the end
iterator insert(iterator itr, const Object&amp; x){
Node *p = itr.current;// itr.current is the tail
++theSize;
return iterator(p-&gt;prev=p-&gt;prev-&gt;next=new Node(x, p-&gt;prev, p)); // turns pointer into iterator
// x is new list element
// p-&gt;prev is initial value of prev
// p is initial value of next
}
</code></pre>
<p>List pointed to by <a href="#d1">pointer p</a> (original)</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Next</th>
<th>Value</th>
<th>Prev</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="d0">head</span></td>
<td><a href="#d1">pointer</a></td>
<td>nullptr</td>
<td>nullptr</td>
</tr>
<tr>
<td><span id="d1">tail</span></td>
<td>nullptr</td>
<td>nullptr</td>
<td><a href="#d0">pointer</a></td>
</tr>
</tbody>
</table>
<p>The “new Node(…)” object:</p>
<table>
<thead>
<tr>
<th>Next</th>
<th>Value</th>
<th>Prev</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#d1">pointer</a></td>
<td>nullptr</td>
<td><a href="#d0">pointer</a></td>
</tr>
</tbody>
</table>
<h2 id="transcribers-addition-implied-in-diagram-what-does-p-prevp-prev-nextnew-node-do-to-this-list">Transcribers addition (implied in diagram): What does <code class="language-plaintext highlighter-rouge">p-&gt;prev=p-&gt;prev-&gt;next=New Node(...)</code> do to this list?</h2>
<p>Transcibers note: It inserts the <code class="language-plaintext highlighter-rouge">new Node(...)</code> into the list like so:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Next</th>
<th>Value</th>
<th>Prev</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="l0">head</span></td>
<td><a href="#l2">pointer</a></td>
<td>nullptr</td>
<td>nullptr</td>
</tr>
<tr>
<td><span id="l2"><code class="language-plaintext highlighter-rouge">new Node(...)</code></span></td>
<td><a href="#l1">pointer</a></td>
<td>nullptr</td>
<td><a href="#l0">pointer</a></td>
</tr>
<tr>
<td><span id="l1">tail</span></td>
<td>nullptr</td>
<td>nullptr</td>
<td><a href="#l2">pointer</a></td>
</tr>
</tbody>
</table>
<p>Transcibers note: This cancels the previous connection between <code class="language-plaintext highlighter-rouge">head</code> and <code class="language-plaintext highlighter-rouge">tail</code> directly.</p>
<h2 id="using-the-textbook-list-class-1">Using the Textbook List Class</h2>
<p>Code snippet:</p>
<pre><code class="language-C++">#include "dsexceptions.h"
#include "List.h"
int main(){
const int N = 5;
List&lt;int&gt; = lst;
for(int i=N-1; i&lt;0; --i){
lst.push_front(i);
}
return 0;
}
</code></pre>
<p>When lst contains the two values [1, 2]:</p>
<p>Call Stack:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>N</td>
<td>5</td>
<td> </td>
</tr>
<tr>
<td>lst</td>
<td>&lt;List Object [theSize=2, head=<a href="#g3v">pointer</a>, tail=<a href="#g1v">pointer</a>&gt;</td>
<td> </td>
</tr>
</tbody>
</table>
<p>Heap (transcribers note: the link takes you to the value, but the pointer is to the group of values [next, value, prev), A.K.A. a Node):</p>
<table>
<tbody>
<tr>
<td>Value</td>
<td>Notes</td>
</tr>
<tr>
<td><a href="#g2v">Node.next</a></td>
<td>Node index 0</td>
</tr>
<tr>
<td><span id="g0v">Node Value: 1</span></td>
<td>Node index 0</td>
</tr>
<tr>
<td><a href="#g3v">Node.prev</a></td>
<td>Node index 0</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>Node.next (nullptr)</td>
<td>Ending Node</td>
</tr>
<tr>
<td><span id="g1v">Node Value: ?</span></td>
<td>Ending Node</td>
</tr>
<tr>
<td><a href="#g2v">Node.prev</a></td>
<td>Ending Node</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td><a href="#g1v">Node.next</a></td>
<td>Node index 1</td>
</tr>
<tr>
<td><span id="g2v">Node Value: 2</span></td>
<td>Node index 1</td>
</tr>
<tr>
<td><a href="#g0v">Node.prev</a></td>
<td>Node index 1</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td><a href="#g0v">Node.next</a></td>
<td>Beginning Node</td>
</tr>
<tr>
<td><span id="g3v">Node Value: ?</span></td>
<td>Beginning Node</td>
</tr>
<tr>
<td>Node.prev (nullptr)</td>
<td>Beginning Node</td>
</tr>
</tbody>
</table>
<h2 id="linked-list-ends">Linked List Ends</h2>
<p>Transcribers note: tell me if you like this way of transcribing simplified linked lists where the individual values dont matter, but only a linear relationship between the nodes:</p>
<p>For [4, 5, 6]:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mn>4</mn><mo></mo><mn>5</mn><mo></mo><mn>6</mn><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>versus</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mn>4</mn><mo></mo><mn>5</mn><mo></mo><mn>6</mn><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>For [5]:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mn>5</mn><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>versus</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo></mo><mn>5</mn><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} \leftrightarrow 5 \leftrightarrow \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<p>For []:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<p>versus (transcribers note: the lack of connection is intentional)</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo separator="true">,</mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} , \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<h2 id="end">End</h2>
<p>After the “End” slide, some previous slides are repeated for no apparent reason.</p>
<footer>
</footer>
</div>
</main>
</body>
</html>

@ -1,393 +0,0 @@
---
layout: simple
math: true
---
# CMPT 225--Call Stack & Heap Memory
## The call stack
* Suppose a function `A` calls a function `B`, which calls `C`.
* During execution control passes from (the code for) `A`, to `B`, then to `C`.
* The execution of `C` ends, control must return to `B` and then to `A`.
{% katex display %}
... \leftrightarrow A \leftrightarrow B \leftrightarrow C \leftrightarrow ...
{% endkatex %}
* At each function call, the system records where control should return to be *pushing an activation record on the call stack*
* The call stack also records all local variables, including the arguments to the function call.
## Call Stack Illustration
Code is in [call_stack.cpp](./call_stack.cpp)
Call stack for this process:
Name|Data
---|---
...|...
main|a=1, b=0
f(2)|c=2, d=3, e=8
g(4)|i=4
...|...
g|&lt;code for g&gt;
f|&lt;code for f&gt;
main|&lt;code for main&gt;
## Stack Code
Picture of code, which can be found in [stack_pointers.cpp](./stack_pointers.cpp)
Picture of code output, which can be found in [stack_pointers_output.txt](stack_pointers_output.txt)
## Dynamic Memory *or* Heap
* Vairables declared in functions are stored on the *call stack*
* These variables:
* are of fixed size
* are destoryed when the function they are defined in terminates
* We often want a function `f` to create data that can be used after it returns.
* In particular, dynamic data structures require this!
* This data is stored in "the heap", a region of memory that is allocated dynamically as needed!
## In C++:
* Basic (or primitive) types can be stored on the call stack *or* on the heap.
* Objects (e.g. instances of classes) can be stored on the call stack *or* on the heap.
* Variables declared in functions are on the stack
* Allocation on the heap is denoted by "new".
## Ex: Basic Types on Call Stack & Heap
Code snippet below:
```
f(){
int n; //n is on stack
n=6;
int * np;//np is on stack
np = new int;// new int is stored in heap
*np = 7; // np points to the location.
}
```
## Ex: Basic Types on Call Stack & Heap
See code above.
Call stack:
Name|Value|Note
---|---|---
n|6|
np|[pointer](#h1)|`np` is a pointer to the location on the heap.
Heap:
<table>
<thead>
<tr>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>...</td>
</tr>
<td><span id="h1">7</span></td>
<tr>
<td>...</td>
</tr>
</tbody>
</table>
Note: When `f` ends, `np` is gone (the stack is popped), but the space it pointed to *is not*.
## Class Instances on Heap & Stack (combines notes on two slides)
Code snippet:
```c++
List * f(){
List L;
List * Lp;
Lp = new List();
return Lp; // returns pointer
}
main(){
...
List * lst = f(); // lst becomes a pointer to the list object
...
}
```
Call Stack:
Name|Values|Notes
---|---|---
lst|[pointer to heap](#o2)|in main
L|&lt;List Object&gt;|entire List object; in function `f` (destroyed when f ends)
Lp|[pointer to &lt;List Object&gt;](#o2)|in function `f` (this *pointer* is also destroyed when f exits); annotation: this instanciates the list class
Heap:
Value|Notes
...|...
<span id="o2">&lt;List Object&gt;</span>|entire list object
...|...
## Accessing Instance Members in C++ (combination of notes in two slides)
Suppose a class *Store* with:
* a data member `x`. (an int)
* a function `put(v)` that stores `v` in `x`.
* a function `get()` that returns the value of `x`.
Consider this code fragment:
```c++
f(){
...
Store s1;
s1.put(5);
y=s1.get();//y=5
...
Store *s2;
s2=new Store();// (annotation: XX)
s2.put(5);// (annotation: X)
y=s2.get()// (annotation: X)
...
*s2.put(5);// (annotation: X)
y=*s2.get();// (annotation: X)
...
(*s2).put(5);// (annotation: check mark)
y=(*s2).get();// (annotation: check mark)
...
s2->put(5); // equiv. to (*s).put(5)// (annotation: check mark)
y=s2->get(); // equiv. to y=(*s).get()// (annotation: check mark)
}
```
Call Stack:
Name|Value|Notes
---|---|---
s2|[pointer to &lt;Store Object&gt;](#pts)|
s1|&lt;Store Object [x=5]&gt;|
Heap:
Value|Notes
---|---
<span id="pts">&lt;Store Object [x=5]&gt;</span>|
## Using the Textbook List Class
See [textbook_list.cpp](./textbook_list.cpp)
and [List.h](./List.h).
Call stack during the run of the program:
Name|Value|Notes
---|---
N|5|within main
lst|&lt;List Object [size=5, head=0, tail=4]&gt;|within main
The heap is empty.
See "doubly linked list" example in lecture 05.
## The List Class
Two images of the doubly linked list slides from lecture 05 with a big red X written overtop each image.
## The List Class - Constructor
Code snippet:
```C++
private:
int theSize;
Node *head;
Node *tail;
void init()
{
theSize = 0;
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
}
};
```
After constructor:
Name|Next|Value|Prev
---|---|---|---
<span id="t0">head</span>|[pointer](#t1)|nullptr|nullptr
<span id="t1">tail</span>|nullptr|nullptr|[pointer](#t0)
Code snippet form [List.h](./List.h):
```C++
Node(const Object& d = Object{}, Node* p = nullptr, Node* n = nullptr)
: data{d}, prev{p}, next{n} {}
```
## The List Class - The iterators
* Data member (Node\* current)
* a pointer to a Node. (the list iterators are implemented with pointers.)
* Constructors:
* turn a pointer into an iterator:
* `iterator(Node\* p): const_iterator{p}{}`
* `const_iterator(Node\* p): current{p}`
* function:
* `iterator end(){ return iterator(tail}`
* turns the tail pointer into the iter to "end". (???can't read completely)
* it corresponds to "just past the end" of the list
## The List Class - the push_back function
```C++
// add an element to the tail end of the list
// end() is the end iterator
// x is the element to add
void push_back(const Object& x){insert(end(), x);}
iterator insert(iterator itr, const Object& x){
Node *p = itr.current; // turns the iterator into a pointer
++theSize; //increments size variable
return iterator(p->prev=p->prev->next=new Node(x, p->prev, p)) // turns the pointer into an iterator
// p->prev=p->prev->next=new Node(x, p->prev, p)) does two things:
// 1. Makes a new node `N`
// 2. stores a pointer to `N` in p->prev and it p->prev->next
}
```
## The List Class - Inserting the first element (combination of notes from two slides)
Code snippet:
```C++
// itr is the end
iterator insert(iterator itr, const Object& x){
Node *p = itr.current;// itr.current is the tail
++theSize;
return iterator(p->prev=p->prev->next=new Node(x, p->prev, p)); // turns pointer into iterator
// x is new list element
// p->prev is initial value of prev
// p is initial value of next
}
```
List pointed to by [pointer p](#d1) (original)
Name|Next|Value|Prev
---|---|---|---
<span id="d0">head</span>|[pointer](#d1)|nullptr|nullptr
<span id="d1">tail</span>|nullptr|nullptr|[pointer](#d0)
The "new Node(...)" object:
Next|Value|Prev
---|---|---
[pointer](#d1)|nullptr|[pointer](#d0)
## Transcriber's addition (implied in diagram): What does `p->prev=p->prev->next=New Node(...)` do to this list?
Transciber's note: It inserts the `new Node(...)` into the list like so:
Name|Next|Value|Prev
---|---|---|---
<span id="l0">head</span>|[pointer](#l2)|nullptr|nullptr
<span id="l2">`new Node(...)`</span>|[pointer](#l1)|nullptr|[pointer](#l0)
<span id="l1">tail</span>|nullptr|nullptr|[pointer](#l2)
Transciber's note: This cancels the previous connection between `head` and `tail` directly.
## Using the Textbook List Class
Code snippet:
```C++
#include "dsexceptions.h"
#include "List.h"
int main(){
const int N = 5;
List<int> = lst;
for(int i=N-1; i<0; --i){
lst.push_front(i);
}
return 0;
}
```
When lst contains the two values [1, 2]:
Call Stack:
Name|Value|Notes
---|---|---
N|5|
lst|&lt;List Object [theSize=2, head=[pointer](#g3v), tail=[pointer](#g1v)&gt;|
Heap (transcriber's note: the link takes you to the value, but the pointer is to the group of values [next, value, prev), A.K.A. a Node):
Value|Notes
[Node.next](#g2v)|Node index 0
<span id="g0v">Node Value: 1</span>|Node index 0
[Node.prev](#g3v)|Node index 0
...|...
Node.next (nullptr)|Ending Node
<span id="g1v">Node Value: ?</span>|Ending Node
[Node.prev](#g2v)|Ending Node
...|...
[Node.next](#g1v)|Node index 1
<span id="g2v">Node Value: 2</span>|Node index 1
[Node.prev](#g0v)|Node index 1
...|...
[Node.next](#g0v)|Beginning Node
<span id="g3v">Node Value: ?</span>|Beginning Node
Node.prev (nullptr)|Beginning Node
## Linked List Ends
Transcriber's note: tell me if you like this way of transcribing simplified linked lists where the individual values don't matter, but only a linear relationship between the nodes:
For [4, 5, 6]:
{% katex display %}
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
{% endkatex %}
versus
{% katex display %}
\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail}
{% endkatex %}
For [5]:
{% katex display %}
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
{% endkatex %}
versus
$$\text{head} \leftrightarrow 5 \leftrightarrow \text{tail}$$
For []:
$$\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail}$$
versus (transcriber's note: the lack of connection is intentional)
$$\text{head} , \text{tail}$$
## End
After the "End" slide, some previous slides are repeated for no apparent reason.

@ -1,22 +0,0 @@
#include <utility> // not in original, needed for compilation
template <typename Object>
class List
{
private:
// the basic doubly linked list node
// Nested inside a list, can be public
// because the node itself is private
struct Node
{
Object data; // (annotation: list element)
Node *prev; // (annotation: pointer to next node)
Node *next; // (annotation: pointer to previous node)
Node(const Object& d = Object{}, Node* p = nullptr, Node* n = nullptr)
: data{d}, prev{p}, next{n} {}
Node(Object&& d, Node* p = nullptr, Node* n = nullptr)
: data{std::move(d)}, prev{p}, next{n} {}
};
}; // not in original, needed for compilation

@ -1,19 +0,0 @@
/*
Transcriber's note: In the slide, the order of the functions is: main, f, g
But I have reordered them g, f, main to allow compilation.
*/
int g(int i){
return 2*i;
} // return control to f
int f(int c){
int d=3;
int e=g(4); // go to g
return e;
} // return control to main
int main(){
int a=1;
int b=f(2); // go to function f
return 0;
} // return from program

@ -1,38 +0,0 @@
/* A program to demonstrate printing out partial contents of the call stack */
#include <iostream> /* Note: only "#include" shown in image */
#include <iomanip> /* Note: only "#include" shwon in image */
using namespace std;
int print_stack(int k, int j){
cout << "print_stack() begins" << endl;
cout << "argument k is at &k=" << &k << " and k=" << k << endl;
cout << "argument j is at &j=" << &j << " and j=" << j << endl;
int CCC[2] = {77777777, 88888888};
cout << "Peeking from &j up, for the space of k ints" << endl;
int *p = (&j)+k;
for (int l=k; l>0; l--){
cout << p << ": " << setw(8) << hex << *p << " = " << setw(11) << dec << *p << endl;
p-=j;// subtracts j from an int pointer sets it to the j-th previous int
}
cout << "End of: print_stack()" << endl;
}
int ffff(int fun_arg){
cout << "fun() begins" << endl;
cout << "fun_arg is at &fun_arg=" << &fun_arg << endl;
int BBB[2] = {44444444,55555555};
cout << "BBB is at BBB=" << BBB << endl;
print_stack(40, +1);
cout << "fun ends" << endl;
}
int main(){
cout << "main() begins" << endl;
int XXXX = 99999999;
int AAAA[2] = {11111111,22222222};
ffff(33333333);
cout << "main() ends" << endl;
}

@ -1,51 +0,0 @@
main() begins
fun() begins
fun_arg is at &fun_arg=0xffffe9fe9e5c
BBB is at BBB=0xffffe9fe9e60
print_stack() begins
argument k is at &k=0xffffe9fe9e1c and k=40
argument j is at &j=0xffffe9fe9e18 and j=1
Peeking from &j up, for the space of k ints
0xffffe9fe9eb8: 0 = 0
0xffffe9fe9eb4: 0 = 0
0xffffe9fe9eb0: 0 = 0
0xffffe9fe9eac: aaaa = 43690
0xffffe9fe9ea8: b3e70b38 = -1276703944
0xffffe9fe9ea4: 0 = 0
0xffffe9fe9ea0: 0 = 0
0xffffe9fe9e9c: 195a3d0f = 425344271
0xffffe9fe9e98: 2b7fe700 = 729802496
0xffffe9fe9e94: 153158e = 22222222
0xffffe9fe9e90: a98ac7 = 11111111
0xffffe9fe9e8c: 5f5e0ff = 99999999
0xffffe9fe9e88: aba454fc = -1415293700
0xffffe9fe9e84: ffff = 65535
0xffffe9fe9e80: e9fe9ea0 = -369189216
0xffffe9fe9e7c: ffff = 65535
0xffffe9fe9e78: aba45538 = -1415293640
0xffffe9fe9e74: ffff = 65535
0xffffe9fe9e70: e9fe9ea0 = -369189216
0xffffe9fe9e6c: 195a3d0f = 425344271
0xffffe9fe9e68: 2b7fe700 = 729802496
0xffffe9fe9e64: 34fb5e3 = 55555555
0xffffe9fe9e60: 2a62b1c = 44444444
0xffffe9fe9e5c: 1fca055 = 33333333
0xffffe9fe9e58: b3e71074 = -1276702604
0xffffe9fe9e54: ffff = 65535
0xffffe9fe9e50: e9fe9e70 = -369189264
0xffffe9fe9e4c: aaaa = 43690
0xffffe9fe9e48: b3e710a4 = -1276702556
0xffffe9fe9e44: ffff = 65535
0xffffe9fe9e40: e9fe9e70 = -369189264
0xffffe9fe9e3c: 195a3d0f = 425344271
0xffffe9fe9e38: 2b7fe700 = 729802496
0xffffe9fe9e34: 54c5638 = 88888888
0xffffe9fe9e30: 4a2cb71 = 77777777
0xffffe9fe9e2c: ffff = 65535
0xffffe9fe9e28: e9fe9e28 = -369189336
0xffffe9fe9e24: 3 = 3
0xffffe9fe9e20: e9fe9e40 = -369189312
0xffffe9fe9e1c: 28 = 40
End of: print_stack()
fun ends
main() ends

@ -1,15 +0,0 @@
#include "dsexception.h" // transcriber's note: I have no idea where this file is or what it does???
#include "List.h"
using namespace std;
int main()
{
const int N = 5;
List<int> lst;
for (int i=N-1; i>0; --i)
{
lst.push_forward(i);
}
return 0;
}

@ -1,351 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="/assets/css/katex.css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="ll-queues--ll-traversal">L.L. Queues &amp; L.L. Traversal</h1>
<h2 id="basic-ll-queue">Basic L.L. Queue</h2>
<ul>
<li><a href="#frontele1">front</a></li>
<li><a href="#backele1">back</a></li>
</ul>
<table>
<thead>
<tr>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="frontele1">a</span></td>
<td><a href="#ele11">pointer</a></td>
</tr>
<tr>
<td><span id="ele11">b</span></td>
<td><a href="#ele12">pointer</a></td>
</tr>
<tr>
<td><span id="ele12"></span></td>
<td></td>
</tr>
<tr>
<td><span id="ele13">g</span></td>
<td><a href="#backele1">pointer</a></td>
</tr>
<tr>
<td><span id="backele1">h</span></td>
<td>nullptr</td>
</tr>
</tbody>
</table>
<p>as a stack, this L.L. would be:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>a</mi><mo></mo><mi>b</mi><mo></mo><mo></mo><mo></mo><mi>h</mi></mrow><annotation encoding="application/x-tex">a \rightarrow b \rightarrow \dots \rightarrow h</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="minner"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">h</span></span></span></span></span>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class Node{
Type data
Node * next
}
</code></pre></div></div>
<h2 id="enqueue--dequeue--first--versions">Enqueue + Dequeue First Versions</h2>
<p>Variables:</p>
<ul>
<li>float (pointer to stack)</li>
<li>back (pointer to stack)</li>
<li>size (# elements in queue)</li>
</ul>
<h3 id="dequeue">Dequeue:</h3>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dequeue(){
temp = front
val = front-&gt;data
front = front-&gt;next
delete temp
size = size-1
return val
}
</code></pre></div></div>
<p>Diagram:</p>
<ul>
<li><del><a href="#frontele2">front</a></del>
<ins><a href="#ele21">front</a></ins></li>
<li><a href="#backele2">back</a></li>
<li><a href="#frontele2">temp</a></li>
</ul>
<table>
<thead>
<tr>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td><del><span id="frontele2">a</span></del></td>
<td><del><a href="#ele21">pointer</a></del></td>
</tr>
<tr>
<td><span id="ele21">b</span></td>
<td><a href="#ele22">pointer</a></td>
</tr>
<tr>
<td><span id="ele22"></span></td>
<td></td>
</tr>
<tr>
<td><span id="ele13">g</span></td>
<td><a href="#backele2">pointer</a></td>
</tr>
<tr>
<td><span id="backele2">h</span></td>
<td>nullptr</td>
</tr>
</tbody>
</table>
<h3 id="unqueue">Unqueue</h3>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>enqueue(x){
n = new node containing x
back-&gt;next = n
back = back-&gt;next
size = size+1
}
</code></pre></div></div>
<p>Diagram:</p>
<ul>
<li><a href="#frontele3">front</a></li>
<li><del><a href="#backele3">back</a></del>
<ins><a href="#backele32">back</a></ins></li>
</ul>
<table>
<thead>
<tr>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="frontele3">a</span></td>
<td><a href="#ele31">pointer</a></td>
</tr>
<tr>
<td><span id="ele31">b</span></td>
<td><a href="#ele32">pointer</a></td>
</tr>
<tr>
<td><span id="ele32"></span></td>
<td></td>
</tr>
<tr>
<td><span id="ele33">g</span></td>
<td><a href="#backele3">pointer</a></td>
</tr>
<tr>
<td><span id="backele3">h</span></td>
<td><del>nullptr</del> <ins><a href="#backele32">pointer</a></ins></td>
</tr>
<tr>
<td><ins><span id="backele32">x</span></ins></td>
<td><ins>nullptr</ins></td>
</tr>
</tbody>
</table>
<h2 id="empty-queue">Empty Queue</h2>
<p>A stack with a</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>a</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow a \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>After dequeue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo separator="true">,</mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front}, \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>After enqueue(b)</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Enqueue &amp; Dequeue are different from empty/non-empty queues.</p>
<h2 id="enqueue">Enqueue</h2>
<p>Example 1: Empty queue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo separator="true">,</mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front}, \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Enqueue b</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Example 2: Starter queue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mo></mo><mo></mo><mi>x</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow \dots \rightarrow x \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="minner"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>enqueue b</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mo></mo><mo></mo><mi>x</mi><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow \dots \rightarrow x \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="minner"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>enqueue(x){
if size &gt; 0{
back-&gt;next = new Node(x)
back = back-&gt;next
} else {
back = new Node(x)
front = back
}
size = size+1
}
</code></pre></div></div>
<h2 id="dequeue-1">Dequeue</h2>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>front = front-&gt;next
return value
</code></pre></div></div>
<p>That code has the following effect upon a queue.</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>a</mi><mo></mo><mi>b</mi><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow a \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>becomes</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>while returning a.</p>
<h3 id="example-1">Example 1:</h3>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>dequeue (b)</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo separator="true">,</mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front}, \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<h3 id="example-2">Example 2:</h3>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>dequeue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Return b</p>
<h3 id="pseudo-code">Pseudo code</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dequeue(){
temp = front
val = front-&gt;data
front = front-&gt;next
delete temp
size = size-1
return val
}
</code></pre></div></div>
<h2 id="traversing-the-list">Traversing the List</h2>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>displayList(){
Node * cur = front;
while(cur!=nullptr){
output cur-&gt;data
cur = cur-&gt;next
}
}
</code></pre></div></div>
<p>A diagram which shows the cursor going from c, to d to be (in order from front to back).</p>
<h2 id="linked-list-ends">Linked List Ends</h2>
<p>Transcribers note: this is the same as in 06. The left/right arrows denote a double linked list, even though the current set of slides is talking about only singly linked lists.</p>
<p>For [4, 5, 6]:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mn>4</mn><mo></mo><mn>5</mn><mo></mo><mn>6</mn><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>versus</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mn>4</mn><mo></mo><mn>5</mn><mo></mo><mn>6</mn><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>For [5]:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mn>5</mn><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>versus</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo></mo><mn>5</mn><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} \leftrightarrow 5 \leftrightarrow \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<p>For []:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<p>versus</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo separator="true">,</mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} , \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<h2 id="list-class-traversing-the-list">List Class: Traversing the List</h2>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>displayList(){
Node * cur = head-&gt;next
while(cur!=tail){
output cur-&gt;data
cur = cur-&gt;next
}
}
</code></pre></div></div>
<p>Graphic of <code class="language-plaintext highlighter-rouge">cur</code> moving through each (empty) element of the queue.</p>
<h2 id="end">End</h2>
<footer>
</footer>
</div>
</main>
</body>
</html>

@ -1,349 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="/assets/css/katex.css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="ll-queues--ll-traversal">L.L. Queues &amp; L.L. Traversal</h1>
<h2 id="basic-ll-queue">Basic L.L. Queue</h2>
<ul>
<li><a href="#frontele1">front</a></li>
<li><a href="#backele1">back</a></li>
</ul>
<table>
<thead>
<tr>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="frontele1">a</span></td>
<td><a href="#ele11">pointer</a></td>
</tr>
<tr>
<td><span id="ele11">b</span></td>
<td><a href="#ele12">pointer</a></td>
</tr>
<tr>
<td><span id="ele12"></span></td>
<td></td>
</tr>
<tr>
<td><span id="ele13">g</span></td>
<td><a href="#backele1">pointer</a></td>
</tr>
<tr>
<td><span id="backele1">h</span></td>
<td>nullptr</td>
</tr>
</tbody>
</table>
<p>as a stack, this L.L. would be:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>a</mi><mo></mo><mi>b</mi><mo></mo><mo></mo><mo></mo><mi>h</mi></mrow><annotation encoding="application/x-tex">a \rightarrow b \rightarrow \dots \rightarrow h</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="minner"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">h</span></span></span></span></span>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class Node{
Type data
Node * next
}
</code></pre></div></div>
<h2 id="enqueue--dequeue--first--versions">Enqueue + Dequeue First Versions</h2>
<p>Variables:</p>
<ul>
<li>float (pointer to stack)</li>
<li>back (pointer to stack)</li>
<li>size (# elements in queue)</li>
</ul>
<h3 id="dequeue">Dequeue:</h3>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dequeue(){
temp = front
val = front-&gt;data
front = front-&gt;next
delete temp
size = size-1
return val
}
</code></pre></div></div>
<p>Diagram:</p>
<ul>
<li><del><a href="#frontele2">front</a></del>
<ins><a href="#ele21">front</a></ins></li>
<li><a href="#backele2">back</a></li>
<li><a href="#frontele2">temp</a></li>
</ul>
<table>
<thead>
<tr>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td><del><span id="frontele2">a</span></del></td>
<td><del><a href="#ele21">pointer</a></del></td>
</tr>
<tr>
<td><span id="ele21">b</span></td>
<td><a href="#ele22">pointer</a></td>
</tr>
<tr>
<td><span id="ele22"></span></td>
<td></td>
</tr>
<tr>
<td><span id="ele13">g</span></td>
<td><a href="#backele2">pointer</a></td>
</tr>
<tr>
<td><span id="backele2">h</span></td>
<td>nullptr</td>
</tr>
</tbody>
</table>
<h3 id="unqueue">Unqueue</h3>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>enqueue(x){
n = new node containing x
back-&gt;next = n
back = back-&gt;next
size = size+1
}
</code></pre></div></div>
<p>Diagram:</p>
<ul>
<li><a href="#frontele3">front</a></li>
<li><del><a href="#backele3">back</a></del>
<ins><a href="#backele32">back</a></ins></li>
</ul>
<table>
<thead>
<tr>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="frontele3">a</span></td>
<td><a href="#ele31">pointer</a></td>
</tr>
<tr>
<td><span id="ele31">b</span></td>
<td><a href="#ele32">pointer</a></td>
</tr>
<tr>
<td><span id="ele32"></span></td>
<td></td>
</tr>
<tr>
<td><span id="ele33">g</span></td>
<td><a href="#backele3">pointer</a></td>
</tr>
<tr>
<td><span id="backele3">h</span></td>
<td><del>nullptr</del> <ins><a href="#backele32">pointer</a></ins></td>
</tr>
<tr>
<td><ins><span id="backele32">x</span></ins></td>
<td><ins>nullptr</ins></td>
</tr>
</tbody>
</table>
<h2 id="empty-queue">Empty Queue</h2>
<p>A stack with a</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>a</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow a \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>After dequeue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo separator="true">,</mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front}, \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>After enqueue(b)</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Enqueue &amp; Dequeue are different from empty/non-empty queues.</p>
<h2 id="enqueue">Enqueue</h2>
<p>Example 1: Empty queue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo separator="true">,</mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front}, \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Enqueue b</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Example 2: Starter queue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mo></mo><mo></mo><mi>x</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow \dots \rightarrow x \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="minner"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>enqueue b</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mo></mo><mo></mo><mi>x</mi><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow \dots \rightarrow x \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="minner"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>enqueue(x){
if size &gt; 0{
back-&gt;next = new Node(x)
back = back-&gt;next
} else {
back = new Node(x)
front = back
}
size = size+1
}
</code></pre></div></div>
<h2 id="dequeue-1">Dequeue</h2>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>front = front-&gt;next
return value
</code></pre></div></div>
<p>That code has the following effect upon a queue.</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>a</mi><mo></mo><mi>b</mi><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow a \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>becomes</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>while returning a.</p>
<h3 id="example-1">Example 1:</h3>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>dequeue (b)</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo separator="true">,</mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front}, \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<h3 id="example-2">Example 2:</h3>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>dequeue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Return b</p>
<h3 id="pseudo-code">Pseudo code</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dequeue(){
temp = front
val = front-&gt;data
front = front-&gt;next
delete temp
size = size-1
return val
}
</code></pre></div></div>
<h2 id="traversing-the-list">Traversing the List</h2>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>displayList(){
Node * cur = front;
while(cur!=nullptr){
output cur-&gt;data
cur = cur-&gt;next
}
}
</code></pre></div></div>
<p>A diagram which shows the cursor going from c, to d to be (in order from front to back).</p>
<h2 id="linked-list-ends">Linked List Ends</h2>
<p>Transcribers note: this is the same as in 06. The left/right arrows denote a double linked list, even though the current set of slides is talking about only singly linked lists.</p>
<p>For [4, 5, 6]:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mn>4</mn><mo></mo><mn>5</mn><mo></mo><mn>6</mn><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>versus</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mn>4</mn><mo></mo><mn>5</mn><mo></mo><mn>6</mn><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>For [5]:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mn>5</mn><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>versus</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo></mo><mn>5</mn><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} \leftrightarrow 5 \leftrightarrow \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<p>For []:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<p>versus</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo separator="true">,</mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} , \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<h2 id="list-class-traversing-the-list">List Class: Traversing the List</h2>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>displayList(){
Node * cur = head-&gt;next
while(cur!=tail){
output cur-&gt;data
cur = cur-&gt;next
}
}
</code></pre></div></div>
<p>Graphic of <code class="language-plaintext highlighter-rouge">cur</code> moving through each (empty) element of the queue.</p>
<footer>
</footer>
</div>
</main>
</body>
</html>

@ -1,267 +0,0 @@
---
layout: simple
math: true
---
# L.L. Queues & L.L. Traversal
## Basic L.L. Queue
* [front](#frontele1)
* [back](#backele1)
Value|Next
---|---
<span id="frontele1">a</span>|[pointer](#ele11)
<span id="ele11">b</span>|[pointer](#ele12)
<span id="ele12">...</span>|...
<span id="ele13">g</span>|[pointer](#backele1)
<span id="backele1">h</span>|nullptr
as a stack, this L.L. would be:
$$a \rightarrow b \rightarrow \dots \rightarrow h$$
Pseudo-code:
```
class Node{
Type data
Node * next
}
```
## Enqueue + Dequeue -- First Versions
Variables:
* float (pointer to stack)
* back (pointer to stack)
* size (# elements in queue)
### Dequeue:
Pseudo-code:
```
dequeue(){
temp = front
val = front->data
front = front->next
delete temp
size = size-1
return val
}
```
Diagram:
* <del>[front](#frontele2)</del>
<ins>[front](#ele21)</ins>
* [back](#backele2)
* [temp](#frontele2)
Value|Next
---|---
<del><span id="frontele2">a</span></del>|<del>[pointer](#ele21)</del>
<span id="ele21">b</span>|[pointer](#ele22)
<span id="ele22">...</span>|...
<span id="ele13">g</span>|[pointer](#backele2)
<span id="backele2">h</span>|nullptr
### Unqueue
Pseudo-code:
```
enqueue(x){
n = new node containing x
back->next = n
back = back->next
size = size+1
}
```
Diagram:
* [front](#frontele3)
* <del>[back](#backele3)</del>
<ins>[back](#backele32)</ins>
Value|Next
---|---
<span id="frontele3">a</span>|[pointer](#ele31)
<span id="ele31">b</span>|[pointer](#ele32)
<span id="ele32">...</span>|...
<span id="ele33">g</span>|[pointer](#backele3)
<span id="backele3">h</span>|<del>nullptr</del> <ins>[pointer](#backele32)</ins>
<ins><span id="backele32">x</span></ins>|<ins>nullptr</ins>
## Empty Queue
A stack with a
$$\text{front} \rightarrow a \leftarrow \text{back}$$
After dequeue
$$\text{front}, \text{back}$$
After enqueue(b)
$$\text{front} \rightarrow b \leftarrow \text{back}$$
Enqueue & Dequeue are different from empty/non-empty queues.
## Enqueue
Example 1: Empty queue
$$\text{front}, \text{back}$$
Enqueue b
$$\text{front} \rightarrow b \leftarrow \text{back}$$
Example 2: Starter queue
$$\text{front} \rightarrow \dots \rightarrow x \leftarrow \text{back}$$
enqueue b
$$\text{front} \rightarrow \dots \rightarrow x \rightarrow b \leftarrow \text{back}$$
Pseudo code:
```
enqueue(x){
if size > 0{
back->next = new Node(x)
back = back->next
} else {
back = new Node(x)
front = back
}
size = size+1
}
```
## Dequeue
Pseudo code:
```
front = front->next
return value
```
That code has the following effect upon a queue.
$$\text{front} \rightarrow a \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}$$
becomes
$$\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}$$
while returning a.
### Example 1:
$$\text{front} \rightarrow b \leftarrow \text{back}$$
dequeue (b)
$$\text{front}, \text{back}$$
### Example 2:
$$\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}$$
dequeue
$$\text{front} \rightarrow c \rightarrow d \leftarrow \text{back}$$
Return b
### Pseudo code
```
dequeue(){
temp = front
val = front->data
front = front->next
delete temp
size = size-1
return val
}
```
## Traversing the List
Pseudo code:
```
displayList(){
Node * cur = front;
while(cur!=nullptr){
output cur->data
cur = cur->next
}
}
```
A diagram which shows the cursor going from c, to d to be (in order from front to back).
## Linked List Ends
Transcriber's note: this is the same as in 06. The left/right arrows denote a double linked list, even though the current set of slides is talking about only singly linked lists.
For [4, 5, 6]:
{% katex display %}
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
{% endkatex %}
versus
{% katex display %}
\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail}
{% endkatex %}
For [5]:
{% katex display %}
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
{% endkatex %}
versus
$$\text{head} \leftrightarrow 5 \leftrightarrow \text{tail}$$
For []:
$$\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail}$$
versus
$$\text{head} , \text{tail}$$
## List Class: Traversing the List
Pseudo code:
```
displayList(){
Node * cur = head->next
while(cur!=tail){
output cur->data
cur = cur->next
}
}
```
Graphic of `cur` moving through each (empty) element of the queue.
## End

File diff suppressed because one or more lines are too long

@ -1,823 +0,0 @@
---
layout: simple
math: true
---
# Rooted Trees -- CMPT 225
## Graphs
Graph is a pair G=&lt;V,E&gt;, with:
* V, a set called "vertacies" or "nodes"
* E, a set of pairs from V, i.e. {% katex %}E \subseteq V\times V{% endkatex %} called edges
* Example: {% katex %}G=\langle\{1,2,3\},\{(1,2),(1,3)\}\rangle{% endkatex %}
Example in table format:
node|connections
---|---
<span id="g1-1">1</span>|<a href="#g1-2">2</a>,<a href="#g1-3">3</a>
<span id="g1-2">2</span>|<a href="#g1-1">1</a>
<span id="g1-3">3</span>|<a href="#g1-1">1</a>
"G is directed": edges are ordered pairs (often called arcs)
{% katex display %}
\langle\{\{1,2,3\},\{(1,2),(1,3)\}\}\rangle
\neq
\langle\{\{1,2,3\},\{(2,1),(1,3)\}\}\rangle
{% endkatex %}
i.e. graph one:
node|connections
---|---
<span id="g2-1">1</span>|<a href="#g2-2">2</a>,<a href="#g2-3">3</a>
<span id="g2-2">2</span>|
<span id="g2-3">3</span>|
does not equal graph two:
node|connections
---|---
<span id="g3-1">1</span>|<a href="#g3-3">3</a>
<span id="g3-2">2</span>|<a href="#g3-1">1</a>
<span id="g3-3">3</span>|
"G is undirected" edges are sets
{% katex display %}
\langle\{\{1,2,3\},\{(2,1),(1,3)\}\}\rangle
=
\langle\{\{1,2,3\},\{(1,2),(3,1)\}\}\rangle
{% endkatex %}
i.e. graph one:
node|connections
---|---
<span id="g4-1">1</span>|<a href="#g4-3">3</a>,<a href="#g4-2">2</a>
<span id="g4-2">2</span>|<a href="#g4-1">1</a>
<span id="g4-3">3</span>|<a href="#g4-1">1</a>
is equal to graph two:
node|connections
---|---
<span id="g5-1">1</span>|<a href="#g5-2">2</a>,<a href="#g5-3">3</a>
<span id="g5-2">2</span>|<a href="#g5-1">1</a>
<span id="g5-3">3</span>|<a href="#g5-1">1</a>
By default, by "graph" we will mean "undirected graph".
## Path
Path of G of length n is a sequence $$\langle V_{0},V_{1},V_{2},\dots V_{n}\rangle$$ of vertacies s-t. $$(V_{0},V_{1}),(V_{1},V_{2}),(V_{2},V_{3})\dots$$ are edges of G.
s-t Path in G: path $$\langle s,\dots,t\rangle$$ in G.
Example of a s-t path of length 6 for G: $$\langle s,d,c,e,f,h,t\rangle$$
node|connections
---|---
<span id="g6-a">a</span>|<a href="#g6-b">b</a>,<a href="#g6-s">s</a>
<span id="g6-b">b</span>|<a href="#g6-a">a</a>,<a href="#g6-c">c</a>
<span id="g6-c">c</span>|<a href="#g6-b">b</a>,<a href="#g6-d">d</a>,<a href="#g6-e">e</a>,<a href="#g6-s">s</a>
<span id="g6-d">d</span>|<a href="#g6-c">c</a>,<a href="#g6-s">s</a>
<span id="g6-e">e</span>|<a href="#g6-c">c</a>,<a href="#g6-f">f</a>,<a href="#g6-g">g</a>
<span id="g6-f">f</span>|<a href="#g6-e">e</a>,<a href="#g6-h">h</a>
<span id="g6-g">g</span>|<a href="#g6-e">e</a>,<a href="#g6-h">h</a>,<a href="#g6-t">t</a>
<span id="g6-h">h</span>|<a href="#g6-f">f</a>,<a href="#g6-g">g</a>,<a href="#g6-t">t</a>
<span id="g6-s">s</span>|<a href="#g6-a">a</a>,<a href="#g6-c">c</a>,<a href="#g6-d">d</a>
<span id="g6-t">t</span>|<a href="#g6-g">g</a>,<a href="#g6-h">h</a>
Vertex t is *reachable* from s in G if ther is an s-t path in G.
G is *connected* if for every pair $$u,v\in V$$, u is reachable from v.
Transcriber's note: this is much easier to show with graphics and in fact very hard to show easily without graphics. I did my best, but it should be noted that this is *very* obvious with a visual graph.
It becomes two seperate graphs, in essense.
So for convenience, I will be splitting this up into two tables, even though technically it's one "graph" with two sets of connected nodes.
<p id="connected1">Connected:</p>
node|connections
---|---
<span id="g7-a">a</span>|<a href="#g7-b">b</a>,<a href="#g7-s">s</a>
<span id="g7-b">b</span>|<a href="#g7-a">a</a>,<a href="#g7-c">c</a>
<span id="g7-c">c</span>|<a href="#g7-b">b</a>,<a href="#g7-d">d</a>,<a href="#g7-e">e</a>,<a href="#g7-s">s</a>
<span id="g7-d">d</span>|<a href="#g7-c">c</a>,<a href="#g7-s">s</a>
<span id="g7-e">e</span>|<a href="#g7-c">c</a>,<a href="#g7-f">f</a>,<a href="#g7-g">g</a>
<span id="g7-f">f</span>|<a href="#g7-e">e</a>,<a href="#g7-h">h</a>
<span id="g7-g">g</span>|<a href="#g7-e">e</a>,<a href="#g7-h">h</a>,<a href="#g7-t">t</a>
<span id="g7-h">h</span>|<a href="#g7-f">f</a>,<a href="#g7-g">g</a>,<a href="#g7-t">t</a>
<span id="g7-s">s</span>|<a href="#g7-a">a</a>,<a href="#g7-c">c</a>,<a href="#g7-d">d</a>
<span id="g7-t">t</span>|<a href="#g7-g">g</a>,<a href="#g7-h">h</a>
Not connected:
node|connections
---|---
<span id="g9-a">a</span>|<a href="#g9-b">b</a>,<a href="#g9-s">s</a>
<span id="g9-b">b</span>|<a href="#g9-a">a</a>,<a href="#g9-c">c</a>
<span id="g9-c">c</span>|<a href="#g9-b">b</a>,<a href="#g9-d">d</a>,<a href="#g9-e">e</a>,<a href="#g9-s">s</a>
<span id="g9-d">d</span>|<a href="#g9-c">c</a>,<a href="#g9-s">s</a>
<span id="g9-e">e</span>|<a href="#g9-c">c</a>
<span id="g9-s">s</span>|<a href="#g9-a">a</a>,<a href="#g9-c">c</a>,<a href="#g9-d">d</a>
node|connections
---|---
<span id="g10-f">f</span>|<a href="#g10-h">h</a>
<span id="g10-g">g</span>|<a href="#g10-h">h</a>,<a href="#g10-t">t</a>
<span id="g10-h">h</span>|<a href="#g10-f">f</a>,<a href="#g10-g">g</a>,<a href="#g10-t">t</a>
<span id="g10-t">t</span>|<a href="#g10-g">g</a>,<a href="#g10-h">h</a>
## Cycles & Trees
Cycle in G: path $$\langle V_{0},\dots V_{n-1},V_{n}\rangle$$ in G where $$V_{0}=V_{n}$$
*Simple path*: all vertecies are distinct.
*Simple Cycle*: cycle $$\langle V_{0},\dots V_{n-1},V_{n}\rangle$$ where $$\langle V_{0},\dots,V_{n-1}$$ is a simple path.
Simple cylcle of length 5: (see ["connected" graph](#connected1) for confirmation): $$\langle s,d,c,b,a,s\rangle$$
Cycle of length 7 + repeats: $$\langle s,d,c,b,f,e,c,s\rangle$$
node|connections
---|---
<span id="g11-a">a</span>|<a href="#g11-b">b</a>,<a href="#g11-s">s</a>
<span id="g11-b">b</span>|<a href="#g11-a">a</a>,<a href="#g11-c">c</a>,<a href="#g11-f">f</a>
<span id="g11-c">c</span>|<a href="#g11-b">b</a>,<a href="#g11-d">d</a>,<a href="#g11-e">e</a>,<a href="#g11-f">f</a>,<a href="#g11-s">s</a>
<span id="g11-d">d</span>|<a href="#g11-c">c</a>,<a href="#g11-s">s</a>
<span id="g11-e">e</span>|<a href="#g11-c">c</a>,<a href="#g11-f">f</a>,<a href="#g11-g">g</a>
<span id="g11-f">f</span>|<a href="#g11-c">c</a>,<a href="#g11-e">e</a>,<a href="#g11-h">h</a>,<a href="#g11-b">b</a>
<span id="g11-g">g</span>|<a href="#g11-e">e</a>,<a href="#g11-h">h</a>,<a href="#g11-t">t</a>
<span id="g11-h">h</span>|<a href="#g11-f">f</a>,<a href="#g11-g">g</a>,<a href="#g11-t">t</a>
<span id="g11-s">s</span>|<a href="#g11-a">a</a>,<a href="#g11-c">c</a>,<a href="#g11-d">d</a>
<span id="g11-t">t</span>|<a href="#g11-g">g</a>,<a href="#g11-h">h</a>
## Tree
Tree: a connected, acyclic graph:
Example 1 (not a tree, due to cycle: $$\langle s,c,d,s\rangle$$):
node|connections
---|---
<span id="g12-a">a</span>|<a href="#g12-b">b</a>
<span id="g12-b">b</span>|<a href="#g12-a">a</a>,<a href="#g12-c">c</a>
<span id="g12-c">c</span>|<a href="#g12-b">b</a>,<a href="#g12-d">d</a>,<a href="#g12-e">e</a>,<a href="#g12-s">s</a>
<span id="g12-d">d</span>|<a href="#g12-c">c</a>,<a href="#g12-s">s</a>
<span id="g12-e">e</span>|<a href="#g12-c">c</a>,<a href="#g12-g">g</a>
<span id="g12-f">f</span>|<a href="#g12-h">h</a>
<span id="g12-g">g</span>|<a href="#g12-e">e</a>,<a href="#g12-h">h</a>,<a href="#g12-t">t</a>
<span id="g12-h">h</span>|<a href="#g12-f">f</a>,<a href="#g12-g">g</a>,<a href="#g12-t">t</a>
<span id="g12-s">s</span>|<a href="#g12-c">c</a>,<a href="#g12-d">d</a>
<span id="g12-t">t</span>|<a href="#g12-g">g</a>,<a href="#g12-h">h</a>
Example 2 (a tree, no cycle, all connected):
Now using ARIA trees:
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
s
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
a
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
b
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
d
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
c
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
e
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
f
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
h
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
g
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
i
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
Example 3 (not a tree) (transcriber's note: written as two trees for simplicity's sake)
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
s
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
a
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
b
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
d
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
c
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
e
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
f
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
h
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
g
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
i
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
Fact: every tree with $$n$$ verticies has $$n-1$$ edges.
## Rooted-tree:
Rooted tree: tree with a distringuished vertex called the root.
Unrooted tree:
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
a
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
b
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
c
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
d
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
e
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
f
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
g
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
h
</span>
</li>
</ul>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
i
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
j
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
k
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
l
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
Tree T with root:
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
a (root)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
b
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
c
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
d
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
e
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
f
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
g
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
h
</span>
</li>
</ul>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
i
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
j
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
k
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
l
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
An alternate drawing of T, with no semantic change.
Notice: in a tree there is a unique path between any two verticies.
So: a unique path from any vertex to the root.
Thus: the root indicates a *direction* on the edges e.g. towards the root.
A graph of the same tree, T, with arrows pointing towards the root.
There is no reason for having any semantic markup for this.
(sometimes "away from the root").
## Rooted Tree Terminology
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
z (root, anscestor of a)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
y
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
x (leaf)
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
w (leaf)
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
v (anscestor of a)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
u (leaf)
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
t (anscestor of a, parent of a)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
s (sibling of a, leaf)
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
r (sibling of a, leaf)
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
a (descendant of a, anscestor of a)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
q (child of a, desandant of a)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
o (decendant of a, leaf)
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
n (decendant of a, leaf)
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
m (decendant of a, leaf)
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
p (child of a, desendant of a, leaf)
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
* The *root* has no parent.
* *Leaves* have no children.
* Internal nodes are the non-leaves (sometimes root ??? [can't read] too)
## Depth & Height
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
4 (depth 0)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
1 (depth 1)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
0 (depth 2)
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
0 (depth 2)
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
3 (depth 1)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
2 (depth 2)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
1 (depth 3)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
0 (depth 4)
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
0 (depth 3)
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
0 (depth 2)
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
* Depth of node v = length of path from v to the root.
* Height of node v = length of longest path from to a decendant of v (e.g. to a leaf)
* Height of Tree T = height of its root = max height of any node in T = max depth of any node in T.
A rooted tree is:
* k-ary if *no node has* >k children.
* binary if *no node has* >2 children.
* ordered if the children of every node are ordered.
E.g. A ordered trenary tree:
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
no name
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
1
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
1
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
2
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
2
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
3
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
1
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
2
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
3
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
Notice: when we draw a tree, or represent it in a data structure, we order it.
In an ordered binary tree, every child of a node v is either the "left child of v" or the "right child of v".
Transcriber's note: nodes' children are transcribes from left to right. So for a binary tree, the first child is the "left child" and the second child is the "right child".
## Subtree rooted at v
Subtree rooted at v: tree with root v and containing all decendants of v.
In a binary tree:
* "left subtree of v" means the subtree rooted at the left child of v.
* sim. for "right child of v".
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
z
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
v (subtree rooted at v)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
y (subtree rooted at v, left subtree of v)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
v (subtree rooted at v, left subtree of v
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
u (subtree rooted at v, left subtree of v
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
x (subtree rooted at v, right subtree of v)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
w (subtree rooted at v, right subtree of v)
</span>
</li>
</ul>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
a
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
b
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
c
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
## End

File diff suppressed because one or more lines are too long

@ -1,398 +0,0 @@
---
layout: simple
math: true
---
# Recursion on Trees
CMPT 225
## Recursion
Recursion: A definition of a function is recursive if the body cointains an application of itself.
Consider: {% katex display %}
S(n)=\sum_{i=0}^{n} i
{% endkatex %}
or
{% katex display %}
S(n) = \begin{cases}
0 & \text{if } n=0\\
n+S(n-1) & \text{if } n>0
\end{cases}
{% endkatex %}
These two descriptions of $$S(n)$$ suggest two implementations:
e.g.:
```
S(n){
s=0
for i=1..n
s=s+i
return s
}
```
or
```
S(n){
if n=0
return 0
else
return n+S(n-1)
}
```
## Recursive Version
* -> S(4)
* -> S(3)
* -> S(2)
* -> S(1)
* -> S(0)
* <span id="inner0">returns 0</span>
* returns <span id="inner1">1+[0](#inner0)</span>
* returns <span id="inner2">2+[1](#inner1)=3</span>
* returns <span id="inner3">3+[3](#inner2)=6</span>
* returns 4+[6](#inner3)=10
Iterative version:
{% katex display %}
S = 0 + \sum_{i=1}^{n} i = 1 + \sum_{i=2}^{n} i = 3 + \sum_{i=3}^{n} i = \dots\\
= 0+1+2+3+4
{% endkatex %}
The *same computation*, but a different control strategy.
## Recursion & The Call Stack
Code:
```
S(n){
if(n=0){
r=0
}else{
r=n+S(n-1)
}
return r
}
```
Compute `p=S(2)`:
* Call S(2):
* n = 2
* r = ? ->
* Call S(1):
* n=1
* r=? ->
* Call S(0):
* N=0
* r=0
* <span id="innerstack1">return 0</span>
* <span id="innerstack2">return 1+[S(0)](#innerstack1)</span>
* return 2+[S(1)](#innerstack2)
The call stack at the end:
Name|Value|Removed
S|&lt;code of S; n=0, r=0&gt;|true
S|&lt;code of S; n=1, r=1&gt;|true
S|&lt;code of S; n=2, r=3&gt;|true
p|3|false
After the call the `S(2)` is complete, the entire call stack of S to S to S is gone.
There are 2 more slides containing *slightly* different versions of the same diagrams.
They do not provide any extra information.
## Recursion on Trees
* We will often use recursion & induction on trees.
* e.g. the tree rooted a v has some property if its subtrees have some related property
* e.g. the height of node v in a binary tree may be defined by:
{% katex display %}
h(v) = \begin{cases}
0 & \text{ if v is a leaf}\\
1 + \text{max}\{h(\text{left}(v)), h(\text{right}(v))\} & \text{ otherwise}
\end{cases}
{% endkatex %}
(We can define h(left(v)) to be -1 if left(v) does not exist, and sim. for right(v)).
## Recurssion a Trees Examples
height of node v in T:
{% katex display %}
h(v) = \begin{cases}
0 & \text{ if v is a leaf}\\
1+ max\{h(\text{left}(v)),h(\text{right}(v))\} & \text{ ow}
\end{cases}
{% endkatex %}
for the follwing tree: $$h(v)=?$$
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
v
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
a
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
e
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
g
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
h
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
f
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
i
</span>
</li>
</ul>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
b
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
c
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
d
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
## Recursion on Trees Example (pt 2)
(See math equation of previous slide)
h(v) = 3
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
v (3)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
a (2)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
e (1)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
g (0)
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
h (0)
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
f (1)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
i (0)
</span>
</li>
</ul>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
b (1)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
c (0)
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
d (0)
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
## Pseudo-code version:
```
height(v){
if v is a leaf
return 0
if v has one child u
return 1+height(u)
else
return 1+max(height(left(v)), height(right(v)))
}
```
## Traversals of Binary Tree
* A traversal of a graph is a process that "visits" each node in the graph once.
* We consider 4 standard tree traversalt:
1. level order
2. pre-order
3. in-order
4. post-order
* 2,3,4 begin at the root & recursively visit the nodes in each subtree & the root. They vary in the relative ???(can't read).
(Level order later)
Code:
```
pre-order-T(v){
visit v
pre-order-T(left(v))
pre-order-T(right(v))
}
```
pre-order-T(v) does nothing if v does not exist.
* v is visited before any of its decendants
* every node in the left subtree is visited before any node in the right subtree.
Tree to come back to:
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span>
A
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
B
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
D
</span>
</li>
<li role="treeitem" tabindex="-1">
<span>
E
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span>
C
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span>
F
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
Pre-order-traversal: A,B,D,E,C,F
## in-order-T
code:
```
in-order-T(v){
in-order-T(left(v))
visit v
in-order-T(right(v))
}
```
In order traversal: D,B,E,A,C,F
## post-order-T
code:
```
post-order-T(v){
post-order-T(left(v))
post-order-T(right(v))
visit v
}
```
Post order traversal: D,E,B,F,C,A
## End
...some repeated slides... unknown reason

File diff suppressed because one or more lines are too long

@ -1,122 +0,0 @@
---
layout: simple
math: true
---
Transciber's notes:
* $$\iff$$ is "if and only if"
* $$\implies$$ is "implies"
* $$\forall$$ is "for all"
* $$\exists$$ is "exists"
# Big-Oh--Part I
CMPT 225
## Slide 1
* Recall: if `f,g` are functions `f:N->N`,`g:N->N`
* f is `O(g)` means there are constants $$n_{0},c > 0$$
s.t. for every $$n>n_{0},f(n) \leq \text{c.g}(n)$$
* that is for all but finitely many "small" values of n: $$f(n) \leq cg(n)$$
* or f grows no faster than g (asymtotically)
* we typically write $$f(n) = O(g(n))$$
## Consider `f(n)`,`g(n)`:
Claim: $$f(n) = O(g(n))$$
Why:
* When $$f(n) > g(n)$$ for all n?
* But $$f(n) < 2g(n)$$ for almost all values of `n`.
* choose $$n_{0}$$ to "exclude" the real values of `n`.
* Now: $$f(n) < 2g(n)$$ for all $$n>n_{0}$$
* So: $$f(n) = O(g(n))$$
## Also:
(A graph a cannot transcribe because the notation doesn't make any sense. All future graphs have complexity on the y axis and the value of n [input to the function] as the x asxis.)
Claim: f(n) is **not** O(g(n))
However large we choose $$c,n_{0}$$, there will be some k (larger than $$n_0$$) s.d.: $$n > k \implies f(n) > c g(n)$$
## Consider `O(1)` (i.e. $$g(n)=1$$)
$$f(n) = O(1)$$ if $$\exists n_{0},c > 0$$ s.t. $$\forall n>n_{0} \space \space f(n) \leq c\times 1$$
(Another graph that makes no Goddamn sense whatsoever.)
* for every $$n>n_{0}, f(n)<c$$ <-- >-->
* so, f(n) grwos no faster than a constant
* so f(n) is asymptotically bounded by a constant
## The constant does not matter
Fact: $$f(n) = O(1) \iff f(n)=O(\text{10}^{27}) \iff f(n) = O(\frac{1}{\text{10}^{27}})$$
Suppose: $$f(n) = O(\text{10}^{27})$$ \*
Claim: $$f(n)$$ is also $$O(\frac{1}{\text{10}^{27}})$$
\* means $$\exists n_{0},c > 0$$ s.t. $$n > n_{0} \implies f(n) \leq c\times\text{10}^{27}$$
Want to show: $$\exists n_{0},c^{1}>0$$ s.t. $$n>n_{0} \implies f(n) \leq c^{1}\times \frac{1}{\text{10}^{27}}$$
Choose $$c^1$$ big enough that $$c^{1} \frac{1}{\text{10}^{27}} \leq c \times \text{10}^{27}$$
e.g: $$c^{1} = c \times \text{10}^{54}$$
Then:
{% katex display %}
\forall n>n_{0}, f(n) \leq c \times \text{10}^{27} \\
\leq c \times \text{10}^{-27} - \text{10}^{54}\\
\leq c^1 - \text{10}^{-27}
{% endkatex %}
(54-27=27)
So: $$f(n)=O(\text{10}^{-27})$$
## Asymtotic Notation (e.g. Big-Oh)
* Is *not* "about" algorithm
* *Is* a tool for describing (growth of) functions
* It is useful for describing functions related to algorithms + data structures, e.g.:
* minimum or maximum time taken
* minimum or maximum space needed
* etc.
* We use it so often for worst-case time for an algorithm that we often leave implciit a statement like "let T(n) be the max time taken by algorithm A as an input of size as most n." This statement is essential.
## Ex. *Complexity of Palindrome Checking*
* using a stack & queue
* Algorithm:
1. Insert all tokens into a stack & a queue
2. Repeat pop one token; dequeue one token. if different report 'no'.
* size of input = number of symbols or token
* each token is (all together O(1)):
* pushed on the stack, O(1)
* enqueued on the queue, O(1)
* popped off the stack, O(1)
* dequeued from the queue, O(1)
* compared to one other token, O(1)
* $$\text{n tokens} \implies n \times O(1)$$ time in total
* So: $$T(n) = n \times O(1) = O(n)$$
## What does $$n\times O(1) = O(n)$$ mean?
It means: $$f(n) = O(1) \iff n\times f(n) = O(n)$$
To see if it is true:
{% katex display %}
f(n) = O(1) \iff \exists c>0 \space \text{s.t.} \space f(n) < c, \text{for any} \space n \in \Z\\
\iff \exists c>0 \text{ s.t. } n\times f(n) < c\times n, \text{for any } n \in \N\\
\iff n\times f(n) = O(n)
{% endkatex %}
## End

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

@ -1,77 +0,0 @@
---
layout: simple
math: true
---
# Big-Omega & Big-Theta
## Big-Oh Gives Upper Bounds
For f,g functions $$\N \rightarrow \R^+$$,<br>
$$f(n) = \Omega(g(n))$$ means there are $$c,n_0 > 0$$ s.t.
$$\forall n>n_0\quad f(n) \geq c \times g(n)$$
i.e. f is asymptotically bounded from below by g
(A graph with two lines.
f is a blue line with a wobbly, but mostly linear movement upwards.
c times g is a red line which has a similar trajectory, but end up just slightly below the blue line.)
Note: We may have $$C << 1$$
or f grows asymptotically at least as fast as g.
## Big-Oh & Big-Omega are Duals
Fact: $$f(n) = \Omega(g(n)) \leftrightarrow g(n) = O(f(n))$$
Pf: $$f(n) = \Omega(g(n))$$:
* $$\iff \exists n_0, c' > 0 \text{ s.t. } n>n_0 \implies f(n) \geq c' \cdot g(n)$$
* $$\iff \exists n_0,c' > 0 \text{ s.t. } n>n_0 \implies c'\cdot g(n) \leq f(n)$$
* $$\iff\exists n_0,c > 0 \text{ s.t. } n>n_0 \implies g(n) \leq c\cdot f(n)$$ // letting $$c = \frac{1}{c'}$$
* $$\iff g(n) O(f(n))$$
So: f grows at least as fast as g $$\iff$$ g grows at most as fast as f.
## Examples: Worse-case times
Operation|$$O(1)$$|$$\Omega(1)$$|$$O(\log n)$$ (highlighted)|$$\Omega(\log n)$$ (highlighted)|$$O(n)$$|$$\Omega(n)$$|$$O(n \log n)$$|$$\Omega(n \log n)$$
---|---|---|---|---|---|---|---|---
stack push/pop (highlighted)|&#x2713; (green)|&#x2713; (green)|&#x2713;|&#x274c; (blue)|&#x2713;|&#x274c; (blue)|&#x2713;|&#x274c; (blue)
unqueue/dequeue|&#x2713; (green)|&#x2713; (green)|&#x2713;|&#x274c; (blue)|&#x2713;|&#x274c; (blue)|&#x2713;|&#x274c; (blue)
heap insert or extract min|&#x274c; (pink)|&#x2713;|&#x2713; (green)|&#x2713; (green)|&#x2713;|&#x274c; (blue)|&#x2713;|&#x274c; (blue)
AVL-tree find, insert, remove|&#x274c; (pink)|&#x2713;|&#x2713; (green)|&#x2713; (green)|&#x2713;|&#x274c; (blue)|&#x2713;|&#x274c; (blue)
make_heap|&#x274c; (pink)|&#x2713;|&#x274c; (pink)|&#x2713;|&#x2713; (green)|&#x2713; (green)|&#x2713;|&#x274c; (blue)
BST find, insert, remove|&#x274c; (pink)|&#x2713;|&#x274c; (pink)|&#x2713;|&#x2713; (green)|&#x2713; (green)|&#x2713;|&#x274c; (blue)
Sorting|&#x274c; (pink)|&#x2713;|&#x274c; (pink)|&#x2713;|&#x274c; (pink)|&#x2713; (green)|&#x2713; (green)|? (red)
## Big-Theta Expresses "Tight Bounds"
For f,g functions $$\N \rightarrow \R^+$$,
$$f(n) = \Theta(g(n))$$ means there are $$c_1, c_2,n_0 > 0 \text{ s.t. } n>n_0 \implies c_1\cdot g(n) \leq f(n) \leq c_2\cdot g(n)$$
i.e. f asymptotically bounded from above and below by g
(Diagram with three lines, wobbly but roughly linear, with them stacked in the following order from top to bottom:
* $$c_2 \cdot g$$
* f
* $$c_1 \cdot g$$)
or f grows asymptotically the same as g.
## "Grows the same as" is systemetic
Fact: $$f(n) = \Theta(g(n)) \iff g(n) = \Theta(f(n))$$
i.e. f grows the same as g $$\iff$$ g grows the same as f.
P.f.: $$f(n) = \Theta(g(n))
* $$\iff \exists \text{c1},\text{c2},n_0 > 0 \text{ s.t. } \forall n>n_0 \quad c1\cdot g(n) \leq f(n) \leq c2\cdot g(n)$$
* $$\iff \exists c_1,c2,n_0 > 0 \text{ s.t. } \forall n>n_0 \quad \frac{1}{c2} f(n) \leq g(n) \leq \frac{1}{c1} f(n)$$
* $$\iff \exists c_3,c_4,n_0 > 0 \text{ s.t. } \forall n>n_0 c_3 f(n) \leq g(n) \leq c_4 f(n)$$
* $$\iff g(n) = \Theta(f(n))$$
## End

File diff suppressed because one or more lines are too long

@ -1,537 +0,0 @@
---
layout: simple
math: true
---
# Sorting
## Sorting
* re-arranging elements of a sequence $$S \text{ s.t. } S_0 \leq S_1 \leq S_2 \leq \cdots \leq S_{n-1}$$
* We will look at 5 sorting algorithms:
* 3 iterative
* 2 recursive
## The iterative algorithms
* maintain a partition: "unsorted part" & "srtoed part"
* sort a sequence of n elements in n-1 stages
* at each stage, move 1 element from the unsorted part to the sorted part:
* (Diagram of a generic array with unseen "sorted" items on the left and "unsorted" elements on the right. Caption: "1 stage moves 1 element")
```
sort(A){
* initialize
* repeat n-1 times
* move 1 element from unsorted and sorted part
}
```
* The algorithms differ in how they:
* select an element to remove from the unsorted part
* insert it into the sorted part
## Insertion Sort
* Initially sorted part is just A[0]
* Repeat n-1 times
* remove the first element from the unsorted part
* insert it into the sorted part (shifting elements to the right as needed)
Diagram of array as it gets sorted in three stages:
* Stage 1: sorted is leftmost (0th) element; n-1 elements are unsorted on the right.
* Stage 2: approximately half of the array is sorted; an arrow points from the leftmost value inside the unsorted side to an arbitrary position inside the sorted side.
* Stage 3: just over half of the array is sorted now.
Code:
```
insertion sort(A){
for(i=1 to n-1){
pivot = A[i] // first element in unsorted part
j=i-1
// The following loop shifts all elements in sorted parts that are larger than pivot 1 "to the right"
while(j>=0 AND A[i] > pivot){
A[j+i] = A[j] // shift jth
j = j-1
}
A[j+i] = pivot // move pivot into position.
}
}
```
## Insertion Sort Example
Stages:
* Stage 0: Original
* 5|4|2|6|1|3
* Stage 1: (label: 4)
* 4|5|2|6|1|3
* Stage 2: (label: 2)
* 2|4|5|6|1|3
* Stage 3: (label: 6)
* 2|4|5|6|1|3
* Stage 4: (label: 1)
* 1|2|4|5|6|3
* Stage 5: (label: 3)
* 1|2|3|4|5|6
## Selection Sort
* initially sorted part is empty
* repeat n-1 times
* find the smallest element in the unsorted part
* make it the first position which becomes the now last position of sorted part.
Diagram of parts:
* Initially, the entire array is all unsorted.
* Over time the sorted elements stack up on the left.
* Every time an element is moved, it is moved from the unsorted part (lowest element) and swapped with the element just after the end of the sorted part, making the sorted part one element bigger.
* Eventually all elements are sorted in descending order.
Code:
```
selection_sort(A){
for(i=1 to n-1){
// find min element of unsorted
j=i-1 // j is index of min found so far.
k=i
while(k<n){
if(A[k]<A[j]) j=k;
k=k+1
}
swap A[i-1] and A[j]
}
}
```
Process of Selection Sort:
* Original: all unsorted
* 5|4|2|6|1|3
* Stage 1: [0] is sorted; 1 and 5 swap
* 1|4|2|6|5|3
* Stage 2: [0..1] is sorted; 2 and 4 swap
* 1|2|4|6|5|3
* Stage 3: [0..2] is sorted; 3 and 4 swap
* 1|2|3|6|5|4
* Stage 4: [0..3] is sorted; 4 and 6 swap
* 1|2|3|4|5|6
* Stage 5: [0..4] is sorted; annotation: s.t. s (final stage)
* 1|2|3|4|5|6
## Heapsort (Selection Sort is crossed out)
* Initially sorted part empty
* (highlighted) make unsorted part into a heap
* repeat n-1 times
* find the smallest element in the unsorted part (Note: heap extract takes log(n) time vs. &Theta;(n) for the scan in selection sort)
* move it to the first position which becomes the new last position of the started part.
Consider the organization of array contents:
1. (Diagram of array with sorted half on the right and the unsorted half on the left.) A purple arrow points to the leftmost element in the unsorted portion. The note reads: "if this is the root of the heap, then it is also the smallest element in the unsorted part, so is in its correct final position.
To use this arrangement, the root of the heap keeps moving, so we have lots of shifting to do."
2. (A diagram showing the same array with sorted and unsorted halves.) A purple arrow points to the last element in the array; it points to a purple circle. A purple square is at the leftmost element of the unsorted half (the one discussed in the last item). The note reads: "If this is the root of the, then everything works:
* We extract the final element (purple circle); move the last leaf (purple square) to the root + do a percolate-down;
store the final element (purple circle) where the last element of the unsorted list (purple square) *was*,
which is now free, and is the correct final location for the previously final element (purple circle); after which we have:
* (Diagram of array with the "sorted" half extended one cell over to encompass the purple circle)
* **But**: we must re-code our heap implementation s.t. the root is at A[n-1], with the result that the indexing is now less intuitive.
3. Instead, we use a max-heap, and this arrangement:
* (Diagram showcasing, as previously, a sorted half to the right and an unsorted half on the left. An orange circle labeled "root of heap" is the very first element of the list and the unsorted half; an orange square labeled "last leaf" sits at the end (rightmost side) of the unsorted half.)
* The heap root is at A[0]
* Heap Extraction remove the root of the heap (orange circle), moves the last leaf (orange square) to A[0],
freeing up the spot where the root of the heap (orange circle) belongs.
* This leaves us with: (Diagram of the orange circle near the middle of the array, at the leftmost portion of the sorted half. The orange square is in the center of the unsorted half.)
* Re-coding a min heap into a max heap is just replacing < with > and vice versa.
## Heapsort (Selectioon Sort is crossed out)
* initially sorted part empty
* (highlighted) make unsorted part into a max heap
* repeat n-1 times:
* find the largest (smallest is crossed out) element in the unsorted part
* move it to the last (first is crossed out) position which becomes the new first (last is crossed out) position of the sorted part.
Code:
```
heapsort(A){
buildMaxHeap(A)
for(i=1 to n-1){
A[n-1] extractMax()
}
}
```
Stages of sorting:
* (Diagram of unsorted array with first element labeled as "heap with max here".)
* (Diagram of a half-sorted array showing the swap between the first and last elements of the unsorted portion of the array. Labeled as "take max element from root..." and "take last leaf from end of heap" with arrows pointing to one another.)
* (Diagram of a half+1 sorted array, displaying the new sorted element that has been swapped from the root element of the heap. Labeled as "newest element of sorted part" and "this is the *final* location" (the new element just swapped), and "y new root of heap (which then gets percolated down)" (what is now the first element of the array, which was also just swapped).)
Unsorted heap of size 1 has smallest element.
## Heapsort with in-line percolate-down
Code:
```
heapsort(A){
makeMaxHeap(A)
for(i=1 to n-1){
swap A[0] and A[n-1] // move last leaf to root and old root to where last leaf was
size <- n-i+1 // size of heap = size of unsorted part
// start of percolate down
j <- 0
while(2j+1 < size){
child <- 2j+1
if(2j+2 < size AND A[2j+2] < A[2j+1]){
child <- 2j+2
}
if(A[child]<A[j]){
swap A[child] and A[j]
j <- child
} else {
j <- size // termite the while
}
} // end of percolate down
}
}
```
## Heapsort Example
* Original:
* 5|4|2|6|1|3
* Turn into heap:
* 6|5|3|4|1|2
* Swap root (6) and last unsorted element (2):
* 2|5|3|4|1|6
* Re-heap the unsorted portion: [0..4]
* 5|4|3|2|1|6
* Swap root (5) and the last unsorted element (2):
* 1|4|3|2|5|6
* Re-heap the unsorted portion: [0..3]
* 4|2|3|1|5|6
* Swap root (4) and the last unsorted element (1):
* 1|2|3|4|5|6
* Re-heap unsorted portion: [0..2]
* 3|2|1|4|5|6
* Swap root (3) and last unsorted element (1):
* 1|2|3|4|5|6
* Re-heap unsorted portion: [0..1]
* 2|1|3|4|5|6
* Swap root (2) and last unsorted element (1):
* 1|2|3|4|5|6
* Array is sorted because unsorted portion is only 1 element.
Tree version of above (heap):
Original:
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span id="t11_5">
5
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t11_4">
4
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t11_6">
6
</span>
</li>
<li role="treeitem" tabindex="-1">
<span id="t11_1">
1
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span id="t11_2">
2
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t11_3">
3 (left)
</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
After re-heap and one removal:
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span id="t0_2">
2
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t0_5">
5
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t0_2">
2
</span>
</li>
<li role="treeitem" tabindex="-1">
<span id="t0_1">
1
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span id="t0_3">
3
</span>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
After a second re-heap and removal:
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span id="t0_1">
1
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t0_4">
4
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t0_2">
2 (left)
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span id="t0_3">
3
</span>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
After a third:
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span id="t0_1">
1
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t0_2">
2
</span>
</li>
<li role="treeitem" tabindex="-1">
<span id="t0_3">
3
</span>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
Examples stop here.
## Heapsort Example (2)
(Repeat same as above, except with different trees.)
Trees (Transcriber's note: these trees don't seem relavant to me.... but maybe I'm wrong):
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span id="t0_2">
2 (crossed out with orange 5)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t0_5">
5 (crossed out next to orange 2 which is also crossed out; an orange 4 is not crossed out)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t0_4">
4 (crossed out with orange 2)
</span>
</li>
<li role="treeitem" tabindex="-1">
<span id="t0_1">
1
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span id="t0_3">
3
</span>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span id="t0_1">
1 (crossed out with an orange 4)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t0_4">
4 (crossed out with orange 1, which is also crossed out; an orange 2, not crossed out is next to it)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t0_2">
2 (left; crossed out with orange 1)
</span>
</li>
</ul>
</li>
<li role="treeitem" tabindex="-1">
<span id="t0_3">
3
</span>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
<!-- AUTO GENERATED FROM CUSTOM PYTHON CODE -->
<ul role="tree">
<li role="treeitem" tabindex="-1">
<span id="t0_1">
1 (orange 2)
</span>
<ul role="group">
<li role="treeitem" tabindex="-1">
<span id="t0_2">
2 (left; orange 1)
</span>
</li>
</ul>
</li>
</ul>
<!-- END OF GENERATED CODE -->
## Time Complexity of Iterative Sorting Algorithms
* each algorithm does exactly n-1 stages
* the work done at the i<sup>th</sup> stage varies with the algorithm (& input).
* we take # from item comparisons as a measure of work/time\*.
<dl>
<dt>Selection Sort</dt>
<dd>exactly n-i comparisons to find num element in unsorted part</dd>
<dt>Insertion Sort</dt>
<dd>between 1 and i comparisons to find location for pivot</dd>
<dt>HeapSort</dt>
<dd>between 1 and {% katex %}2\log_{2} (n-i-1){% endkatex %} comparisons for percolate-down</dd>
</dl>
## \* Number of comparisons
* We must verify # comparisons (or some constant times # comparisons) is an upper bound on work done by each algorithm.
* # of assignments (& swaps) also matters in actual run time.
## Selection Sort
On input of size n, # of comparisons is always (regardless of input):
{% katex display %}
\begin{aligned}
\sum_{i=1}^{n-1} (n-i) & = \sum_{i=1}^{n-1} i\\
& = S(n-i)\\
& = \frac{(n-1)(n)}{2}\\
& = \frac{n^2 -n}{2}\\
& = \Theta(n^2)
\end{aligned}
{% endkatex %}
## Insertion Sort -- Worst Case
Upper Bound: {% katex display %}\text{\# comparisons} \leq \sum_{i=1}^{n-1} i = \frac{n^{2} -n}{2} = O(n^{2}){% endkatex %}
Lower Bound:
* Worst case initial sequence is in reverse order. e.g.:
* n|n-1|n-2|...|1
* In the i<sup>th</sup> stage we have:
* n-i+1|n-1+2|...|n|n-1|n-1-1|...|2|1
* n-i|n-i+1|...|n-1|n|n-i-1|...|2|1
* This takes i comparisons, because the sorted part is of size i.
* So, {% katex display %}\text{\# comparisons} \leq \sum{i=1}^{n-1} = \Omega(n^{2}){% endkatex %}
So, insertion sort worst case is $$\Theta(n^{2})$$
(Transcriber's note: I'm fairly certain you can only use big-O notation when talking about worst case scenario, not Theta. But I'm leaving it as written.)
## Insertion Sort Best Case
Best case: initial sequence is fully ordered.
Then: In each stage, exactly 1 comparison is made.
So, {% katex %}\text{\# comparisons} = n-1 = \Theta(n){% endkatex %}.
## Heapsort Worst Case
Upper bound:
{% katex display %}
\begin{aligned}
\text{\# comparisons} & \leq \sum_{i=1}^{n-1} 2\log_{2} (n-i+1)\\
& = 2\sum_{i=1}^{n-1} \log_{2} (i+1)\\
& = \leq 2\sum_{i=1}^{n-1} \log_{2} n\\
& = \leq 2n\log_{2} n\\
& = O(n \log n)
\end{aligned}
{% endkatex %}
Lower Bound? (empty space)
Base Case? (What input would lead to **no** movement during percolate-down?
What if we exclude this case?)
## Recursive Divide & Conquer Sorting
TODO

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1,8 +0,0 @@
---
layout: simple
math: true
---
{% katex display %}
0011_{2} \And 1110_{2} = 0010_{2}
{% endkatex %}

@ -1,22 +0,0 @@
Title: Activity - Code
Video of CERT Secure Coding Initiative Conference 2015 - Robert C. Seacord
https://www.youtube.com/watch?v=1ew0GvB3NpE
Blue box with code in it:
char *copy (size_t n, const char *a) {
if (n == 0) return NULL;
if (a == NULL) return NULL;
char *p = (char *)malloc(n);
if (p == NULL) return NULL;
for (int i = 0; i < n; i++) p[i] = *a++;
return p;
}
White text over-laying the blue box: Spot the defect

@ -1,105 +0,0 @@
<aside>Hello everyone!</aside>
# Welcome to CMPT 295
## Introduction to Computer Systems
My name is Anne Lavergne
Lecture 1 Course Overview + Activity
<aside>How does it feel to be back on campus?</aside>
## Todays Menu [1]
* COVID Protocol
* What is CMPT 295?
* What shall we learn in CMPT 295?
* What should we already know?
* Which resources do we have to help us learn all this?
* Activity
* Questions
## COVID protocol About masks! [2]
Here is a message from Elizabeth Elle, SFU Vice Provost Learning & Teaching, based on the public health order:
* Unless we have an approved exemption, we are required to wear a mask in all indoor common and learning spaces, including classrooms. Please come to campus prepared with a non-medical mask.
* If we forget our mask, disposable masks are available from Student Central in Burnaby and at the information desks in Vancouver and Surrey.
* If we require a mask exemption in the classroom for medical reasons, please contact the Centre for Accessible Learning at cal_admin@sfu.ca for assistance.
* If we are requesting mask exemptions on other protected grounds, such as religion, we can contact the Office of Student Support, Rights and Responsibilities at student_support@sfu.ca.
* And please remember to be kind to each other. If we see someone not wearing a mask, do not make assumptions or judgments as that person may be exempt.
## What is CMPT 295? [3]
* The goal of this course is to give us, software developers, a look “under the hood” of a computer, i.e., to learn about Computer Systems => microprocessor, memory, … <img src="" alt="a car with its hood up.">
* This knowledge will allow us to become more efficient software developers
## The big picture: [4]
In CMPT 295, we shall learn …
* C programs (.c) -- How our code and data are represented in memory
* Assembly programs (.s) -- How a compiler transforms our code into machine executable code in several steps
* Object (.o) file an executable -- How a compiler optimizes (or not) our code
* Computer executes it -- How a microprocessor is designed and how it executes our code
* CPU, Memory -- How memory is designed
How all of this can impact the execution of our code How to write more efficient and reliable code:
* Be able to find and eliminate bugs
more efficiently
* Be able to ascertain program performance and tune it by optimizing our code
## What should we already know? [5]
* Write correct C programs
* C constructs (variables, data types, pointers, if/else, switch/case, for/while/do while, function calls, arrays, …)
* What a stack is and how it works
* Binary/decimal/hexadecimal numeral systems
* How to convert from one numeral system to the others
* Basic arithmetic
* Perform Boolean algebra using and, or, not, xor
## Which resources do we have? [6]
* Course web site
https://www2.cs.sfu.ca/CourseCentral/295/alavergn/index.html
* Textbook
* Computer Systems: A Programmer's Perspective, 3/E, Randal E. Bryant, David R. O'Hallaron, Pearson, 2016
* Labs in CSIL (Computing Science Instructional Lab)
* Target Machine: CSIL workstation
* Linux platform (or OS)
* C programming language
* x86-64 assembly language
* gcc compiler
* Instructor and TAs - Office hours
## Activity - Discover our resources [7]
Instructions:
1. Form teams of 3 to 4
2. Do Lecture 1 Activity on CourSys
3. Time: about 30 minutes
## Question? [8]
Blank page.
## Summary [9]
* COVID Protocol
* What is CMPT 295?
* What shall we learn in CMPT 295?
* What should we already know?
* Which resources do we have to help us learn all this?
* Activity
* Questions
## Next Lecture [10]
* Data Representation
* Representing information as bits
* To get ready for our next lecture:
* Optional: Read Chapter 1 of textbook
* Not so optional: Read Section 2.1 of Chapter 2
* Download the partial lecture notes found under the column Lecture in the table on our course web site

@ -1,170 +0,0 @@
<p><aside>Hello everyone!</aside></p>
<h1>Welcome to CMPT 295</h1>
<h2>Introduction to Computer Systems</h2>
<p>My name is Anne Lavergne</p>
<p>Lecture 1 Course Overview + Activity</p>
<p><aside>How does it feel to be back on campus?</aside></p>
<h2>Todays Menu [1]</h2>
<ul>
<li>COVID Protocol</li>
<li>What is CMPT 295?
<ul>
<li>What shall we learn in CMPT 295?</li>
<li>What should we already know?</li>
<li>Which resources do we have to help us learn all this?</li>
</ul>
</li>
<li>Activity</li>
<li>Questions</li>
</ul>
<h2>COVID protocol About masks! [2]</h2>
<p>Here is a message from Elizabeth Elle, SFU Vice Provost Learning &amp; Teaching, based on the public health order:</p>
<ul>
<li>Unless we have an approved exemption, we are required to wear a mask in all indoor common and learning spaces, including classrooms. Please come to campus prepared with a non-medical mask.
<ul>
<li>If we forget our mask, disposable masks are available from Student Central in Burnaby and at the information desks in Vancouver and Surrey.</li>
<li>If we require a mask exemption in the classroom for medical reasons, please contact the Centre for Accessible Learning at cal_admin@sfu.ca for assistance.</li>
<li>If we are requesting mask exemptions on other protected grounds, such as religion, we can contact the Office of Student Support, Rights and Responsibilities at student_support@sfu.ca.</li>
</ul>
</li>
<li>And please remember to be kind to each other. If we see someone not wearing a mask, do not make assumptions or judgments as that person may be exempt.</li>
</ul>
<h2>What is CMPT 295? [3]</h2>
<ul>
<li>The goal of this course is to give us, software developers, a look “under the hood” of a computer, i.e., to learn about Computer Systems => microprocessor, memory, … <img src="" alt="a car with its hood up."></li>
<li>This knowledge will allow us to become more efficient software developers</li>
</ul>
<h2>The big picture: [4]</h2>
<p>In CMPT 295, we shall learn …</p>
<p>C programs (.c) &ndash; How our code and data are represented in memory</p>
<p>Assembly programs (.s) &ndash; How a compiler transforms our code into machine executable code in several steps</p>
<p>Object (.o) file an executable &ndash; How a compiler optimizes (or not) our code</p>
<p>Computer executes it &ndash; How a microprocessor is designed and how it executes our code</p>
<p>CPU, Memory &ndash; How memory is designed</p>
<p>How all of this can impact the execution of our code How to write more efficient and reliable code:
* Be able to find and eliminate bugs
more efficiently
* Be able to ascertain program performance and tune it by optimizing our code</p>
<h2>What should we already know? [5]</h2>
<ul>
<li>Write correct C programs
<ul>
<li>C constructs (variables, data types, pointers, if/else, switch/case, for/while/do while, function calls, arrays, …)</li>
</ul>
</li>
<li>What a stack is and how it works</li>
<li>Binary/decimal/hexadecimal numeral systems
<ul>
<li>How to convert from one numeral system to the others</li>
<li>Basic arithmetic</li>
</ul>
</li>
<li>Perform Boolean algebra using and, or, not, xor</li>
</ul>
<h2>Which resources do we have? [6]</h2>
<ul>
<li>Course web site
https://www2.cs.sfu.ca/CourseCentral/295/alavergn/index.html</li>
<li>Textbook
<ul>
<li>Computer Systems: A Programmer&rsquo;s Perspective, 3/E, Randal E. Bryant, David R. O'Hallaron, Pearson, 2016</li>
</ul>
</li>
<li>Labs in CSIL (Computing Science Instructional Lab)
<ul>
<li>Target Machine: CSIL workstation
<ul>
<li>Linux platform (or OS)</li>
<li>C programming language</li>
<li>x86-64 assembly language</li>
<li>gcc compiler</li>
</ul>
</li>
</ul>
</li>
<li>Instructor and TAs - Office hours</li>
</ul>
<h2>Activity - Discover our resources [7]</h2>
<p>Instructions:
1. Form teams of 3 to 4
2. Do Lecture 1 Activity on CourSys
3. Time: about 30 minutes</p>
<h2>Question? [8]</h2>
<p>Blank page.</p>
<h2>Summary [9]</h2>
<ul>
<li>COVID Protocol</li>
<li>What is CMPT 295?
<ul>
<li>What shall we learn in CMPT 295?</li>
<li>What should we already know?</li>
<li>Which resources do we have to help us learn all this?</li>
</ul>
</li>
<li>Activity</li>
<li>Questions</li>
</ul>
<h2>Next Lecture [10]</h2>
<ul>
<li><p>Data Representation</p>
<ul>
<li>Representing information as bits</li>
</ul>
</li>
<li><p>To get ready for our next lecture:</p>
<ul>
<li>Optional: Read Chapter 1 of textbook</li>
<li>Not so optional: Read Section 2.1 of Chapter 2</li>
<li>Download the partial lecture notes found under the column Lecture in the table on our course web site</li>
</ul>
</li>
</ul>

@ -1,523 +0,0 @@
---
layout: simple
---
# CMPT 295
Unit: Textbook Chapter 2 - Data Representation
Lecture 2 - Representing data in memory
Data = information = data, instructions (code, programs)
## CAL Volunteer Note-Taker Position [1]
* If you are taking lecture notes in CMPT 295 and your hand writing is you may be interested in applying for the following volunteer note-taker position:
* The Centre for Accessible Learning (CAL) is looking for a CMPT 295 notetaker
* CAL volunteer lecture note-takers are provided with a $100 credit applied to their student account in acknowledgment of their assistance
* Interested?
* Please see the email CAL has sent us
* Please feel free to call 778-782-3112 or email calexams@sfu.ca the Centre if
you have any questions
## Last Lecture [2]
* COVID Protocol
* What is CMPT 295?
* What shall we learn in CMPT 295?
* What should we already know?
* Which resources do we have to help us learn all this?
* Activity
* Questions
## Feedback on Lecture 1 Activity [3]
* Thank you for participating in the Lecture 1 Activity!
* Feedback now posted on our course web site
* Check it out!
## Unit Objectives [4]
Chapter 2 of our textbook
* Understand how a computer represents (encodes) data in (fixed-size) memory
* Become aware of the impact this fixed size has on …
* Range of values represented in memory
* Results of arithmetic operations
* Become aware of ...
* How one data type is converted to another
* And the impact this conversion has on the values
* Bottom Line: allow software developers to write more reliable code
## Todays Menu [5]
* Representing data in memory Most of this is review
* “Under the Hood” - Von Neumann architecture
* Bits and bytes in memory
* How to diagram memory -> Used in this course and other references
* How to represent series of bits -> In binary, in hexadecimal (conversion)
* What kind of information (data) do series of bits represent -> Encoding scheme
* Order of bytes in memory -> Endian
* Bit manipulation bitwise operations
* Boolean algebra + Shifting
* Representing integral numbers in memory
* Unsigned and signed
* Converting, expanding and truncating
* Arithmetic operations
* Representing real numbers in memory
6
* IEEE floating point representation
* Floating point in C casting, rounding, addition, …
## “Under the hood” Von Neumann architecture [6]
Architecture of most computers
Its features:
* CPU, memory,
input and ouput, bus
* (circled in red) Data and instructions
(code/programs) both stored in memory
* C programs (.c) -- How our code and data are represented in memory
* Assembly programs (.s) -- How a compiler transforms our code into machine executable code in several steps
* Object (.o) file an executable -- How a compiler optimizes (or not) our code
* *Computer executes it -- How a microprocessor is designed and how it executes our code*
* CPU, Memory -- How memory is designed
"Computer executes it" has a diagram attached.
Two I/O devices are shown (represented by a cylindar), one on the left, one one the right.
On the left is "input peripheral", on the right is "output peripheral".
A red box surrounds the two central nodes (represented by squares), labeled "motherboard".
It contains two items inside it.
On the left is the "Central Processing Unit (CPU)", which executes instructions.
On the right is "Memory", which is "volatile" and stores data and instructions.
An arrow points from the far left input peripheral to the CPU.
A bi-directional arrow points between the CPU and memory.
A final arrow points from memory to the output peripheral.
## How to diagram memory [7]
* Seen as a linear (contiguous) array of bytes
* 1 byte (8 bits) smallest addressable unit of memory
* Each byte has a unique address
* Byte-addressable memory
* Computer reads a word worth of bits at a time (=> word size)
* Questions:
1. If word size is 8, how many bytes are read at a time from memory?
Answer: (annotated) 1 byte
2. If a computer can read 4 bytes at a time, its
word size is (annotated) 32 bits.
Address|M[]
size1|
...|
0x0008|
0x0007|
0x0006|
0x0005|
0x0004|
0x0003|
0x0002|
0x0001|
0x0000|1 byte
## Closer look at memory [8]
Typically, in a diagram, we represent memory (memory content) as a series of memory “cells” (or bits) in which one of two possible values (0 and 1) is stored
Address|M[]
size1|
...|
0x0008|
0x0007|
0x0006|
0x0005|
0x0004|
0x0003|
0x0002|
0x0001|
0x0000|01000000
`0x0000` is labled as "1 memory cell"
## Compressed view of memory [9]
Address|M[]
size1|
...|
0x0008|
0x0007|7
0x0006|6
0x0005|5
0x0004|4
0x0003|3
0x0002|2
0x0001|1
0x0000|0
Address|M[]
size8||||||||
...||||||||
0x0018||||||||
0x0010 (annotation: why is this not `0x0016)||||||||
0x0008||||||||
0x0000|0|1|2|3|4|5|6|7
Each cell, no matter which diagram is used, represents 1 byte, or 8 bits.
`0x0000` to `0x0008` is eight bytes, or 64 bits.
## Why can only two possible values be stored in a memory “cell”?
* As electronic machines, computers use two voltage levels
* Transmitted on noisy wires -> value of two voltage levels vary over a range
* These ranges are abstracted using “0” and “1”
A diagram shows two horizontal green bars.
Bar one has a minimum of 0.0V, and a maximum of 0.2V. "0"
Bar two has a minimum of 0.9V, and a maximum of 1.1V. "1"
A wiggly line jiggles around somewhat randomly within the confines of the lower "0" bar--moving to the right with time, it moves up, temporarily traversing the whitespace between the two bars before settling back down in its jiggly pattern within the "1" bar.
It then reverses this step, through the whitespace back to the "0" bar where it started.
* Back to the question Why can only two possible values be stored in a
memory “cell”?
* Because computers manipulate two-valued information
## A bit of history [11]
ENIAC: Electronic Numerical Integrator And Calculator
* U. Penn by Eckert + Mauchly (1946)
* Data: 20 × 10-digit regs + ~18,000 vacuum tubes
* To code: manually set switches and plugged cables
* Debugging was manual
* No method to save program for later use
* Separated code from the data
Source: [https://en.wikipedia.org/wiki/ENIAC#/media/File:ENIAC_Penn1.jpg](https://en.wikipedia.org/wiki/ENIAC#/media/File:ENIAC_Penn1.jpg)
## Review [12] -- Back to our bits -- How to represent series of bits
* From binary numeral system
* Base: 2
* Bit values: 0 and 1
* Possible bit patterns in a byte: 000000002 to 111111112
* Drawback of manipulating binary numbers?
* What number is this?
* 1001100 11001001 01000101 01001000<sub>2</sub>
* Lengthy to write -> not very compact
* Difficult to read
Annotation: "Error prone!" encompasses "lengthy to write" and "difficult to read".
## Review -- A solution: hexadecimal numbers
Decimal|Binary|Hexidecimal
0|0000|0
1|0001|1
2|0010|2
3|0011 (circled in annotation)|3
4|0100|4
5|0101 (circled in annotation)|5
6|0110|6
7|0111|7
8|1000|8
9|1001|9
10|1010|A
11|1011|B
12|1100|C
13|1101 (circled in annotation)|D
14|1110|E
15|1111 (circled in annotation)|F
* Base: 16
* Values: 0, 1, 2, …, 9, A, B, C, D, E, F
* Possible patterns in a byte: 0016 to FF16
* Conversion binary -> hex (hex is in annotation, binary numbers grouped by 4 by green lines)
* e.g.: 0100(0x4) 1100(0xC) 1100(0xC) 1001(0x9) 0100(0x4) 0101(0x5) 0100(0x4) 1000(0xB)
* annotation: conversion algorithm?
* Conversion hex -> binary (binary in annotation, an arrow points off from each hex character to the binary sequence representing it)
* e.g.: 3(0011) D(1101) 5(0101) F(1111) (in C: 0x3D5F)
* annotation: conversion algorithm?
## What could these 32 bits represent? -- What kind of information could they encode?
01100010011010010111010001110011<sub>2</sub>
Answer:
* Integer
* string of cleartexts
* colour
## What kind of information (data) do series of bits represent?
Encoding Scheme
Bit pattern:
`01100010 01101001
01110100 01110011`<sub>2</sub>
An arrow pointing to a box labeled "encoding schemes":
* ASCII character
* Unsigned integer
* Twos complement (signed) integer
* Floating point
* Memory Address
* Assembly language
* RGB
* MP3
* …
An arrow pointing to the following list:
* Letters and symbols
* Positive numbers
* Negative numbers
* Real numbers
* C pointers
* Machine-level instructions
* Colour
* Audio/Sound
* …
Definition: An encoding scheme is an interpretation (representation) of a series of bits
Bottom line: Which encoding scheme is used to interpret a series of bits depends on the application currently executing (the “context”) not the computer
## Endian Order of bytes in memory
* It is straight forward to store a byte in memory
* All we need is the byte (series of bits) and a memory address
* For example, lets store byte `01110011`<sub>2</sub> at address `0x0000`
Address|M[]
size1|
...|
0x0003|
0x0002|
0x0001|
0x0000|01110011<sub>2</sub>
## Endian Order of bytes in memory
Question: But how do we store several bytes in memory?
* For example, lets store these 4 bytes starting at address 0x0000
`01000010 01101001 01110100 01110011`<sub>2</sub>
<aside>Trick: (L)ittle-endian, (L)SB, (L)owest address number</aside>
Way 1: Little endian
Address|M[]|Hex (filled out as annotation)
size-1||
...||
0x0003|01000010|42
0x0002|01101001|69
0x0001|01110100|74
0x0000|01110011|73
Way 2: Big endian
Address|M[]|Hex
size1||
...||
0x0003|01110011|73
0x0002|01110100|74
0x0001|01101001|69
0x0000|01000010|42
### Compressed view of memory:
Little-endian:
Address||||||||
size-8||||||||
...||||||||
0x0008||||||||
0x0000|73|74|69|42||||
Big-endian:
Address||||||||
...||||||||
0x0008||||||||
0x0000|42|69|74|73||||
## Review Bit -- Bit Manipulation - Boolean algebra
No matter what a series of bits represent, they can be manipulated using bit-level operations:
* Boolean algebra
* Shifting
* Developed by George Boole in 19th Century
* Algebraic representation of logic
* Encode “True” as 1 and “False” as 0
AND -> A&B = 1 when both A=1 and B=1
&|0|1
0|0|0
1|0|1
NOT -> ~A = 1 when A=0
~|
0|1
1|0
OR -> A|B = 1 when either A=1 or B=1
\||0|1
0|0|1
1|1|1
XOR (Exclusive-Or) -> A^B = 1 when either A=1 or B=1, but not both
^|0|1
0|0|1
1|1|0
## Interesting fact about Boolean algebra and digital logic
* Claude Shannon 1937 masters thesis
* Made connection between Boolean algebra and digital logic
* Boolean algebra could be applied to design and analysis of digital
systems (digital circuits)
* Example:
Diagram of an AND gate.
Two lines go into a black box.
One is 1/high, one is 0/low.
An output comes out the other side: 0/low.
## Review (annotation: HW2)
Lets try some Boolean algebra!
* Operations applied bitwise -> to each bit
* Spot the error(s):
<pre>
01101001
& 01010101
= 01000001
</pre>
<pre>
01101001
| 01010101
= 01111101
</pre>
<pre>
01101001
^ 01010101
= 00111100
(notation on 2nd last bit of equals: error!)
</pre>
<pre>
~ 01010101
= 10101010
</pre>
## Useful bit manipulations
* Using a binary mask (or bit mask) as an operand
1. AND: Extracts particular bit(s) so we can test whether they are set. Example:
<pre>
10110011 <- some value x
& 00000001 <- binary mask
00000001
</pre>
The result tells us that the least significant bit (LSb) is set.
2. XOR: Toggle specific bits
Example: The result tells us that the least significant bit (LSb) of x is set
<pre>
10110011 <- some value x
^ 00011100 <- binary mask
= 10101111 We get a toggled version of the 3 original bits (of x) that
</pre>
(red square around bits 4-6): "We get a toggled version of the three original bits (of x) that corresponds to the 3 set bits of the binary mask."
* Using two operands
1. OR: Merge all set bits of operands
* Example:
<pre>
10110011 <- some value x
| 00011100 <- some value y
= 10111111
</pre>
The result contains all the set bits of x and y.
## Bit Manipulation - Shift operations
* LSb: least significant bit is the rightmost bit of a series of bits (or bit vector)
* MSb: most significant bit is the leftmost bit of a series of bits (or bit vector)
* Left Shift: x << y
* Shift bit vector (a series of bits) `x` left `y` positions.
* Effect:
* Throw away y most significant bits (MSb) of x on left
* Fill x with y 0s on right
* Right Shift: x >> y
* Shift bit vector `x` right `y` positions
* Effect:
* Throw away y least significant bits (LSb) of x on right
* Logical shift: Fill x with y 0s on left
* Arithmetic shift: Fill x with y copies of xs sign bit on left
* Sign bit: most significant bit (MSb) of x (before shifting occurred)
## Bit Manipulation - Shift operations Lets try! (HW3)
* Left Shift: 10111001 << 4 = 10010000
* Left Shift: 10111001 << 2 = 11100100
* Right Shift (logical): 00111001 >> 4 = 00000011
* Right Shift (arithmatic): 10111001 >> 4 = 11111011
* Right Shift (logical/arithmatic): 10111001 >> 2 = 00101110/11101110
## Summary
* Von Neumann architecture
* Architecture of most computers
* Its components: CPU, memory, input and ouput, bus
* One of its characteristics: Data and code (programs) both stored in memory
* A look at memory: defined byte-addressable memory, diagram of (compressed) memory
* Word size (w): size of a series of bits (or bit vector) we manipulate, also size of machine words (see Section 2.1.2)
* A look at bits in memory
* Why binary numeral system (0 and 1 -> two values) is used to represent information in memory
* Algorithm for converting binary to hexadecimal (hex)
1. Partition bit vector into groups of 4 bits, starting from right, i.e., least significant byte (LSB)
* If most significant “byte” (MSB) does not have 8 bits, pad it: add 0s to its left
2. Translate each group of 4 bits into its hex value
* What do bits represent? Encoding scheme gives meaning to bits
* Order of bytes in memory: little endian versus big endian
* Bit manipulation regardless of what bit vectors represent
* Boolean algebra: bitwise operations => AND (&), OR (|), XOR (^), NOT (~)
* Shift operations: left shift, right logical shift and right arithmetic shift
* Logical shift: Fill x with y 0s on left
* Arithmetic shift: Fill x with y copies of xs sign bit on left
* Sign bit: Most significant bit (MSb) before shifting occurred
NOTE: C logical operators and C bitwise (bit-level) operators behave differently! Watch out for && versus &, || versus |, …
## Next Lecture
* Representing data in memory Most of this is review
* “Under the Hood” - Von Neumann architecture
* Bits and bytes in memory
* How to diagram memory -> Used in this course and other references
* How to represent series of bits -> In binary, in hexadecimal (conversion)
* What kind of information (data) do series of bits represent -> Encoding scheme
* Order of bytes in memory -> Endian
* Bit manipulation bitwise operations
* Boolean algebra + Shifting
* Representing integral numbers in memory
* Unsigned and signed
* Converting, expanding and truncating
* Arithmetic operations
* Representing real numbers in memory
* IEEE floating point representation
* Floating point in C casting, rounding, addition, …

File diff suppressed because one or more lines are too long

@ -1,383 +0,0 @@
---
layout: simple
math: true
---
# CMPT 295
Unit - Data Representation
Lecture 3 Representing integral numbers in memory - unsigned and signed
## Last Lecture
* Von Neumann architecture
* Architecture of most computers
* Its components: CPU, memory, input and ouput, bus
* One of its characteristics: Data and code (programs) both stored in memory
* A look at memory: defined byte-addressable memory, diagram of (compressed) memory
* Word size (w): size of a series of bits (or bit vector) we manipulate, also size of machine words (see Section 2.1.2)
* A look at bits in memory
* Why binary numeral system (0 and 1 -> two values) is used to represent information in memory
* Algorithm for converting binary to hexadecimal (hex)
1. Partition bit vector into groups of 4 bits, starting from right, i.e., least significant byte (LSB)
* If most significant “byte” (MSB) does not have 8 bits, pad it: add 0s to its left
2. Translate each group of 4 bits into its hex value
* What do bits represent? Encoding scheme gives meaning to bits
* Order of bytes in memory: little endian versus big endian
* Bit manipulation regardless of what bit vectors represent
* Boolean algebra: bitwise operations => AND (&), OR (\|), XOR (^), NOT (~)
* Shift operations: left shift, right logical shift and right arithmetic shift
* Logical shift: Fill x with y 0s on left
* Arithmetic shift: Fill x with y copies of xs sign bit on left
* Sign bit: Most significant bit (MSb) before shifting occurred
NOTE: C logical operators and C bitwise (bit-level) operators behave differently! Watch out for && versus &, \|\| versus \|, …
## Todays Menu
* Representing data in memory Most of this is review
* “Under the Hood” - Von Neumann architecture
* Bits and bytes in memory
* How to diagram memory -> Used in this course and other references
* How to represent series of bits -> In binary, in hexadecimal (conversion)
* What kind of information (data) do series of bits represent -> Encoding scheme
* Order of bytes in memory -> Endian
* Bit manipulation bitwise operations
* Boolean algebra + Shifting
* Representing integral numbers in memory
* Unsigned and signed
* Converting, expanding and truncating
* Arithmetic operations
* Representing real numbers in memory
* IEEE floating point representation
* Floating point in C casting, rounding, addition, …
## Warm up exercise!
As a warm up exercise, fill in the blanks!
* If the context is C (on our target machine)
* char => \_\_\_\_\_ bits/ \_\_\_\_\_ byte
* short => \_\_\_\_\_ bits/ \_\_\_\_\_ bytes
* int => \_\_\_\_\_ bits/ \_\_\_\_\_ bytes
* long => \_\_\_\_\_ bits/ \_\_\_\_\_ bytes
* float => \_\_\_\_\_ bits/ \_\_\_\_\_ bytes
* double => \_\_\_\_\_ bits/ \_\_\_\_\_ bytes
* pointer (e.g. char *) => \_\_\_\_\_ bits/ \_\_\_\_\_ bytes
## Unsigned integral numbers
Remember:
Address|M[]
size-1|
...|
0x0003|01000010
0x0002|01101001
0x0001|01110100
0x0000|01110011
* What if the byte at M[0x0002] represented an unsigned integral number, what would be its value?
x = a series of bits = bit vector
w = width of bit vector
* $$X = 01101001_{2}, w=8$$
* Lets apply the encoding scheme:
{% katex display %}
\it \text{B2U}(X) = \sum_{i=0}^{w-1} X_{i} \times 2^i
{% endkatex %}
Example: $$0 \times 2^7 + 1 \times 2^6 + 1 \times 2^5 + 0 \times 2^4 + 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^0 = $$
* For w = 8, range of possible unsigned values: [ *blank* ]
* For any w, range of possible unsigned values: [ *blank* ]
* Conclusion: w bits can only represent a fixed # of possible values, but these w bits represent these values exactly
## B2U(X) Conversion (Encoding scheme)
* Positional notation: expand and sum all terms
Decimal:
$$d_{i}$$|$$10^{i}$$
$$d_{i-1}$$|$$10_{i-1}$$
...|...
$$d_{2}$$|100
$$d_{1}$$|10
$$d_{0}$$|1
Binary:
$$b_{i}$$|$$2^{i}$$
$$b_{i-1}$$|$$2^{i-1}$$
...|...
$$b_{2}$$|4
$$b_{1}$$|2
$$b_{0}$$|1
Remember:
{% katex display %}
\it \text{B2U}(X) = \sum_{i=0}^{w-1} X_{i} \times 2^i
{% endkatex %}
Example: $$246_{10} = 2 \times 10^{2} + 4 \times 10^{1} + 6 \times 10^{0}$$
## Range of possible values?
* If the context is C (on our target machine)
* unsigned char?
* unsigned short?
* unsigned int?
* unsigned long?
## Examples of “Show your work”
U2B(X) Conversion (into 8-bit binary # => w = 8)
Method 1 - Using subtraction: subtracting decreasing power of 2 until reach 0:
Starting number: 246
* $$246 128 = 118$$ -> $$128 = 1 \times 2^{7}$$
* $$118 64 = 54$$ -> $$64 = 1 \times 2^{6}$$
* $$54 32 = 22$$ -> $$32 = 1 \times 2^{5}$$
* $$22 16 = 6$$ -> $$16 = 1 \times 2^{4}$$
* $$6 8 = \text{nop!}$$ -> $$8 = 0 \times 2^{3}$$
* $$6 4 = 2$$ -> $$4 = 1 \times 2^{2}$$
* $$2 2 = 0$$ -> $$2 = 1 \times 2^{1}$$
* $$0 1 = \text{nop!}$$ -> $$1 = 0 \times 2^{0}$$
* $$246_{10} = 11110110_{2}$$
Method 2 - Using division: dividing by 2 until reach 0
Start with 246
* $$246 \div 2 = 123,R=0$$
* $$123 \div 2 = 61,R=1$$
* $$61 \div 2 = 30,R=1$$
* $$30 \div 2 = 15,R=0$$
* $$15 \div 2 = 7,R=1$$
* $$7 \div 2 = 3,R=1$$
* $$3 \div 2 = 1,R=1$$
* $$1 \div 2 = 0,R=1$$
* $$246_{10} = 11110110_{2}$$
## U2B(X) Conversion A few tricks
* Decimal -> binary
* Trick: When decimal number is 2n, then its binary representation is 1 followed by n zeros
* Lets try: `if X = 32 => X = 25, then n = 5 => 100002 (w = 5)` What if w = 8? Check: 1 x 24 = 32
* Decimal -> hex
* Trick: When decimal number is 2n, then its hexadecimal representation is 2i followed by j zeros, where n = i + 4j and 0 <= i <=3
* Let try: if X = 8192 => X = 213, then n = 13 and 13 = i + 4j => 1 + 4 x 3
=> 0x2000. Convert 0x2000 into a binary number: Check: 2 x 163 = 2 x 4096 = 8192
## Signed integral numbers
### Remember:
Address|M[]
size-1|
...|
0x0003|01000010
0x0002|01101001
0x0001|01110100
0x0000|01110011
* What if the byte at M[0x0001] represented a signed integral number, what would be its value?
* X = 111101002 w = 8
* T => Twos Complement, w => width of the bit vector (annotation: first part of equaltion [everything before the plus sign] is the "sign bit")
* Lets apply the encoding scheme:
{% katex display %}
\it {B2T}(X) = -x_{w-1} \times 2^{w-1} + \sum_{i=0}^{w-2} x_{i} \times 2^{i}
{% endkatex %}
Example: $$-1 \times 2^{7} + 1 \times 2^{6} + 1 \times 2^{5} + 1 \times 2^{4} + 0 \times 2^{3} + 1 \times 2^{2} + 0 \times 2^{1} + 0 \times 2^{0} = ?$$
* What would be the bit pattern of the …
* Most negative value:
* Most positive value:
* For w = 8, range of possible signed values: [ *blank* ]
* For any w, range of possible signed values: [ *blank* ]
* Conclusion: same as for unsigned integral numbers
## Examples of “Show your work”
T2B(X) Conversion -> Twos Complement
Annotation: w = 8
Method 1: If X < 0, (~(U2B(\|X\|)))+1
If X = -14 (and 8 bit binary #s)
1. $$\lvert X\rvert => \lvert -14 \vert = $$
2. $$\text{U2B}(14)$$ =>
3. (first symbol is a tilde) $$\sim(00001110_{2})$$ =>
4. $$(11110001_{2})+1$$ =>
Binary addition:
<pre>
11110001
+ 00000001
= ????????
</pre>
Method 2: If X = -14 (and 8 bit binary #s)
1. $$X + 2^{w} => -14 +$$
2. $$\text{U2B}(242)$$ =>
Using subtraction:
1. $$242 - 128 = 114$$ -> $$1 \times 2^{7}$$
2. $$114 - 64 = 50$$ -> $$1 \times 2^{6}$$
3. $$50 32 = 18$$ -> $$1 \times 2^{5}$$
4. $$18 16 = 2$$ -> $$1 \times 2^{4}$$
5. $$2 8 = \text{nop!}$$ -> $$0 \times 2^{3}$$
6. $$2 4 = \text{nop!}$$ -> $$0 \times 2^{2}$$
7. $$2 2 = 0$$ -> $$1 \times 2^{1}$$
8. $$0 1 = \text{nop!}$$ -> $$0 \times 2^{0}$$
## Properties of unsigned & signed conversions
Annotation: w = 4
X|B2U(X)|B2T(X)
0000|0|0
0001|1|1
0010|2|2
0011|3|3
0100|4|4
0101|5|5
0110|6|6
0111|7|7
1000|8|-8
1001|9|-7
1010|10|-6
1011|11|-5
1100|12|-4
1101|13|-3
1110|14|-2
1111|15|-1
* Equivalence
* Both encoding schemes (B2U and B2T ) produce the same bit patterns for nonnegative values
* Uniqueness
* Every bit pattern produced by these encoding schemes (B2U and B2T) represents a unique (and exact) integer value
* Each representable integer has unique bit pattern
## Converting between signed & unsigned of same size (same data type)
* Unsigned
* w=8
* if unsigned `ux` = 129<sub>10</sub>
* U2T(X) = B2T(U2B(X))
* then x = ???
* Maintain Same Bit Pattern
* Signed (Twos Complement)
* w=4
* if signed (2's C) $$x = -5_{10}$$
* T2U(X) = B2U(T2B(X))
* then unsigned `ux` = ???
* Maintain Same Bit Pattern
* Conclusion - Converting between unsigned and signed numbers:
Both have same bit pattern, however, this bit pattern may be interpreted differently, i.e., producing a different value
## Converting signed to unsigned (and back) with $$w = 4$$
Signed|Bits|Unsigned|Note
0|0000|0|All rows from 0-7 inclusive can be converted from signed to unsigned with T2U(X), and unsigned to signed with U2T(X).
1|0001|1|
2|0010|2|
3|0011|3|
4|0100|4|
5|0101|5|
6|0110|6|
7|0111|7|
-8|1000|8|All rows from here to 15 inclusive can be converted to the other like so: T2U(signed + 16/$$2^{4}$$) -> unsigned, U2T(unsigned - 16/$$2^{4}$$) -> signed.
-7|1001|9|
-6|1010|10|
-5|1011|11|
-4|1100|12|
-3|1101|13|
-2|1110|14|
-1|1111|15|
## Visualizing the relationship between signed & unsigned
If $$w = 4, 2^{4} = 16$$
* Signed (2's Complement) Range: TMin to TMax (0 is the center)
* Unsigned range: 0 to UMax (TMax is the center)
## Sign extension
* Converting unsigned (or signed) of different sizes (different data types)
1. Small data type -> larger
* Sign extension
* Unsigned: zero extension
* Signed: sign bit extension
* Conclusion: Value unchanged
* Lets try:
* Going from a data type that has a width of 3 bits (w = 3) to a data type that has a width of 5 bits (w = 5)
* Unsigned: $$X = 3 = 011_{2},w=3$$, $$X = 4 = 100_{2},w = 3$$
* New: $$X = ? = ?_{2},w=5$$, $$X = ? + ?_{2},w=5$$
* Signed: $$X = 3 = 011_{2},w=3$$, $$X=-3 = 101_{2},w=3$$
* New: $$X = ? = ?_{2},w=5$$, $$x = ? = ?_{2}, w=5$$
## Truncation
* Converting unsigned (or signed) of different sizes(different data types)
2. Large data type -> smaller
* Truncation
* Conclusion: Value may be altered
* A form of overflow
* Lets try:
* Going from a data type that has a width of 5 bits (w = 5) to a data type that has a width of 3 bits (w = 3)
* Unsigned: $$X = 27 = 11011_{2},w = 5$$
* New: $$X = ? = ?_{2},w=3$$
* Signed: $$X = -15 = 10001_{2},w=3$$, $$X = -1 = 11111_{2}, w=5$$
* New: $$X = ? = ?_{2}, w=3$$, $$X = ? = ?_{2}, w=3$$
## Summary
* Interpretation of bit pattern B into either unsigned value U or signed value T
* B2U(X) and U2B(X) encoding schemes (conversion)
* B2T(X) and T2B(X) encoding schemes (conversion)
* Signed value expressed as twos complement => T
* Conversions from unsigned <-> signed values
* U2T(X) and T2U(X) => adding or subtracting $$2^{w}$$
* Implication in C: when converting (implicitly via promotion and explicitly via casting):
* Sign:
* Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may be interpreted differently
* Can have unexpected effects -> producing a different value
* Size:
* Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int)
* sign extension: For unsigned -> zeros extension, for signed -> sign bit extension
* Both yield expected result > resulting value unchanged
* Large -> small (e.g., unsigned int to unsigned short)
* truncation: Unsigned/signed -> most significant bits are truncated (discarded)
* May not yield expected results -> original value may be altered
* Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
## Next Lecture
* Representing data in memory Most of this is review
* “Under the Hood” - Von Neumann architecture
* Bits and bytes in memory
* How to diagram memory -> Used in this course and other references
* How to represent series of bits -> In binary, in hexadecimal (conversion)
* What kind of information (data) do series of bits represent -> Encoding scheme
* Order of bytes in memory -> Endian
* Bit manipulation bitwise operations
* Boolean algebra + Shifting
* Representing integral numbers in memory
* Unsigned and signed
* Converting, expanding and truncating
* Arithmetic operations
* Representing real numbers in memory
19
* IEEE floating point representation
* Floating point in C casting, rounding, addition, …

@ -1,781 +0,0 @@
CMPT 295
Unit - Data Representation
Lecture 3 Representing integral numbers in memory - unsigned and signed
1
Last Lecture
 Von Neumann architecture
 Architecture of most computers
 Its components: CPU, memory, input and ouput, bus
 One of its characteristics: Data and code (programs) both stored in memory
 A look at memory: defined byte-addressable memory, diagram of (compressed) memory
 Word size (w): size of a series of bits (or bit vector) we manipulate, also size of machine words
(see Section 2.1.2)
 A look at bits in memory
 Why binary numeral system (0 and 1 -> two values) is used to represent information in memory
 Algorithm for converting binary to hexadecimal (hex)
1. Partition bit vector into groups of 4 bits, starting from right, i.e., least significant byte (LSB)
 If most significant “byte” (MSB) does not have 8 bits, pad it: add 0s to its left
2. Translate each group of 4 bits into its hex value
 What do bits represent? Encoding scheme gives meaning to bits
 Order of bytes in memory: little endian versus big endian
 Bit manipulation regardless of what bit vectors represent
 Boolean algebra: bitwise operations => AND (&), OR (|), XOR (^), NOT (~)
 Shift operations: left shift, right logical shift and right arithmetic shift
2
 Logical shift: Fill x with y 0s on left
 Arithmetic shift: Fill x with y copies of xs sign bit on left
 Sign bit: Most significant bit (MSb) before shifting occurred
NOTE:
C logical operators
and C bitwise (bit-level)
operators behave
differently!
Watch out for && versus
&, || versus |, …
Todays Menu
 Representing data in memory Most of this is review
 “Under the Hood” - Von Neumann architecture
 Bits and bytes in memory
 How to diagram memory -> Used in this course and other references
 How to represent series of bits -> In binary, in hexadecimal (conversion)
 What kind of information (data) do series of bits represent -> Encoding scheme
 Order of bytes in memory -> Endian
 Bit manipulation bitwise operations
 Boolean algebra + Shifting
 Representing integral numbers in memory
 Unsigned and signed
 Converting, expanding and truncating
 Arithmetic operations
 Representing real numbers in memory
3
 IEEE floating point representation
 Floating point in C casting, rounding, addition, …
Warm up exercise!
As a warm up exercise, fill in the blanks!
 If the context is C (on our target machine)
 char
=> _____ bits/ _____ byte
 short => _____ bits/ _____ bytes
 int
=> _____ bits/ _____ bytes
 long
=> _____ bits/ _____ bytes
 float => _____ bits/ _____ bytes
 double=> _____ bits/ _____ bytes
 pointer (e.g. char *)
4
=> _____ bits/ _____ bytes
Remember:
Unsigned integral numbers
 What if the byte at M[0x0002] represented an unsigned integral
A series of bits
number, what would be its value?
=> bit vector
w =>width of
 X = 011010012
the bit vector
w=8
 Lets apply the encoding scheme:
B2U(X) 
w1
 xi 2
i
i0
0 x 27 + 1 x 26 + 1 x 25 + 0 x 24 + 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20 =
5
 For w = 8, range of possible unsigned values: [
]
 For any w, range of possible unsigned values: [
]
 Conclusion: w bits can only represent a fixed # of possible values,
but these w bits represent these values exactly
B2U(X) Conversion (Encoding scheme)
 Positional notation: expand and sum all terms
•••
10i
2i
10i-1
2i-1
100
10
1
4
2
1
•••
di di-1 ••• d2 d1 d0
Example: 24610 = 2 x 102 + 4 x 101 + 6 x 100
6
1s = 100
10s = 101
100s = 102
B2U(X ) 
w1
 xi 2
i0
i
Range of possible values?
 If the context is C (on our target machine)
unsigned char?
unsigned short?
unsigned int?
unsigned long?
7
Examples of “Show your work”
U2B(X) Conversion (into 8-bit binary # => w = 8)
8
Method 1 - Using subtraction:
subtracting decreasing
power of 2 until reach 0
246 => 246 128 = 118 ->128 = 1 x 27
118 64 = 54
-> 64 = 1 x 26
54 32 = 22
-> 32 = 1 x 25
22 16 = 6
-> 16 = 1 x 24
6 8 = nop! -> 8 = 0 x 23
6 4 =2
-> 4 = 1 x 22
2 2=0
-> 2 = 1 x 21
0 1 = nop! -> 1 = 0 x 20
Method 2 - Using division:
dividing by 2
until reach 0
246 => 246 / 2 = 123 -> R = 0
123 / 2 = 61 -> R = 1
61 / 2 = 30 -> R = 1
30 / 2 = 15 -> R = 0
15 / 2 = 7
-> R = 1
7/2 =3
-> R = 1
3/2 =1
-> R = 1
1/2 =0
-> R = 1
246 => 1 1 1 1 0 1 1 02
246 => 1 1 1 1 0 1 1 02
U2B(X) Conversion A few tricks
 Decimal -> binary
 Trick: When decimal number is 2n, then its binary representation is 1 followed
by n zeros
 Lets try: if X = 32 => X = 25, then n = 5 => 100002 (w = 5)
What if w = 8?
Check: 1 x 24 = 32
 Decimal -> hex
 Trick: When decimal number is 2n, then its hexadecimal representation is 2i
followed by j zeros, where n = i + 4j and 0 <= i <=3
 Let try: if X = 8192 => X = 213, then n = 13 and 13 = i + 4j => 1 + 4 x 3
=> 0x2000
9
Convert 0x2000 into a binary number:
Check: 2 x 163 = 2 x 4096 = 8192
Remember:
Signed integral numbers
 What if the byte at M[0x0001] represented a signed integral
number, what would be its value? T => Twos Complement w =>width of
the bit vector
 X = 111101002 w = 8
w2
w1
i
B2T
(X
)
x
2
x
2
w1
i
 Lets apply the encoding scheme:
Sign bit
i0
-1 x 27 + 1 x 26 + 1 x 25 + 1 x 24 + 0 x 23 + 1 x 22 + 0 x 21 + 0 x 20 =
 What would be the bit pattern of the …
 Most negative value:
 Most positive value:
10
 For w = 8, range of possible signed values: [
]
 For any w, range of possible signed values: [
]
 Conclusion: same as for unsigned integral numbers
Examples of “Show your work”
T2B(X) Conversion -> Twos Complement
w=8
Method 1 If X < 0, (~(U2B(|X|)))+1
Method 2
If X = -14 (and 8 bit binary #s)
If X = -14 (and 8 bit binary #s)
1. |X| => |-14| =
1.
2. U2B(14) =>
2. U2B(242) =>
3. ~(000011102) =>
4. (111100012)+1 =>
Binary addition:
11110001
+ 00000001
11
Check:
If X < 0, U2B(X + 2w)
X + 2w => -14 +
Using subtraction:
242 128 = 114 -> 1 x 27
114 64 = 50 -> 1 x 26
50 32 = 18
-> 1 x 25
18 16 = 2
-> 1 x 24
2 8 -> nop! -> 0 x 23
2 4 -> nop! -> 0 x 22
22=0
-> 1 x 21
0 1 -> nop! -> 0 x 20
Properties of unsigned & signed conversions
w=4
12
X
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
B2U(X)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
B2T(X)
0
1
2
3
4
5
6
7
8
7
6
5
4
3
2
1
 Equivalence
 Both encoding schemes (B2U
and B2T ) produce the same bit
patterns for nonnegative values
 Uniqueness
Every bit pattern produced by
these encoding schemes (B2U
and B2T ) represents a unique
(and exact) integer value
 Each representable integer has
unique bit pattern
Converting between signed & unsigned
of same size (same data type)
Unsigned
w=8
ux
If ux = 12910
Signed (Twos Complement)
w=4
13
x
If x = -510
U2T
U2B X
B2T
Maintain Same Bit Pattern
then x =
Unsigned
T2U
T2B X
Signed (Twos Complement)
x
B2U
Maintain Same Bit Pattern
ux
then ux =
 Conclusion - Converting between unsigned and signed numbers:
Both have same bit pattern, however, this bit pattern may be interpreted
differently, i.e., producing a different value
Converting signed  unsigned with w = 4
Signed
Bits
Unsigned
0
0000
0
1
0001
2
0010
2
3
0011
3
0100
4
5
0101
5
6
0110
6
7
0111
7
-8
1000
8
-7
1001
9
-6
1010
10
4
-5
U2T(X)
+ 16 (+24)
-4
-3
14
14
-2
-1
U2T(X)
T2U(X)
1
1011
T2U(X)
11
1100
- 16 (+24)
12
1101
13
1110
14
1111
15
Visualizing the relationship between
signed & unsigned
If w = 4,
24
UMax
UMax 1
= 16
TMax
Signed
(2s Complement)
Range
15
0
1
2
TMin
TMax + 1
TMax
0
Unsigned
Range
Sign extension
 Converting unsigned (or signed) of different sizes (different data types)
1. Small data type -> larger
Sign bit
 Sign extension
X
Unsigned: zero extension
•••
Signed: sign bit extension
 Conclusion: Value unchanged
•••
X
 Lets try:
•••
•••
 Going from a data type that has a width of 3 bits (w = 3) to a data type
that has a width of 5 bits (w = 5)
 Unsigned: X = 3 =>
new X =
16
 Signed:
0112 w = 3
<=
w=5
X = 3 =>
0112 w = 3
new X =
<=
w=5
X = 4 =>
new X =
1002 w = 3
<=
w=5
X = -3 =>
1012 w = 3
new X =
<=
w=5
Truncation
 Converting unsigned (or signed) of different sizes(different data types)
2. Large data type -> smaller
•••
X
 Truncation
•••
 Conclusion: Value may be altered
A form of overflow
 Lets try:
X
•••
 Going from a data type that has a width of 5 bits (w = 5) to a data type
that has a width of 3 bits (w = 3)
 Unsigned: X = 27 => 110112 w = 5
new X =
 Signed:
17
<=
w=3
X = -15 => 100012 w = 5
new X =
<=
w=3
X = -1 => 111112 w = 5
new X =
<=
w=3
Summary
 Interpretation of bit pattern B into either unsigned value U or signed value T
 B2U(X) and U2B(X) encoding schemes (conversion)
 B2T(X) and T2B(X) encoding schemes (conversion)
 Signed value expressed as twos complement => T
 Conversions from unsigned <-> signed values
 U2T(X) and T2U(X) => adding or subtracting 2w
 Implication in C: when converting (implicitly via promotion and explicitly via casting):
 Sign:
 Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may
be interpreted differently
 Can have unexpected effects -> producing a different value
 Size:
 Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int)
 sign extension: For unsigned -> zeros extension, for signed -> sign bit extension
 Both yield expected result > resulting value unchanged
 Large -> small (e.g., unsigned int to unsigned short)
 truncation: Unsigned/signed -> most significant bits are truncated (discarded)
 May not yield expected results -> original value may be altered
18
 Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
Next Lecture
 Representing data in memory Most of this is review
 “Under the Hood” - Von Neumann architecture
 Bits and bytes in memory
 How to diagram memory -> Used in this course and other references
 How to represent series of bits -> In binary, in hexadecimal (conversion)
 What kind of information (data) do series of bits represent -> Encoding scheme
 Order of bytes in memory -> Endian
 Bit manipulation bitwise operations
 Boolean algebra + Shifting
 Representing integral numbers in memory
 Unsigned and signed
 Converting, expanding and truncating
 Arithmetic operations
 Representing real numbers in memory
19
 IEEE floating point representation
 Floating point in C casting, rounding, addition, …

@ -1,584 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="/assets/css/katex.css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-295">CMPT 295</h1>
<p>Unit - Data Representation</p>
<p>Lecture 4 Representing integral numbers in memory Arithmetic operations</p>
<h2 id="warm-up-question">Warm up question</h2>
<ul>
<li>What is the value of …
<ul>
<li>TMin (in hex) for signed char in C: _________________</li>
<li>TMax (in hex) for signed int in C: _________________</li>
<li>TMin (in hex) for signed short in C: ________________</li>
</ul>
</li>
</ul>
<h2 id="last-lecture">Last Lecture</h2>
<ul>
<li>Interpretation of bit pattern B into either unsigned value U or signed value T
<ul>
<li>B2U(X) and U2B(X) encoding schemes (conversion)</li>
<li>B2T(X) and T2B(X) encoding schemes (conversion)
<ul>
<li>Signed value expressed as twos complement =&gt; T</li>
</ul>
</li>
</ul>
</li>
<li>Conversions from unsigned &lt;-&gt; signed values
<ul>
<li>U2T(X) and T2U(X) =&gt; adding or subtracting 2w</li>
</ul>
</li>
<li>Implication in C: when converting (implicitly via promotion and explicitly via casting):
<ul>
<li>Sign:
<ul>
<li>Unsigned &lt;-&gt; signed (of same size) -&gt; Both have same bit pattern, however, this bit pattern may be interpreted differently
<ul>
<li>Can have unexpected effects -&gt; producing a different value</li>
</ul>
</li>
</ul>
</li>
<li>Size:
<ul>
<li>Small -&gt; large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int)
<ul>
<li>sign extension: For unsigned -&gt; zeros extension, for signed -&gt; sign bit extension</li>
<li>Both yield expected result &gt; resulting value unchanged</li>
</ul>
</li>
<li>Large -&gt; small (for signed, e.g., int to short and for unsigned, e.g., unsigned int to unsigned short)
<ul>
<li>truncation: Unsigned/signed -&gt; most significant bits are truncated (discarded)</li>
<li>May not yield expected results -&gt; original value may be altered</li>
</ul>
</li>
</ul>
</li>
<li>Both (sign and size): 1) size conversion is first done then 2) sign conversion is done</li>
</ul>
</li>
</ul>
<h2 id="todays-menu">Todays Menu</h2>
<ul>
<li>Representing data in memory Most of this is review
<ul>
<li>“Under the Hood” - Von Neumann architecture</li>
<li>Bits and bytes in memory
<ul>
<li>How to diagram memory -&gt; Used in this course and other references</li>
<li>How to represent series of bits -&gt; In binary, in hexadecimal (conversion)</li>
<li>What kind of information (data) do series of bits represent -&gt; Encoding scheme</li>
<li>Order of bytes in memory -&gt; Endian</li>
</ul>
</li>
<li>Bit manipulation bitwise operations
<ul>
<li>Boolean algebra + Shifting</li>
</ul>
</li>
</ul>
</li>
<li>Representing integral numbers in memory
<ul>
<li>Unsigned and signed</li>
<li>Converting, expanding and truncating</li>
<li>Arithmetic operations</li>
</ul>
</li>
<li>Representing real numbers in memory
<ul>
<li>IEEE floating point representation</li>
<li>Floating point in C casting, rounding, addition, …</li>
</ul>
</li>
</ul>
<p>Lets first illustrate what we covered last lecture with a demo!</p>
<h2 id="demo--looking-at-size-and-sign-conversions-in-c">Demo Looking at size and sign conversions in C</h2>
<ul>
<li>What does the demo illustrate?
<ul>
<li>Size conversion:
<ul>
<li>Converting to a larger (wider) data type -&gt; Converting short to int</li>
<li>Converting to a smaller (narrower) data type -&gt; Converting short to char</li>
</ul>
</li>
<li>Sign conversion:
<ul>
<li>Converting from signed to unsigned -&gt; Converting short to unsigned short</li>
<li>Converting from unsigned to signed -&gt; Converting unsigned short to short</li>
</ul>
</li>
<li>Size and Sign conversion:
<ul>
<li>Converting from signed to unsigned larger (wider) data type -&gt; Converting short to unsigned int</li>
<li>Converting from signed to unsigned smaller (narrower) data type -&gt;
Converting short to unsigned char</li>
</ul>
</li>
</ul>
</li>
<li>This demo (code and results) posted on our course web site</li>
</ul>
<h2 id="integer-addition-unlimited-space">Integer addition (unlimited space)</h2>
<ul>
<li>
<p>What happens when we add two decimal numbers? (Highlights show carring the one to complete the equation)
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>10</mn><msub><mn>7</mn><mn>10</mn></msub><mo>+</mo><mn>93</mn><msub><mn>8</mn><mn>10</mn></msub><mo>=</mo><mn>104</mn><msub><mn>5</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">
107_{10} + 938_{10} = 1045_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord"><span class="mord">7</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">9</span><span class="mord">3</span><span class="mord"><span class="mord">8</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
</li>
<li>
<p>Same thing happens when we add two binary numbers:</p>
</li>
</ul>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>10110</mn><msub><mn>0</mn><mn>2</mn></msub><mo>+</mo><mn>10111</mn><msub><mn>0</mn><mn>2</mn></msub><mo>=</mo><mn>101101</mn><msub><mn>0</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">
101100_{2} + 101110_{2} = 1011010_{2}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord">0</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<h2 id="unsigned-addition-limited-space-ie-fixed-size-in-memory">Unsigned addition (limited space, i.e., fixed size in memory)</h2>
<p>What happens when we add two unsigned values?</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>w</mi><mo>=</mo><mn>8</mn></mrow><annotation encoding="application/x-tex">w=8</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">8</span></span></span></span></span>
<p>a)</p>
<p>Binary:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0011101</mn><msub><mn>1</mn><mn>2</mn></msub><mo>+</mo><mn>0101101</mn><msub><mn>0</mn><mn>2</mn></msub><mo>=</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">
00111011_{2} + 01011010_{2} = ?
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mclose">?</span></span></span></span></span></p>
<p>Decimal:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>5</mn><msub><mn>9</mn><mn>10</mn></msub><mo>+</mo><mn>9</mn><msub><mn>0</mn><mn>10</mn></msub><mo>=</mo><mn>14</mn><msub><mn>9</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">
59_{10} + 90_{10} = 149_{10}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">5</span><span class="mord"><span class="mord">9</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">9</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">4</span><span class="mord"><span class="mord">9</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<p>b)</p>
<p>Binary:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1010111</mn><msub><mn>0</mn><mn>2</mn></msub><mo>+</mo><mn>1100101</mn><msub><mn>1</mn><mn>2</mn></msub><mo>=</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">
10101110_{2} + 11001011_{2} = ?
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">1</span><span class="mord">0</span><span class="mord">0</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mclose">?</span></span></span></span></span></p>
<p>Decimal:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>17</mn><msub><mn>4</mn><mn>10</mn></msub><mo>+</mo><mn>20</mn><msub><mn>3</mn><mn>10</mn></msub><mo>=</mo><mn>37</mn><msub><mn>7</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">
174_{10} + 203_{10} = 377_{10}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">7</span><span class="mord"><span class="mord">4</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">2</span><span class="mord">0</span><span class="mord"><span class="mord">3</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">3</span><span class="mord">7</span><span class="mord"><span class="mord">7</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<h2 id="unsigned-addition-u_w-and-overflow">Unsigned addition (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mo>+</mo><mi>w</mi><mi>u</mi></msubsup></mrow><annotation encoding="application/x-tex">+^{u}_{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.911392em;vertical-align:-0.247em;"></span><span class="mord"><span class="mbin">+</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">u</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span></span></span></span>) and overflow</h2>
<ul>
<li>True/expected sum is the result of integer addition with unlimited space.</li>
<li>Actual sum is the result of unsigned addition with limited space. Discarding the carry out bit.</li>
<li>Discarding carry out bit has same effect as applying modular arithmetic</li>
</ul>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mo>=</mo><mi>U</mi><msubsup><mo>+</mo><mi>w</mi><mi>u</mi></msubsup><mi>v</mi><mo>=</mo><mo stretchy="false">(</mo><mi>u</mi><mo>+</mo><mi>v</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="1em"></mspace><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup></mrow><annotation encoding="application/x-tex">
s = U+^{u}_{w}v = (u + v) \mod{2^{w}}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">s</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9613919999999999em;vertical-align:-0.247em;"></span><span class="mord mathdefault" style="margin-right:0.10903em;">U</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"><span class="mbin">+</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight">u</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">v</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathdefault">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">v</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:1em;"></span></span><span class="base"><span class="strut" style="height:0.7143919999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">m</span><span class="mord mathrm">o</span><span class="mord mathrm">d</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<ul>
<li>Operands: w bits</li>
<li>True Sum: w+1 bits</li>
</ul>
<h2 id="closer-look-at-unsigned-addition-overflow">Closer look at unsigned addition overflow</h2>
<ul>
<li>w = 8 -&gt; [0..255]</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>25</mn><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mn>1111111</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">255_{10} = 11111111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">25</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1111111</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>9</mn><msub><mn>0</mn><mn>10</mn></msub><mo>=</mo><mn>0101101</mn><msub><mn>0</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">90_{10} = 01011010_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">9</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0101101</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mn>0010110</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">45_{10} = 00101101_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0010110</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
</ul>
<h3 id="example-1">Example 1:</h3>
<p>Decimal (carry out the 1 in 135):</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>9</mn><msub><mn>0</mn><mn>10</mn></msub><mo>+</mo><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mn>13</mn><msub><mn>5</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">
90_{10} + 45_{10} = 135_{10}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">9</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">3</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<p>Binary (carry out the 1 in <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1000011</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">10000111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1000011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>):</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0101101</mn><msub><mn>0</mn><mn>2</mn></msub><mo>+</mo><mn>0010110</mn><msub><mn>1</mn><mn>2</mn></msub><mo>=</mo><mn>1000011</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">
01011010_{2} + 00101101_{2} = 10000111_{2}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0</span><span class="mord">0</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord">0</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">0</span><span class="mord">0</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<ul>
<li>True sum, w=8: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1000011</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">10000111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1000011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></li>
<li>Actual (overflowed) sum, w=8: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1000011</mn><msub><mn>1</mn><mn>2</mn></msub><mo>=</mo><mn>13</mn><msub><mn>5</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">10000111_{2} = 135_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1000011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">13</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></li>
</ul>
<h3 id="example-2">Example 2:</h3>
<p>Decimal (carry 1 to the 3 in 300):</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>25</mn><msub><mn>5</mn><mn>10</mn></msub><mo>+</mo><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mn>30</mn><msub><mn>0</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">
255_{10} + 45_{10} = 300_{10}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">2</span><span class="mord">5</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">3</span><span class="mord">0</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<p>Binary (carry the 1 at the beginning of the result):</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1111111</mn><msub><mn>1</mn><mn>2</mn></msub><mo>+</mo><mn>0010110</mn><msub><mn>1</mn><mn>2</mn></msub><mo>=</mo><mn>10010110</mn><msub><mn>0</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">
11111111_{2} + 00101101_{2} = 100101100_{2}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">1</span><span class="mord">1</span><span class="mord">1</span><span class="mord">1</span><span class="mord">1</span><span class="mord">1</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0</span><span class="mord">0</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord">0</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">0</span><span class="mord">1</span><span class="mord">0</span><span class="mord">1</span><span class="mord">1</span><span class="mord">0</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<ul>
<li>True sum, w=9: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>10010110</mn><msub><mn>0</mn><mn>2</mn></msub><mo>=</mo><mn>30</mn><msub><mn>0</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">100101100_{2} = 300_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">10010110</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">30</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></li>
<li>Actual (overflowed) sum, w=8: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0010110</mn><msub><mn>0</mn><mn>2</mn></msub><mo>=</mo><mn>4</mn><msub><mn>4</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">00101100_{2} = 44_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0010110</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">4</span><span class="mord"><span class="mord">4</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></li>
</ul>
<h2 id="comparing-integer-addition-with-overflow-effect-of-unsigned-addition-w--4">Comparing integer addition with Overflow: Effect of unsigned addition (w = 4)</h2>
<p>Annotation: with unlimited space:</p>
<p>A 3d chart showing two numbers being added.
a and b on the z and x axies, the sum on the y axis.
y goes to a maximum height of 32
(a = 15) + (b = 15) = (y = 30)
Annotation: With limited space (fixed-size memory):</p>
<p>A 3d chart showing two numbers being added.
a and b on the z and x axies, the sum on the y axis.
y goes to a maximum height of 15
(a = 15) + (b = 15) = (y = 14)</p>
<p>An overflow occurs when there is a carry out</p>
<p>For example: 15 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>111</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">1111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">111</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) + 15 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>111</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">1111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">111</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) = 30 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1111</mn><msub><mn>0</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">11110_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1111</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) as a true sum, and 14 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1111</mn><msub><mn>0</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">11110_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1111</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) as an actual sum.</p>
<h2 id="signed-addition-limited-space-ie-fixed-size-in-memory">Signed addition (limited space, i.e., fixed size in memory)</h2>
<p>What happens when we add two signed values:</p>
<p>w=8</p>
<p>a)</p>
<p>Binary:
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0011101</mn><msub><mn>1</mn><mn>2</mn></msub><mo>+</mo><mn>0101101</mn><msub><mn>0</mn><mn>2</mn></msub><mo>=</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">00111011_{2} + 01011010_{2} = ?</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0011101</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0101101</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mclose">?</span></span></span></span></p>
<p>Decimal:
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>5</mn><msub><mn>9</mn><mn>10</mn></msub><mo>+</mo><mn>9</mn><msub><mn>0</mn><mn>10</mn></msub><mo>=</mo><mn>14</mn><msub><mn>9</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">59_{10} + 90_{10} = 149_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">5</span><span class="mord"><span class="mord">9</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">9</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">14</span><span class="mord"><span class="mord">9</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>b)</p>
<p>Binary: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1010111</mn><msub><mn>0</mn><mn>2</mn></msub><mo>+</mo><mn>1100101</mn><msub><mn>1</mn><mn>2</mn></msub><mo>=</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">10101110_{2} + 11001011_{2} = ?</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1010111</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1100101</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mclose">?</span></span></span></span></p>
<p>Decimal: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>8</mn><msub><mn>2</mn><mn>10</mn></msub><mo>+</mo><mo></mo><mn>5</mn><msub><mn>3</mn><mn>10</mn></msub><mo>=</mo><mo></mo><mn>13</mn><msub><mn>5</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">-82_{10} + -53_{10} = -135_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">8</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">5</span><span class="mord"><span class="mord">3</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">13</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Observation: Unsigned and signed additions have identical behavior @ the bit level, i.e., their sum have the same bit-level representation, but their interpretation differs</p>
<h2 id="signed-addition-t_w-and-overflow">Signed addition (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mo>+</mo><mi>w</mi><mi>t</mi></msubsup></mrow><annotation encoding="application/x-tex">+^{t}_{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.040556em;vertical-align:-0.247em;"></span><span class="mord"><span class="mbin">+</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.7935559999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span></span></span></span>) and overflow</h2>
<p>True sum would be the result of integer addition with unlimited space:
Actual sum is the result of signed addition with limited space:</p>
<p>Operands: w bits
True Sum: w+1 bits</p>
<p>After discarduing carry out bit: w bits (overflow)</p>
<ul>
<li>Discarding carry out bit has same effect as applying modular arithmetic</li>
</ul>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mo>=</mo><mi>u</mi><msubsup><mo>+</mo><mi>w</mi><mi>t</mi></msubsup><mi>v</mi><mo>=</mo><msub><mtext>U2T</mtext><mi>w</mi></msub><mo stretchy="false">[</mo><mo stretchy="false">(</mo><mi>u</mi><mo>+</mo><mi>v</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="1em"></mspace><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">
s = u +^{t}_{w} v = \text{U2T}_{w}[(u + v) \mod 2^{w}]
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">s</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0905559999999999em;vertical-align:-0.247em;"></span><span class="mord mathdefault">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"><span class="mbin">+</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8435559999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight">t</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">v</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord text"><span class="mord">U2T</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.02691em;">w</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">[</span><span class="mopen">(</span><span class="mord mathdefault">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">v</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:1em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">m</span><span class="mord mathrm">o</span><span class="mord mathrm">d</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></span></p>
<p>Negative overflow and positive overflows are possible.
Diagram showing negative overflows becoming positive, and positive overflows becoming negative.</p>
<h2 id="closer-look-at-signed-addition-overflow">Closer look at signed addition overflow</h2>
<ul>
<li>w = 8 -&gt; [-128..127]</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>9</mn><msub><mn>0</mn><mn>10</mn></msub><mo>=</mo><mn>0101101</mn><msub><mn>0</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">90_{10} = 01011010_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">9</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0101101</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mn>0010110</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">45_{10} = 00101101_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0010110</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo></mo><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mn>1101001</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">-45_{10} = 11010011_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1101001</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo></mo><mn>9</mn><msub><mn>0</mn><mn>10</mn></msub><mo>=</mo><mn>1010011</mn><msub><mn>0</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">-90_{10} = 10100110_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">9</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1010011</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
</ul>
<h3 id="example-1-1">Example 1:</h3>
<p>Decimal: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>9</mn><msub><mn>0</mn><mn>10</mn></msub><mo>+</mo><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mn>13</mn><msub><mn>5</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">90_{10} + 45_{10} = 135_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">9</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">13</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Binary: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0101101</mn><msub><mn>0</mn><mn>2</mn></msub><mo>+</mo><mn>0010110</mn><msub><mn>1</mn><mn>2</mn></msub><mo>=</mo><mn>01000011</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">01011010_{2} + 00101101_{2} = 010000111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0101101</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0010110</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">01000011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>The binary result is -121</p>
<h3 id="example-2-1">Example 2:</h3>
<p>Decimal: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>9</mn><msub><mn>0</mn><mn>10</mn></msub><mo>+</mo><mo></mo><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mo></mo><mn>13</mn><msub><mn>5</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">-90_{10} + -45_{10} = -135_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">9</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">13</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Binary: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1010011</mn><msub><mn>0</mn><mn>2</mn></msub><mo>+</mo><mn>1101001</mn><msub><mn>1</mn><mn>2</mn></msub><mo>=</mo><mn>10111100</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">10100110_{2} + 11010011_{2} = 101111001_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1010011</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1101001</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">10111100</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Binary result is 121</p>
<h3 id="example-3">Example 3:</h3>
<p>Decimal: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>9</mn><msub><mn>0</mn><mn>10</mn></msub><mo>+</mo><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mo></mo><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">-90_{10} + 45_{10} = -45_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">9</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Binary: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1010011</mn><msub><mn>0</mn><mn>2</mn></msub><mo>+</mo><mn>0010110</mn><msub><mn>1</mn><mn>2</mn></msub><mo>=</mo><mn>01101001</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">10100110_{2} + 00101101_{2} = 011010011_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1010011</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0010110</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">01101001</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<h3 id="example-4">Example 4:</h3>
<p>Decimal: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>9</mn><msub><mn>0</mn><mn>10</mn></msub><mo>+</mo><mo></mo><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mn>4</mn><msub><mn>5</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">90_{10} + -45_{10} = 45_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">9</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">4</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Binary: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0101101</mn><msub><mn>0</mn><mn>2</mn></msub><mo>+</mo><mn>1101001</mn><msub><mn>1</mn><mn>2</mn></msub><mo>=</mo><mn>10010110</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">01011010_{2} + 11010011_{2} = 100101101_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0101101</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1101001</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">10010110</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>A chart showing the relationship between true sum and actual (overflowed) sum.
Actual sum has a possible value between 127 to -128.
A true sum, however has a range between 255 and -256.
As your true sum goes further down from -128, its actual sum becomes lower as well. Starting at -129 = 127.
As you true sum goes futher up from 127, the actual sum also rises, satrting with 128 = -128.</p>
<h2 id="visualizing-signed-addition-overflow-w--4">Visualizing signed addition overflow (w = 4)</h2>
<p>A 3D chart which has its x axis go from -8 to +7, its z axis go from -8 to +6, and a y axis which goes from :wq</p>
<p>Positive Overflow</p>
<p>For example: 7 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>011</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">0111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) + 1 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>000</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">0001_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">000</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) = 8 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>100</mn><msub><mn>0</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">1000_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">100</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) is the true sum and = -8 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>100</mn><msub><mn>0</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">1000_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">100</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) is the actual sum</p>
<h2 id="what-about-subtraction---addition">What about subtraction? -&gt; Addition</h2>
<p>x + (-x) = 0</p>
<ul>
<li>Subtracting a number is equivalent to adding its additive inverse
<ul>
<li>Instead of subtracting a positive number, we could add its negative version:</li>
</ul>
</li>
</ul>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>107</mn><mo></mo><mn>118</mn><mo>=</mo><mo></mo><mn>11</mn></mrow><annotation encoding="application/x-tex">
107 - 118 = -11
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">7</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord">1</span><span class="mord">8</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord"></span><span class="mord">1</span><span class="mord">1</span></span></span></span></span></p>
<p>becomes:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>107</mn><mo>+</mo><mo stretchy="false">(</mo><mo></mo><mn>118</mn><mo stretchy="false">)</mo><mo>=</mo><mo></mo><mn>18</mn></mrow><annotation encoding="application/x-tex">
107 + (-118) = -18
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">7</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"></span><span class="mord">1</span><span class="mord">1</span><span class="mord">8</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord"></span><span class="mord">1</span><span class="mord">8</span></span></span></span></span></p>
<ul>
<li>Lets try:</li>
</ul>
<p>Decimal:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>10</mn><msub><mn>7</mn><mn>10</mn></msub><mo></mo><mn>11</mn><msub><mn>8</mn><mn>10</mn></msub><mo>=</mo><mo></mo><mn>11</mn></mrow><annotation encoding="application/x-tex">107_{10} - 118_{10} = -11</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">10</span><span class="mord"><span class="mord">7</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">11</span><span class="mord"><span class="mord">8</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord"></span><span class="mord">11</span></span></span></span></span>
<p>Binary:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>0110101</mn><msub><mn>1</mn><mn>2</mn></msub><mo></mo><mn>0111011</mn><msub><mn>0</mn><mn>2</mn></msub><mo>=</mo></mrow><annotation encoding="application/x-tex">01101011_{2} - 01110110_{2} =</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0110101</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0111011</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span></span></span></span></span>
<p>Binary subtraction by addition:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>0110101</mn><msub><mn>1</mn><mn>2</mn></msub><mo>+</mo><mn>1000101</mn><msub><mn>0</mn><mn>2</mn></msub><mo>=</mo><mn>1111010</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">01101011_{2} + 10001010_{2} = 11110101_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0110101</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1000101</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1111010</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
<p>Binary subtraction by addition is equal to -11</p>
<p>Check: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>128</mn><mo>+</mo><mn>64</mn><mo>+</mo><mn>32</mn><mo>+</mo><mn>16</mn><mo>+</mo><mn>4</mn><mo>+</mo><mn>1</mn><mo>=</mo><mo></mo><mn>1</mn><msub><mn>1</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">-128 + 64 + 32 + 16 + 4 + 1 = -11_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord"></span><span class="mord">128</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">64</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">32</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">16</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"></span><span class="mord">1</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>T2B(X) conversion:</p>
<ol>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo stretchy="false">(</mo><mtext> </mtext><mo stretchy="false">(</mo><mtext>U2B</mtext><mo stretchy="false">(</mo><mi mathvariant="normal"></mi><mi>X</mi><mi mathvariant="normal"></mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">(~(\text{U2B}(|X|)))+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mspace nobreak"> </span><span class="mopen">(</span><span class="mord text"><span class="mord">U2B</span></span><span class="mopen">(</span><span class="mord"></span><span class="mord mathnormal" style="margin-right:0.07847em;">X</span><span class="mord"></span><span class="mclose">)))</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo stretchy="false">(</mo><mtext> </mtext><mo stretchy="false">(</mo><mtext>U2B</mtext><mo stretchy="false">(</mo><mi mathvariant="normal"></mi><mo></mo><mn>118</mn><mi mathvariant="normal"></mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">(~(\text{U2B}(|-118|)))+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mspace nobreak"> </span><span class="mopen">(</span><span class="mord text"><span class="mord">U2B</span></span><span class="mopen">(</span><span class="mord"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">118</span><span class="mclose">)))</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo stretchy="false">(</mo><mtext> </mtext><mo stretchy="false">(</mo><mtext>U2B</mtext><mo stretchy="false">(</mo><mn>118</mn><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">(~(\text{U2B}(118)))+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mspace nobreak"> </span><span class="mopen">(</span><span class="mord text"><span class="mord">U2B</span></span><span class="mopen">(</span><span class="mord">118</span><span class="mclose">)))</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo stretchy="false">(</mo><mo></mo><mo stretchy="false">(</mo><mn>0111011</mn><msub><mn>0</mn><mn>2</mn></msub><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">(\sim(01110110_{2}))+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">0111011</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">))</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo stretchy="false">(</mo><mn>1000100</mn><msub><mn>1</mn><mn>2</mn></msub><mo stretchy="false">)</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">(10001001_{2})+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1000100</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>1000101</mn><msub><mn>0</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">10001010_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1000101</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
</ol>
<h2 id="multiplication-u_w-t_w-and-overflow">Multiplication (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mo></mo><mi>w</mi><mi>u</mi></msubsup><mo separator="true">,</mo><msubsup><mo></mo><mi>w</mi><mi>t</mi></msubsup></mrow><annotation encoding="application/x-tex">*^{u}_{w}, *^{t}_{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.040556em;vertical-align:-0.247em;"></span><span class="mord"><span class="mbin"></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">u</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mbin"></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.7935559999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span></span></span></span>) and overflow</h2>
<p>True product would be the result of integer multiplication with unlimited space: expected product
Actual product is the result of multiplication with limited space.</p>
<ul>
<li>Operands: w bits</li>
<li>True Product: 2w bits <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mo>×</mo><mi>v</mi></mrow><annotation encoding="application/x-tex">u \times v</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span></span></span></span></li>
<li>
<p>Discard: w bits</p>
</li>
<li>Discarding high order w bits has same effect as applying modular arithmetic</li>
</ul>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>p</mi><mo>=</mo><mi>u</mi><msubsup><mo></mo><mi>w</mi><mi>u</mi></msubsup><mi>v</mi><mo>=</mo><mo stretchy="false">(</mo><mi>u</mi><mo>×</mo><mi>v</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="1em"/><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup></mrow><annotation encoding="application/x-tex">p = u *^{u}_{w}v = (u \times v) \mod 2^{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal">p</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9613919999999999em;vertical-align:-0.247em;"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"><span class="mbin"></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">u</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:1em;"></span></span><span class="base"><span class="strut" style="height:0.7143919999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">mod</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>p</mi><mo>=</mo><mi>u</mi><msubsup><mo></mo><mi>w</mi><mi>t</mi></msubsup><mi>v</mi><mo>=</mo><msub><mtext>U2T</mtext><mi>w</mi></msub><mo stretchy="false">[</mo><mo stretchy="false">(</mo><mi>u</mi><mo>×</mo><mi>v</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="1em"/><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">p = u *^{t}_{w}v = \text{U2T}_{w}[(u \times v) \mod 2^{w}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal">p</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0905559999999999em;vertical-align:-0.247em;"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"><span class="mbin"></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8435559999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord text"><span class="mord">U2T</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">[(</span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:1em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">mod</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></span>
<ul>
<li>Example: w = 4</li>
</ul>
<p>Decimal:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mn>5</mn><mn>10</mn></msub><mo>×</mo><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><mn>2</mn><msub><mn>5</mn><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">5_{10} \times 5_{10} = 25_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">2</span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
<p>Binary:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>010</mn><msub><mn>1</mn><mn>2</mn></msub><mo>×</mo><mn>010</mn><msub><mn>1</mn><mn>2</mn></msub><mo>=</mo><menclose notation="horizontalstrike"><mn>001</mn></menclose><mn>100</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">0101_{2} \times 0101_{2} = \sout{001}1001_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">010</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">010</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.64444em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">001</span></span></span><span style="top:-3.2155em;"><span class="pstrut" style="height:3em;"></span><span class="stretchy sout"></span></span></span></span></span></span><span class="mord">100</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
<h2 id="multiplication-with-power-of-2-versus-shifting">Multiplication with power-of-2 versus shifting</h2>
<ul>
<li>If <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>×</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">x \times y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span> where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi><mo>=</mo><msup><mn>2</mn><mi>k</mi></msup></mrow><annotation encoding="application/x-tex">y = 2^{k}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.849108em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.849108em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span></span></span></span></span></span></span></span></span></span></span></span> then <code class="language-plaintext highlighter-rouge">x &lt;&lt; k</code>
<ul>
<li>For both signed and unsigned</li>
</ul>
</li>
<li>Example:
<ul>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>×</mo><mn>8</mn><mo>=</mo><mi>x</mi><mo>×</mo><msup><mn>2</mn><mn>3</mn></msup></mrow><annotation encoding="application/x-tex">x \times 8 = x \times 2^{3}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">8</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">3</span></span></span></span></span></span></span></span></span></span></span></span> = <code class="language-plaintext highlighter-rouge">x &lt;&lt; 3</code></li>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>×</mo><mn>24</mn><mo>=</mo><mo stretchy="false">(</mo><mi>x</mi><mo>×</mo><mn>25</mn><mo stretchy="false">)</mo><mtext></mtext><mo stretchy="false">(</mo><mi>x</mi><mo>×</mo><mn>23</mn><mo stretchy="false">)</mo><mo>=</mo><mo stretchy="false">(</mo><mi>x</mi><mo>×</mo><mn>32</mn><mo stretchy="false">)</mo><mtext></mtext><mo stretchy="false">(</mo><mi>x</mi><mo>×</mo><mn>8</mn><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">x \times 24 = (x \times 25) (x \times 23) = (x \times 32) (x \times 8)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">24</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">25</span><span class="mclose">)</span><span class="mord"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">23</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">32</span><span class="mclose">)</span><span class="mord"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">8</span><span class="mclose">)</span></span></span></span> = (x « 5) (x « 3) (decompose 24 in powers of 2 =&gt; 32 8)</li>
</ul>
</li>
<li>Most machines shift and add faster than multiply
<ul>
<li>Well soon see that compiler generates this code automatically
17</li>
</ul>
</li>
</ul>
<h2 id="summary">Summary</h2>
<ul>
<li>Demo of size and sign conversion in C: code and results posted!</li>
<li>Addition:
<ul>
<li>Unsigned/signed:
<ul>
<li>Behave the same way at the bit level</li>
<li>Interpretation of resulting bit vector (sum) may differ</li>
</ul>
</li>
<li>Unsigned addition -&gt; may overflow, i.e., (w+1)th bit is set
<ul>
<li>If so, then actual sum obtained =&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mi>y</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="0.6666666666666666em"/><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup></mrow><annotation encoding="application/x-tex">(x + y) \mod 2^{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:0.6666666666666666em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">mod</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span></span></span></span></li>
</ul>
</li>
<li>Signed addition -&gt; may overflow, i.e., (w+1)th bit is set
<ul>
<li>If so, then true sum may be too +ve -&gt; positive overflow OR too -ve -&gt; negative overflow</li>
<li>Then actual sum obtained =&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>U2T</mtext><mi>w</mi></msub><mo stretchy="false">[</mo><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mi>y</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="0.6666666666666666em"/><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">\text{U2T}_{w}[(x + y) \mod 2^{w}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord text"><span class="mord">U2T</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">[(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:0.6666666666666666em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">mod</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></li>
</ul>
</li>
</ul>
</li>
<li>Subtraction
<ul>
<li>Becomes an addition where negative operands are transformed into their additive inverse (in twos complement)</li>
</ul>
</li>
<li>Multiplication:
<ul>
<li>Unsigned: actual product obtained -&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>×</mo><mi>y</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="0.6666666666666666em"/><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup></mrow><annotation encoding="application/x-tex">(x \times y) \mod 2^{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:0.6666666666666666em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">mod</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span></span></span></span></li>
<li>Signed: actual product obtained -&gt; $$\text{U2T}_{w}[(x \times y) \mod 2^{w}]</li>
<li>Can be replaced by additions and shifts</li>
</ul>
</li>
</ul>
<h2 id="next-lecture">Next lecture</h2>
<ul>
<li>Representing data in memory Most of this is review
<ul>
<li>“Under the Hood” - Von Neumann architecture</li>
<li>Bits and bytes in memory
<ul>
<li>How to diagram memory -&gt; Used in this course and other references</li>
<li>How to represent series of bits -&gt; In binary, in hexadecimal (conversion)</li>
<li>What kind of information (data) do series of bits represent -&gt; Encoding scheme</li>
<li>Order of bytes in memory -&gt; Endian</li>
</ul>
</li>
<li>Bit manipulation bitwise operations
<ul>
<li>Boolean algebra + Shifting</li>
</ul>
</li>
</ul>
</li>
<li>Representing integral numbers in memory
<ul>
<li>Unsigned and signed</li>
<li>Converting, expanding and truncating</li>
<li>Arithmetic operations</li>
</ul>
</li>
<li>Representing real numbers in memory
<ul>
<li>IEEE floating point representation</li>
<li>Floating point in C casting, rounding, addition, …</li>
</ul>
</li>
</ul>
<p>Well illustrate what we covered today by having a demo!</p>
<footer>
</footer>
</div>
</main>
</body>
</html>

@ -1,401 +0,0 @@
---
layout: simple
math: true
---
# CMPT 295
Unit - Data Representation
Lecture 4 Representing integral numbers in memory Arithmetic operations
## Warm up question
* What is the value of …
* TMin (in hex) for signed char in C: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
* TMax (in hex) for signed int in C: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
* TMin (in hex) for signed short in C: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
## Last Lecture
* Interpretation of bit pattern B into either unsigned value U or signed value T
* B2U(X) and U2B(X) encoding schemes (conversion)
* B2T(X) and T2B(X) encoding schemes (conversion)
* Signed value expressed as twos complement => T
* Conversions from unsigned <-> signed values
* U2T(X) and T2U(X) => adding or subtracting 2w
* Implication in C: when converting (implicitly via promotion and explicitly via casting):
* Sign:
* Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may be interpreted differently
* Can have unexpected effects -> producing a different value
* Size:
* Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int)
* sign extension: For unsigned -> zeros extension, for signed -> sign bit extension
* Both yield expected result > resulting value unchanged
* Large -> small (for signed, e.g., int to short and for unsigned, e.g., unsigned int to unsigned short)
* truncation: Unsigned/signed -> most significant bits are truncated (discarded)
* May not yield expected results -> original value may be altered
* Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
## Todays Menu
* Representing data in memory Most of this is review
* “Under the Hood” - Von Neumann architecture
* Bits and bytes in memory
* How to diagram memory -> Used in this course and other references
* How to represent series of bits -> In binary, in hexadecimal (conversion)
* What kind of information (data) do series of bits represent -> Encoding scheme
* Order of bytes in memory -> Endian
* Bit manipulation bitwise operations
* Boolean algebra + Shifting
* Representing integral numbers in memory
* Unsigned and signed
* Converting, expanding and truncating
* Arithmetic operations
* Representing real numbers in memory
* IEEE floating point representation
* Floating point in C casting, rounding, addition, …
Lets first illustrate what we covered last lecture with a demo!
## Demo Looking at size and sign conversions in C
* What does the demo illustrate?
* Size conversion:
* Converting to a larger (wider) data type -> Converting short to int
* Converting to a smaller (narrower) data type -> Converting short to char
* Sign conversion:
* Converting from signed to unsigned -> Converting short to unsigned short
* Converting from unsigned to signed -> Converting unsigned short to short
* Size and Sign conversion:
* Converting from signed to unsigned larger (wider) data type -> Converting short to unsigned int
* Converting from signed to unsigned smaller (narrower) data type ->
Converting short to unsigned char
* This demo (code and results) posted on our course web site
## Integer addition (unlimited space)
* What happens when we add two decimal numbers? (Highlights show carring the one to complete the equation)
{% katex display %}
107_{10} + 938_{10} = 1045_{10}{% endkatex %}
* Same thing happens when we add two binary numbers:
{% katex display %}
101100_{2} + 101110_{2} = 1011010_{2}
{% endkatex %}
## Unsigned addition (limited space, i.e., fixed size in memory)
What happens when we add two unsigned values?
$$w=8$$
a)
Binary:
{% katex display %}
\mathit{00111011}_{2} + \mathit{01011010}_{2} = ?
{% endkatex %}
Decimal:
{% katex display %}
\mathit{59}_{10} + {90}_{10} = {149}_{10}
{% endkatex %}
b)
Binary:
{% katex display %}
\mathit{10101110}_{2} + \mathit{11001011}_{2} = ?
{% endkatex %}
Decimal:
{% katex display %}
174_{10} + 203_{10} = 377_{10}
{% endkatex %}
## Unsigned addition ($$+^{u}_{w}$$) and overflow
* True/expected sum is the result of integer addition with unlimited space.
* Actual sum is the result of unsigned addition with limited space. Discarding the carry out bit.
* Discarding carry out bit has same effect as applying modular arithmetic
{% katex display %}
s = U+^{u}_{w}v = (u + v) \mod{2^{w}}
{% endkatex %}
* Operands: w bits
* True Sum: w+1 bits
## Closer look at unsigned addition overflow
* w = 8 -> [0..255]
* $$255_{10} = 11111111_{2}$$
* $$90_{10} = 01011010_{2}$$
* $$45_{10} = 00101101_{2}$$
### Example 1:
Decimal (carry out the 1 in 135):
{% katex display %}
90_{10} + 45_{10} = 135_{10}
{% endkatex %}
Binary (carry out the 1 in $$10000111_{2}$$):
{% katex display %}
01011010_{2} + 00101101_{2} = 10000111_{2}
{% endkatex %}
* True sum, w=8: $$10000111_{2}$$
* Actual (overflowed) sum, w=8: $$10000111_{2} = 135_{10}$$
### Example 2:
Decimal (carry 1 to the 3 in 300):
{% katex display %}
255_{10} + 45_{10} = 300_{10}
{% endkatex %}
Binary (carry the 1 at the beginning of the result):
{% katex display %}
11111111_{2} + 00101101_{2} = 100101100_{2}
{% endkatex %}
* True sum, w=9: $$100101100_{2} = 300_{10}$$
* Actual (overflowed) sum, w=8: $$00101100_{2} = 44_{10}$$
## Comparing integer addition with Overflow: Effect of unsigned addition (w = 4)
Annotation: with unlimited space:
A 3d chart showing two numbers being added.
a and b on the z and x axies, the sum on the y axis.
y goes to a maximum height of 32
(a = 15) + (b = 15) = (y = 30)
Annotation: With limited space (fixed-size memory):
A 3d chart showing two numbers being added.
a and b on the z and x axies, the sum on the y axis.
y goes to a maximum height of 15
(a = 15) + (b = 15) = (y = 14)
An overflow occurs when there is a carry out
For example: 15 ($$1111_{2}$$) + 15 ($$1111_{2}$$) = 30 ($$11110_{2}$$) as a true sum, and 14 ($$11110_{2}$$) as an actual sum.
## Signed addition (limited space, i.e., fixed size in memory)
What happens when we add two signed values:
w=8
a)
Binary:
$$00111011_{2} + 01011010_{2} = ?$$
Decimal:
$$59_{10} + 90_{10} = 149_{10}$$
b)
Binary: $$10101110_{2} + 11001011_{2} = ?$$
Decimal: $$-82_{10} + -53_{10} = -135_{10}$$
Observation: Unsigned and signed additions have identical behavior @ the bit level, i.e., their sum have the same bit-level representation, but their interpretation differs
## Signed addition ($$+^{t}_{w}$$) and overflow
True sum would be the result of integer addition with unlimited space:
Actual sum is the result of signed addition with limited space:
Operands: w bits
True Sum: w+1 bits
After discarduing carry out bit: w bits (overflow)
* Discarding carry out bit has same effect as applying modular arithmetic
{% katex display %}
s = u +^{t}_{w} v = \text{U2T}_{w}[(u + v) \mod 2^{w}]
{% endkatex %}
Negative overflow and positive overflows are possible.
Diagram showing negative overflows becoming positive, and positive overflows becoming negative.
## Closer look at signed addition overflow
* w = 8 -> [-128..127]
* $$90_{10} = 01011010_{2}$$
* $$45_{10} = 00101101_{2}$$
* $$-45_{10} = 11010011_{2}$$
* $$-90_{10} = 10100110_{2}$$
### Example 1:
Decimal: $$90_{10} + 45_{10} = 135_{10}$$
Binary: $$01011010_{2} + 00101101_{2} = 010000111_{2}$$
The binary result is -121
### Example 2:
Decimal: $$-90_{10} + -45_{10} = -135_{10}$$
Binary: $$10100110_{2} + 11010011_{2} = 101111001_{2}$$
Binary result is 121
### Example 3:
Decimal: $$-90_{10} + 45_{10} = -45_{10}$$
Binary: $$10100110_{2} + 00101101_{2} = 011010011_{2}$$
### Example 4:
Decimal: $$90_{10} + -45_{10} = 45_{10}$$
Binary: $$01011010_{2} + 11010011_{2} = 100101101_{2}$$
A chart showing the relationship between true sum and actual (overflowed) sum.
Actual sum has a possible value between 127 to -128.
A true sum, however has a range between 255 and -256.
As your true sum goes further down from -128, its actual sum becomes lower as well. Starting at -129 = 127.
As you true sum goes futher up from 127, the actual sum also rises, satrting with 128 = -128.
## Visualizing signed addition overflow (w = 4)
A 3D chart which has its x axis go from -8 to +7, its z axis go from -8 to +6, and a y axis which goes from :wq
Positive Overflow
For example: 7 ($$0111_{2}$$) + 1 ($$0001_{2}$$) = 8 ($$1000_{2}$$) is the true sum and = -8 ($$1000_{2}$$) is the actual sum
## What about subtraction? -> Addition
x + (-x) = 0
* Subtracting a number is equivalent to adding its additive inverse
* Instead of subtracting a positive number, we could add its negative version:
{% katex display %}
107 - 118 = -11
{% endkatex %}
becomes:
{% katex display %}
107 + (-118) = -18
{% endkatex %}
* Lets try:
Decimal:
$$107_{10} - 118_{10} = -11$$
Binary:
$$01101011_{2} - 01110110_{2} = $$
Binary subtraction by addition:
$$01101011_{2} + 10001010_{2} = 11110101_{2}$$
Binary subtraction by addition is equal to -11
Check: $$-128 + 64 + 32 + 16 + 4 + 1 = -11_{10}$$
T2B(X) conversion:
1. $$(~(\text{U2B}(|X|)))+1$$
2. $$(~(\text{U2B}(|-118|)))+1$$
3. $$(~(\text{U2B}(118)))+1$$
4. $$(\sim(01110110_{2}))+1$$
5. $$(10001001_{2})+1$$
6. $$10001010_{2}$$
## Multiplication ($$*^{u}_{w}, *^{t}_{w}$$) and overflow
True product would be the result of integer multiplication with unlimited space: expected product
Actual product is the result of multiplication with limited space.
* Operands: w bits
* True Product: 2w bits $$u \times v$$
* Discard: w bits
* Discarding high order w bits has same effect as applying modular arithmetic
$$p = u *^{u}_{w}v = (u \times v) \mod 2^{w}$$
$$p = u *^{t}_{w}v = \text{U2T}_{w}[(u \times v) \mod 2^{w}]$$
* Example: w = 4
Decimal:
$$ 5_{10} \times 5_{10} = 25_{10} $$
Binary:
$$ 0101_{2} \times 0101_{2} = \sout{001}1001_{2} $$
## Multiplication with power-of-2 versus shifting
* If $$x \times y$$ where $$y = 2^{k}$$ then `x << k`
* For both signed and unsigned
* Example:
* $$x \times 8 = x \times 2^{3}$$ = `x << 3`
* $$x \times 24 = (x \times 25) (x \times 23) = (x \times 32) (x \times 8)$$ = (x << 5) (x << 3) (decompose 24 in powers of 2 => 32 8)
* Most machines shift and add faster than multiply
* Well soon see that compiler generates this code automatically
17
## Summary
* Demo of size and sign conversion in C: code and results posted!
* Addition:
* Unsigned/signed:
* Behave the same way at the bit level
* Interpretation of resulting bit vector (sum) may differ
* Unsigned addition -> may overflow, i.e., (w+1)th bit is set
* If so, then actual sum obtained => $$(x + y) \mod 2^{w}$$
* Signed addition -> may overflow, i.e., (w+1)th bit is set
* If so, then true sum may be too +ve -> positive overflow OR too -ve -> negative overflow
* Then actual sum obtained => $$\text{U2T}_{w}[(x + y) \mod 2^{w}]$$
* Subtraction
* Becomes an addition where negative operands are transformed into their additive inverse (in twos complement)
* Multiplication:
* Unsigned: actual product obtained -> $$(x \times y) \mod 2^{w}$$
* Signed: actual product obtained -> $$\text{U2T}_{w}[(x \times y) \mod 2^{w}]
* Can be replaced by additions and shifts
## Next lecture
* Representing data in memory Most of this is review
* “Under the Hood” - Von Neumann architecture
* Bits and bytes in memory
* How to diagram memory -> Used in this course and other references
* How to represent series of bits -> In binary, in hexadecimal (conversion)
* What kind of information (data) do series of bits represent -> Encoding scheme
* Order of bytes in memory -> Endian
* Bit manipulation bitwise operations
* Boolean algebra + Shifting
* Representing integral numbers in memory
* Unsigned and signed
* Converting, expanding and truncating
* Arithmetic operations
* Representing real numbers in memory
* IEEE floating point representation
* Floating point in C casting, rounding, addition, …
Well illustrate what we covered today by having a demo!

@ -1,97 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, size_t len) {
int i;
for ( i = len-1; i >= 0; i--)
printf(" %.2x", start[i]);
return;
}
int main(int argc, char *argv[]) {
if ( argc < 2 ) {
printf("Forgot the argment! Try again!\n");
return 1;
}
short aShort = atoi(argv[1]);
printf("\nAs a short data type, the variable aShort has the value %hd (hex: 0x", aShort);
show_bytes((byte_pointer) &aShort, sizeof(short));
printf(")\n\n");
/* Convert - size - implicitly */
printf("Converting SIZE implicitly: short -> integer *** Sign extension ***\n");
printf("This is done by issuing the statement: int anInt = aShort;\n");
int anInt = aShort;
printf("As a int data type, the variable anInt has the value %d (hex: 0x", anInt);
show_bytes((byte_pointer) &anInt, sizeof(int));
printf(")\n\n");
/* Convert - size - implicitly */
printf("Converting SIZE implicitly: short -> char *** Truncation ***\n");
printf("This is done by issuing the statement: signed char aChar = aShort;\n");
signed char aChar = aShort;
printf("As a char data type, the variable aChar has the value %hhi (hex: 0x", aChar);
show_bytes((byte_pointer) &aChar, sizeof(signed char));
printf(")\n\n");
/* Convert - sign - implicitly*/
printf("Converting SIGN implicitly: short -> unsigned short\n");
printf("This is done by issuing the statement: unsigned short aUShort = aShort;\n");
unsigned short aUShort = aShort;
printf("As an unsigned short data type, the variable aUShort has the value %hu (hex: 0x", aUShort);
show_bytes((byte_pointer) &aUShort, sizeof(unsigned short));
printf(")\n\n");
/* Convert - sign - implicitly*/
printf("Converting SIGN implicitly: unsigned short -> short\n");
printf("This is done by issuing the statement: short aShort1 = aUShort;\n");
short aShort1 = aUShort;
printf("As a signed short data type, the variable aShort1 has the value %hi (hex: 0x", (signed short) aShort1);
show_bytes((byte_pointer) &aShort1, sizeof(signed short));
printf(")\n\n");
/* Convert - both: 1) size, 2) sign */
printf("Converting both SIZE and SIGN: short -> unsigned int\n");
printf("This is done by issuing the statement: unsigned aUInt = aShort;\n");
unsigned aUInt = aShort;
printf("As an unsigned int data type, the variable aUInt has the value %u (hex: 0x", aUInt);
show_bytes((byte_pointer) &aUInt, sizeof(unsigned));
printf(")\n\n");
/* One step at a time */
printf("One step at a time - First conversion is SIZE: (int) aShort = %d\n", (int) aShort);
printf("One step at a time - Second conversion is SIGN: (unsigned) (int) aShort = %u\n\n", (unsigned) (int) aShort);
/* Reverse the process and see what happens ... */
printf("What if ... First conversion is SIGN: (unsigned short) aShort = %hu\n", (unsigned short) aShort);
printf("What if ... Second conversion is SIZE: (unsigned int) (unsigned short) aShort = %d\n\n", (unsigned int) (unsigned short) aShort);
/* Convert - both: 1) size, 2) sign */
printf("Converting both SIZE and SIGN: short -> unsigned char\n");
printf("This is done by issuing the statement: unsigned char anUChar = aShort;\n");
unsigned char anUChar = aShort;
printf("As an unsigned char data type, the variable anUChar has the value %hhu (hex: 0x", anUChar);
show_bytes((byte_pointer) &anUChar, sizeof(unsigned char));
printf(")\n\n");
/* One step at a time */
printf("One step at a time - First conversion is SIZE: (signed char) aShort = %hhi\n", (signed char) aShort);
printf("One step at a time - Second conversion is SIGN: (unsigned char) (signed char) aShort = %hhu\n\n", (unsigned char) (signed char) aShort);
/* Reverse the process and see what happens ... */
printf("What if ... First conversion is SIGN: (unsigned short) aShort = %hu\n", (unsigned short) aShort);
printf("What if ... Second conversion is SIZE: (unsigned char) (unsigned short) aShort = %hhu\n\n", (unsigned char) (unsigned short) aShort);
return 0;
}

@ -1,81 +0,0 @@
$ ./Demo 12345
As a short data type, the variable aShort has the value 12345 (hex: 0x 30 39)
Converting SIZE implicitly: short -> integer *** Sign extension ***
This is done by issuing the statement: int anInt = aShort;
As a int data type, the variable anInt has the value 12345 (hex: 0x 00 00 30 39)
Converting SIZE implicitly: short -> char *** Truncation ***
This is done by issuing the statement: signed char aChar = aShort;
As a char data type, the variable aChar has the value 57 (hex: 0x 39)
Converting SIGN implicitly: short -> unsigned short
This is done by issuing the statement: unsigned short aUShort = aShort;
As an unsigned short data type, the variable aUShort has the value 12345 (hex: 0x 30 39)
Converting SIGN implicitly: unsigned short -> short
This is done by issuing the statement: short aShort1 = aUShort;
As a signed short data type, the variable aShort1 has the value 12345 (hex: 0x 30 39)
Converting both SIZE and SIGN: short -> unsigned int
This is done by issuing the statement: unsigned aUInt = aShort;
As an unsigned int data type, the variable aUInt has the value 12345 (hex: 0x 00 00 30 39)
One step at a time - First conversion is SIZE: (int) aShort = 12345
One step at a time - Second conversion is SIGN: (unsigned) (int) aShort = 12345
What if ... First conversion is SIGN: (unsigned short) aShort = 12345
What if ... Second conversion is SIZE: (unsigned int) (unsigned short) aShort = 12345
Converting both SIZE and SIGN: short -> unsigned char
This is done by issuing the statement: unsigned char anUChar = aShort;
As an unsigned char data type, the variable anUChar has the value 57 (hex: 0x 39)
One step at a time - First conversion is SIZE: (signed char) aShort = 57
One step at a time - Second conversion is SIGN: (unsigned char) (signed char) aShort = 57
What if ... First conversion is SIGN: (unsigned short) aShort = 12345
What if ... Second conversion is SIZE: (unsigned char) (unsigned short) aShort = 57
----------------------------------------------------------------------------------------------
$ ./Demo -12345
As a short data type, the variable aShort has the value -12345 (hex: 0x cf c7)
Converting SIZE implicitly: short -> integer *** Sign extension ***
This is done by issuing the statement: int anInt = aShort;
As a int data type, the variable anInt has the value -12345 (hex: 0x ff ff cf c7)
Converting SIZE implicitly: short -> char *** Truncation ***
This is done by issuing the statement: signed char aChar = aShort;
As a char data type, the variable aChar has the value -57 (hex: 0x c7)
Converting SIGN implicitly: short -> unsigned short
This is done by issuing the statement: unsigned short aUShort = aShort;
As an unsigned short data type, the variable aUShort has the value 53191 (hex: 0x cf c7)
Converting SIGN implicitly: unsigned short -> short
This is done by issuing the statement: short aShort1 = aUShort;
As a signed short data type, the variable aShort1 has the value -12345 (hex: 0x cf c7)
Converting both SIZE and SIGN: short -> unsigned int
This is done by issuing the statement: unsigned aUInt = aShort;
As an unsigned int data type, the variable aUInt has the value 4294954951 (hex: 0x ff ff cf c7)
One step at a time - First conversion is SIZE: (int) aShort = -12345
One step at a time - Second conversion is SIGN: (unsigned) (int) aShort = 4294954951
What if ... First conversion is SIGN: (unsigned short) aShort = 53191
What if ... Second conversion is SIZE: (unsigned int) (unsigned short) aShort = 53191
Converting both SIZE and SIGN: short -> unsigned char
This is done by issuing the statement: unsigned char anUChar = aShort;
As an unsigned char data type, the variable anUChar has the value 199 (hex: 0x c7)
One step at a time - First conversion is SIZE: (signed char) aShort = -57
One step at a time - Second conversion is SIGN: (unsigned char) (signed char) aShort = 199
What if ... First conversion is SIGN: (unsigned short) aShort = 53191
What if ... Second conversion is SIZE: (unsigned char) (unsigned short) aShort = 199

File diff suppressed because one or more lines are too long

@ -1,324 +0,0 @@
---
layout: simple
math: true
---
# CMPT 295: Unit - Data Representation
## Lecture 5
* Representing fractional numbers in memory
* IEEE floating point representation
## Last Lecture
* Demo of size and sign conversion in C: code and results posted!
* Addition:
* Unsigned/signed:
* Behave the same way at the bit level
* Interpretation of resulting bit vector (sum) may differ
* Unsigned addition -> true sum may overflow its w bits in memory (annotation with diagram of unisnged overflows, see lecture 04 to see full description of diagram)
* If so, then actual sum = $$(x + y) \mod 2^{w}$$ (equivalent to subtracting $$2^{w}$$ from true sum $$(x + y)$$)
* Signed addition -> true sum may overflow its w bits in memory (annotation attached displaying diagram of negative and positive overflows, see Lecture 04 for detailed description of diagram)
* If so then …
* actual sum = $$\text{U2T}_{w}[(x + y) \mod 2^{w}]$$
* true sum may be too +ve -> positive overflow OR too ve -> negative overflow
* Subtraction
* Becomes an addition where the 2nd operand is transformed into its additive inverse in twos complement
* Multiplication:
* Unsigned: actual product = $$(x \times y) \mod 2^{w}$$
* Signed: actual product = $$\text{U2T}_{w}[(x \times y) \mod 2^{w}]$$ <!--_ -->
* Can be replaced by additions and shifts
Conclusion: the same bit pattern is interpreted differently.
## Questions
* Why are we learning this?
* What can we do in our program when we suspect that overflow may occur?
## Demo Looking at integer additions in C
* What does the demo illustrate?
* Unsigned addition
* Without overflow
* With overflow
* Can overflow be predicted?
* Signed addition
* Without overflow
* With positive overflow and negative overflow
* Can overflow be predicted?
* This demo (code and results) posted on our course web site
4
## Todays Menu
* (greyed out) Representing data in memory Most of this is review
* (greyed out) “Under the Hood” - Von Neumann architecture
* (greyed out) Bits and bytes in memory
* (greyed out) How to diagram memory -> Used in this course and other references
* (greyed out) How to represent series of bits -> In binary, in hexadecimal (conversion)
* (greyed out) What kind of information (data) do series of bits represent -> Encoding scheme
* (greyed out) Order of bytes in memory -> Endian
* (greyed out) Bit manipulation bitwise operations
* (greyed out) Boolean algebra + Shifting
* (greyed out) Representing integral numbers in memory
* (greyed out) Unsigned and signed
* (greyed out) Converting, expanding and truncating
* (greyed out) Arithmetic operations
* (highlighted) Representing real numbers in memory
* (highlighted) IEEE floating point representation
* (greyed out) Floating point in C casting, rounding, addition, …
Well illustrate what we covered today by having a demo!
## Converting a fractional decimal number into a binary number (bit vector) [R2B(X)]
* How would 346.625 ($$346 \frac{5}{8}$$) be represented as a binary number?
* Expanding the subtraction method we have already seen:
Starting number: 346.625
Whole:
Value|Attempted Addition (subtraction) value|Result|Binary Implication|Note
346|-256|90|$$1 \times 2^{8}$$|MSb
90|-128|&#9785;|$$0 \times 2^{7}$$|
90|-64|26|$$1 \times 2^{6}$$|
26|-32|&#9785;|$$0 \times 2^{5}$$|
26|-16|10|$$1 \times 2^{4}$$|
10|-8|2|$$1 \times 2^{3}$$|
2|-4|&#9785;|$$0 \times 2^{2}$$|
2|-2|0|$$1 \times 2^{1}$$|
0|-1|&#9785;|$$0 \times 2^{1}$$|LSb
Fractional:
Value|Attempted addition (subtraction) value|result|binary implication|notes
.625|-0.5|.125|$$1 \times 2^{-1}$$|MSb
.125|-0.25|&#9785;|$$0 \times 2^{-2}$$|
.125|-0.125|0|$$1 \times 2^{-3}$$|LSb
Binary representation is: $$\text{101011010.101}_{2}$$
First, last binary digit *before* the period are MSb, LSb respectively.
First, last binary digit *after* the period are also MSb, LSb respectively.
Negative Powers of 2:
* $$2^{-1}$$ = 0.5
* $$2^{2}$$ = 0.25
* $$2^{3}$$ = 0.125
* $$2^{4}$$ = 0.0625
* $$2^{5}$$ = 0.03125
## Converting a binary number into a fractional decimal number [R2B(X)]
* How would $$\text{1011.101}_{2}$$ be represented as a fractional decimal number?
## Review: Fractional decimal numbers
Positional notation:
Notation|Value
$$d_{i}$$|$$\text{10}^{i}$$
$$d_{i-1}$$|$$\text{10}^{i-1}$$
...|...
$$d_{2}$$|100
$$d_{1}$$|10
$$d_{0}$$|1
$$d_{-1}$$|$$\frac{1}{10}$$
$$d_{-2}$$|$$\frac{1}{100}$$
$$d_{-3}$$|$$\frac{1}{1000}$$
...|...
$$d_{-j}$$|$$\text{10}^{-j}$$
Example: 2.345
Digit in number|Note
2|$$\text{10}^{0}$$
.|
3|$$\text{10}^{-1}$$
4|$$\text{10}^{-2}$$
5|$$\text{10}^{-3}$$
{% katex display %}
2.345 = 2 \times \text{10}^{0} + 3 \times \text{10}^{1} + 4 \times \text{10}^{2} + 5 \times \text{10}^{3}
{% endkatex %}
## Converting a binary number into a fractional decimal number [B2R(X)]
Positional notation: can this be a possible encoding scheme?
$$b_{i}$$|$$2^{i}$$
$$b_{i-1}$$|$$2^{i-1}$$
...|...
$$b_{2}$$|4
$$b_{1}$$|2
$$b_{0}$$|1
$$b_{-1}$$|$$\frac{1}{2}$$
$$b_{-2}$$|$$\frac{1}{4}$$
$$b_{-3}$$|$$\frac{1}{8}$$
...|...
$$b_{-j}$$|$$2^{-j}$$
## Converting a binary number into a fractional decimal number [B2R(X)]
* How would $$\text{1011.101}_{2}$$ be represented as a fractional decimal number?
* Using the positional encoding scheme:
{% katex display %}
\begin{aligned}
&\text{1011.101}_{2} = \\
&(\text{1011}_{2} = 1 \times 2^{3} + 1 \times 2^{1} + 1 \times 2^{0} = \text{11}_{10}) +\\
&(\text{.101}_{2} = 1 \times 2^{-1} + 1 \times 2^{-3} = 0.5 + 0.125 = \text{0.625}_{10})
\end{aligned}
{% endkatex %}
Result: \_\_\_\_
Negative Powers of 2
$$2^{1}$$|0.5
$$2^{2}$$|0.25
$$2^{3}$$|0.125
$$2^{4}$$|0.0625
$$2^{5}$$|0.03125
$$2^{6}$$|0.015625
$$2^{7}$$|0.0078125
$$2^{8}$$|0.00390625
## Positional notation as encoding scheme?
* One way to answer this question is to investigate whether the encoding scheme allows for arithmetic operations
* Lets see: Using the positional notation as an encoding scheme produces fractional binary numbers that can be
* added
* multiplied by 2 by shifting left
* divided by 2 by shifting right (unsigned)
Example:
Operand|Binary|Fraction|Makeup
|$$\text{1011.101}_{2}$$|$$11\frac{5}{8}$$|$$8 + 2 + 1 + \frac{1}{2} + \frac{1}{8}$$
Divide by 2|$$\text{101.1101}_{2}$$|$$5\frac{13}{16}$$|$$4 + 1 + \frac{1}{2} + \frac{1}{4} + \frac{1}{16}$$
Divide by 2|$$\text{10.11101}_{2}$$|$$2\frac{29}{32}$$|$$2 + \frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \frac{1}{32}$$
Multiply by 4|$$\text{1011.101}_{2}$$|$$11\frac{5}{8}$$|$$8 + 2 + 1 + \frac{1}{2} + \frac{1}{8}$$
Multiply by 2|$$\text{10111.01}_{2}$$|$$23\frac{1}{4}$$|$$16 + 4 + 2 + 1 + \frac{1}{4}$$
So far so good!
## Positional notation as encoding scheme?
* Advantage (so far):
* Straightforward arithmetic: can shift to multiply and divide, convert
* Disadvantage:
* Cannot encode all fractional numbers:
* Can only represent numbers of the form $$x \div 2^{k}$$ (what about $$\frac{1}{5}$$ or -34.8)
* Only one setting of binary point within the w bits -> this limits the range of possible values
* What is this range?
* Example: w = 32 bits and binary point located at 16th bit:
* Whole number range: [0 ... 65535]
* Fraction range: [0 ... 1 - ε]
* $$1 - \text{bits} = \epsilon$$
* Range: [0.0 ... 65535.99998]
Not so good anymore!
## Representing fractional numbers in memory
* Here is another possible encoding scheme: IEEE floating point representation (IEEE Standard 754)
* Overview:
* Binary Numerical Form: $$V = \text{(1)}^{s} M 2^{E}$$
* s Sign bit -> determines whether number is negative or positive
* M Significand (or Mantissa) -> fractional part of number
* E Exponent
* Form of bit pattern (number of items not important, focus on scale): `[s|eee|fffffffffffff]`
* Most significant bit (MSb) s (similar to sign-magnitude encoding)
* exp (e) field encodes E (but is not equal to E)
* frac (f) field encodes M (but is not equal to M)
## IEEE Floating Point Representation -- Precision options
* Single precision: 32 bits ≈ 7 decimal digits, range:10±38 (in C: float)
* In C: diagram of memory showing:
* 1 sign bit
* 8 exp bits
* 23 frac bits
* Double precision: 64 bits ≈ 16 decimal digits, range:10±308 (in C: double)
* In C: diagram of memory showing:
* 1 sign bit
* 11 exp bits
* 52 frac bits
## IEEE Floating Point Representation -- Three “kinds” of values
Numerical Form: $$V = \text{(1)}^{s} M 2^{E}$$
* 1 sign bit
* `k` exp bits
* denormalized: 00...00 (all 0's)
* normalized: exp != 0 and exp != 11..11
* special cases: 11...11 (all 1's)
* `n` frac bits
* E = exp - bias, bias = $$2^{k-1} -1$$
* M = 1 + frac
Why is E biased? Using single precision as an example:
* exp range: [00000001 .. 11111110] and bias = $$2^{8-1} 1$$
* E range: [-126 .. 127]
* If no bias: E range: [1 .. 254] => $$2^{1}$$ to $$2^{254}$$
Why adding 1 to frac? Because number V is first normalized before it is converted.
## Review: Scientific Notation and normalization
* From Wikipedia:
* Scientific notation is a way of expressing numbers that are too large or too small (usually would result a long string of digits) to be conveniently written in decimal form.
* In scientific notation, nonzero numbers are written in the form
* In normalized notation, the exponent n is chosen so that the absolute value of the significand m is at least 1 but less than 10.
* Examples:
* A proton's mass is 0.0000000000000000000000000016726 kg -> $$1.6726 \times \text{10}^{27}$$kg
* Speed of light is 299,792,458 m/s -> $$2.99792,458 \times \text{10}^{8}$$ m/s
Syntax:
Notation|Name
+/|sign
$$d_{0}$$, $$d_{-1}$$, $$d_{-2}$$, $$d_{-3}$$ ... $$d_{-n}$$|significand
&times;|times
b|base
$$^{exp}$$|exponent
Lets try: $$\text{101011010.101}_{2}$$ = \_\_\_\_
## Summary
* Representing integral numbers (signed/unsigned) in memory:
* Encode schemes allow for small range of values exactly
* Representing fractional numbers in memory:
1. Positional notation (advantages and disadvantages)
2. IEEE floating point representation: wider range, mostly approximately
* Overview of IEEE Floating Point representation
* $$V = \text{(-1)}^{s} \times M \times 2^{E}$$
* Precision options
* 3 kinds: normalized, denormalized and special values
## Todays Menu
* Representing data in memory Most of this is review
* “Under the Hood” - Von Neumann architecture
* Bits and bytes in memory
* How to diagram memory -> Used in this course and other references
* How to represent series of bits -> In binary, in hexadecimal (conversion)
* What kind of information (data) do series of bits represent -> Encoding scheme
* Order of bytes in memory -> Endian
* Bit manipulation bitwise operations
* Boolean algebra + Shifting
* Representing integral numbers in memory
* Unsigned and signed
* Converting, expanding and truncating
* Arithmetic operations
* Representing real numbers in memory
18
* IEEE floating point representation
* Floating point in C casting, rounding, addition, …

@ -1,596 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="/assets/css/katex.css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-295">CMPT 295</h1>
<ul>
<li>Unit - Data Representation
<ul>
<li>Lecture 6 Representing fractional numbers in memory</li>
<li>IEEE floating point representation contd</li>
</ul>
</li>
</ul>
<h2 id="have-you-heard-of-that-new-band-1023-megabytes">Have you heard of that new band “1023 Megabytes”?</h2>
<p>Theyre pretty good,
but they dont have a gig just yet.
😭</p>
<h2 id="last-lecture">Last Lecture</h2>
<ul>
<li>Representing integral numbers in memory
<ul>
<li>Can encode a small range of values exactly (in 1, 2, 4, 8 bytes)
<ul>
<li>For example: We can represent the values -128 to 127 exactly in 1 byte using a signed char in C</li>
</ul>
</li>
</ul>
</li>
<li>Representing fractional numbers in memory
<ol>
<li>Positional notation has some advantages, but also disadvantages -&gt; so not used!</li>
<li>IEEE floating point representation: can encode a much larger range of e.g., single precision: [10-38..1038] values approximately (in 4 or 8 bytes)</li>
</ol>
</li>
<li>Overview of IEEE floating point representation
<ul>
<li>Precision options (float 32-bit, double 64-bit)</li>
<li>V = (-1)s x M x 2E</li>
<li>s &gt; sign bit</li>
<li>exp encodes E (but != E)</li>
<li>frac encodes M (but != M)</li>
</ul>
</li>
</ul>
<p>We interpret the bit vector (expressed in IEEE floating point encoding) stored in memory using this equation.</p>
<h2 id="todays-menu">Todays Menu</h2>
<ul>
<li>Representing data in memory Most of this is review
<ul>
<li>“Under the Hood” - Von Neumann architecture</li>
<li>Bits and bytes in memory
<ul>
<li>How to diagram memory -&gt; Used in this course and other references</li>
<li>How to represent series of bits -&gt; In binary, in hexadecimal (conversion)</li>
<li>What kind of information (data) do series of bits represent -&gt; Encoding scheme</li>
<li>Order of bytes in memory -&gt; Endian</li>
</ul>
</li>
<li>Bit manipulation bitwise operations
<ul>
<li>Boolean algebra + Shifting</li>
</ul>
</li>
</ul>
</li>
<li>Representing integral numbers in memory
<ul>
<li>Unsigned and signed</li>
<li>Converting, expanding and truncating</li>
<li>Arithmetic operations</li>
</ul>
</li>
<li>Representing real numbers in memory
4
<ul>
<li>IEEE floating point representation</li>
<li>Floating point in C casting, rounding, addition, …</li>
</ul>
</li>
</ul>
<h2 id="ieee-floating-point-representation-three-kinds-of-values">IEEE Floating Point Representation Three “kinds” of values</h2>
<p>We interpret the bit vector
(expressed in IEEE floating point encoding) stored in memory using this equation:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mo></mo><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">
V = (-1)^{s} M 2^{E}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"></span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathdefault" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>Bit breakdown<code class="language-plaintext highlighter-rouge">exp</code> and <code class="language-plaintext highlighter-rouge">frac</code> interpreted as unsigned:</p>
<ul>
<li>s = 1 bit</li>
<li>exp = k bits
<ol>
<li>If <code class="language-plaintext highlighter-rouge">exp</code> != 0 and <code class="language-plaintext highlighter-rouge">exp</code> != 11…11 (exp range: [0000001…11111110]). Equations:
<ul>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>E</mi><mo>=</mo><mtext>exp</mtext><mo></mo><mtext>bias</mtext></mrow><annotation encoding="application/x-tex">E = \text{exp} - \text{bias}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.7777700000000001em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">exp</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span></span></span></span> and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mo></mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} - 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9324379999999999em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>M</mi><mo>=</mo><mn>1</mn><mo>+</mo><mtext>frac</mtext></mrow><annotation encoding="application/x-tex">M = 1 + \text{frac}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">frac</span></span></span></span></span></span>
</li>
</ul>
</li>
<li>If <code class="language-plaintext highlighter-rouge">exp</code> = 00…00 (all 0s) =&gt; denormalized. Equations:
<ul>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>E</mi><mo>=</mo><mn>1</mn><mo></mo><mtext>bias</mtext></mrow><annotation encoding="application/x-tex">E = 1 - \text{bias}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span></span></span></span> and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mo></mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} - 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9324379999999999em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>M</mi><mo>=</mo><mtext>frac</mtext></mrow><annotation encoding="application/x-tex">M = \text{frac}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">frac</span></span></span></span></span></span>
</li>
</ul>
</li>
<li>If <code class="language-plaintext highlighter-rouge">exp</code> 11…11 (all 1s) =&gt; special cases.
<ul>
<li>Case 1: <code class="language-plaintext highlighter-rouge">frac</code> = 000…0</li>
<li>Case 2: <code class="language-plaintext highlighter-rouge">frac</code> != 000…0</li>
</ul>
</li>
</ol>
</li>
</ul>
<h2 id="ieee-floating-point-representation---normalized">IEEE floating point representation - normalized</h2>
<p>Numerical Form: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0913309999999998em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8413309999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>Bit breakdown:</p>
<ul>
<li>s = 1 bit</li>
<li><code class="language-plaintext highlighter-rouge">exp</code> = k bits
<ul>
<li>If <code class="language-plaintext highlighter-rouge">exp</code> != 0 and <code class="language-plaintext highlighter-rouge">exp</code> != 11…11 (<code class="language-plaintext highlighter-rouge">exp</code> range: [00000001…11111110]) =&gt; normalized. Equations:
<ul>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>E</mi><mo>=</mo><mtext>exp</mtext><mo></mo><mtext>bias</mtext></mrow><annotation encoding="application/x-tex">E = \text{exp} - \text{bias}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.7777700000000001em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">exp</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span></span></span></span> and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mo></mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} - 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9324379999999999em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>M</mi><mo>=</mo><mn>1</mn><mo>+</mo><mtext>frac</mtext></mrow><annotation encoding="application/x-tex">M = 1 + \text{frac}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">frac</span></span></span></span></span></span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Why is <code class="language-plaintext highlighter-rouge">E</code> biased?</p>
<p>Using single precision as an example (s = 1 bit, exp = 8 bits, frac = 23 bits):</p>
<ul>
<li>(exp range: [00000001 .. 11111110]) =&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><msub><mn>1</mn><mn>10</mn></msub><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mn>25</mn><msub><mn>4</mn><mn>10</mn></msub><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[1_{10}...254_{10}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord">...25</span><span class="mord"><span class="mord">4</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></li>
<li>If <code class="language-plaintext highlighter-rouge">E</code> is not biased (i.e. E = exp), then <code class="language-plaintext highlighter-rouge">E</code> range <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><msub><mn>1</mn><mn>10</mn></msub><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mn>25</mn><msub><mn>4</mn><mn>10</mn></msub><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[1_{10} ... 254_{10}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord">...25</span><span class="mord"><span class="mord">4</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></li>
<li><code class="language-plaintext highlighter-rouge">V</code> range [<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mn>1</mn></msup></mrow><annotation encoding="application/x-tex">2^{1}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span></span></span></span></span></span></span></span></span></span></span></span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mn>254</mn></msup></mrow><annotation encoding="application/x-tex">2^{254}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">254</span></span></span></span></span></span></span></span></span></span></span></span>] = [2…<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>2.89</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mn>76</mn></msup></mrow><annotation encoding="application/x-tex">\approx 2.89 \times 10^{76}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.48312em;vertical-align:0em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">2.89</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">76</span></span></span></span></span></span></span></span></span></span></span></span>] (so cannot express numbers &lt; 2)</li>
<li>By biasing <code class="language-plaintext highlighter-rouge">E</code> (i.e. E = exp - bias), then <code class="language-plaintext highlighter-rouge">E</code> range: [1-127…254-127] == [-126…127] (since k = 8, bias = <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mrow><mn>8</mn><mo></mo><mn>1</mn></mrow></msup><mo></mo><mn>1</mn></mrow><annotation encoding="application/x-tex">2^{8-1} - 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.897438em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">8</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span> = 127)</li>
<li><code class="language-plaintext highlighter-rouge">V</code> range: [<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mrow><mo></mo><mn>126</mn></mrow></msup></mrow><annotation encoding="application/x-tex">2^{-126}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mtight">126</span></span></span></span></span></span></span></span></span></span></span></span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mn>127</mn></msup></mrow><annotation encoding="application/x-tex">2^{127}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">127</span></span></span></span></span></span></span></span></span></span></span></span>] = [<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>1.18</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mrow><mo></mo><mn>38</mn></mrow></msup></mrow><annotation encoding="application/x-tex">\approx 1.18 \times 10^{-38}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.48312em;vertical-align:0em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1.18</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mtight">38</span></span></span></span></span></span></span></span></span></span></span></span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>1.7</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mn>38</mn></msup></mrow><annotation encoding="application/x-tex">\approx 1.7 \times 10^{38}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.48312em;vertical-align:0em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1.7</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">38</span></span></span></span></span></span></span></span></span></span></span></span> (so can now express very small (and very large) numbers)</li>
<li>Why adding 1 to <code class="language-plaintext highlighter-rouge">frac</code>? Because the number (or value) V is first normalized before it is converted.</li>
</ul>
<h2 id="review-scientific-notation-and-normalization">Review: Scientific Notation and normalization</h2>
<ul>
<li>From Wikipedia:
<ul>
<li>Scientific notation is a way of expressing numbers that are too large or too small to be conveniently written in decimal form (as they are long strings of digits).</li>
<li>In scientific notation, nonzero numbers are written in the form +/- M × 10n</li>
<li>In normalized notation, the exponent n is chosen such that the absolute value of the significand M is at least 1 (M = 1.0) but less than the base
<ul>
<li>M range for base 10 =&gt; [1.0 .. 10.0 ε ]</li>
<li>M range for base 2 =&gt; [1.0 .. 2.0 ε ]</li>
</ul>
</li>
</ul>
</li>
<li>
<p>Examples:</p>
<ul>
<li>A protons mass is 0.0000000000000000000000000016726 kg -&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1.6726</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mrow><mtext></mtext><mn>27</mn></mrow></msup></mrow><annotation encoding="application/x-tex">1.6726 \times 10^{27}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1.6726</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">27</span></span></span></span></span></span></span></span></span></span></span></span> kg</li>
<li>Speed of light is 299,792,458 m/s -&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2.99792</mn><mo separator="true">,</mo><mn>458</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mn>8</mn></msup></mrow><annotation encoding="application/x-tex">2.99792,458 \times 10^{8}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8388800000000001em;vertical-align:-0.19444em;"></span><span class="mord">2.99792</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">458</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">8</span></span></span></span></span></span></span></span></span></span></span></span> m/s</li>
</ul>
</li>
</ul>
<p>Syntax of normalized notation:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Notation</th>
</tr>
</thead>
<tbody>
<tr>
<td>Sign</td>
<td>+/-</td>
</tr>
<tr>
<td>Significant</td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mn>0</mn></msub><mo separator="true">,</mo><msub><mi>d</mi><mrow><mo></mo><mn>1</mn></mrow></msub><mo separator="true">,</mo><msub><mi>d</mi><mrow><mo></mo><mn>2</mn></mrow></msub><mo separator="true">,</mo><msub><mi>d</mi><mrow><mo></mo><mn>3</mn></mrow></msub></mrow><annotation encoding="application/x-tex">d_{0}, d_{-1}, d_{-2}, d_{-3}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.902771em;vertical-align:-0.208331em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.301108em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.208331em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.301108em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.208331em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.301108em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mtight">3</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.208331em;"><span></span></span></span></span></span></span></span></span></span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mrow><mo></mo><mi>n</mi></mrow></msub></mrow><annotation encoding="application/x-tex">d_{-n}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.902771em;vertical-align:-0.208331em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.25833100000000003em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mathnormal mtight">n</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.208331em;"><span></span></span></span></span></span></span></span></span></span></td>
</tr>
<tr>
<td>Base</td>
<td><code class="language-plaintext highlighter-rouge">b</code></td>
</tr>
<tr>
<td>Exponent</td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mrow></mrow><mtext>exp</mtext></msup></mrow><annotation encoding="application/x-tex">^{\text{exp}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.664392em;vertical-align:0em;"></span><span class="mord"><span></span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">exp</span></span></span></span></span></span></span></span></span></span></span></span></span></td>
</tr>
</tbody>
</table>
<ul>
<li>Lets try: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>101011010.10</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">101011010.101_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">101011010.10</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> -&gt; ___</li>
</ul>
<h2 id="lets-try-normalizing-these-fractional-binary-numbers">Lets try normalizing these fractional binary numbers!</h2>
<ol>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>101011010.10</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">101011010.101_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">101011010.10</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>0.00000000110</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">0.000000001101_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0.00000000110</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>1100000011100</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">11000000111001_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1100000011100</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
</ol>
<h2 id="ieee-floating-point-representation">IEEE floating point representation</h2>
<ul>
<li>Once V is normalized, we apply the equations
<ul>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup><mo>=</mo><mn>1.0101101010</mn><msub><mn>1</mn><mn>2</mn></msub><mo>×</mo><msup><mn>2</mn><mn>8</mn></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E} = 1.01011010101_{2} \times 2^{8}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1.0101101010</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8641079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8641079999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">8</span></span></span></span></span></span></span></span></span></span></span></span></span>
</li>
<li><code class="language-plaintext highlighter-rouge">s</code> = ???</li>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>E</mi><mo>=</mo><mtext>exp</mtext><mo></mo><mtext>bias</mtext></mrow><annotation encoding="application/x-tex">E = \text{exp} - \text{bias}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.7777700000000001em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">exp</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span></span></span></span> where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mo></mo><mn>1</mn><mo>=</mo><msup><mn>2</mn><mn>7</mn></msup><mo></mo><mn>1</mn><mo>=</mo><mn>128</mn><mo></mo><mn>1</mn><mo>=</mo><mn>127</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} - 1 = 2^{7} - 1 = 128 - 1 = 127</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9324379999999999em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.897438em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">7</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">128</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">127</span></span></span></span></li>
<li><code class="language-plaintext highlighter-rouge">exp</code> = <code class="language-plaintext highlighter-rouge">E</code> + <code class="language-plaintext highlighter-rouge">bias</code> = ___</li>
<li><code class="language-plaintext highlighter-rouge">M</code> = 1 + <code class="language-plaintext highlighter-rouge">frac</code> = ___</li>
<li><code class="language-plaintext highlighter-rouge">s</code> = 1 bit, <code class="language-plaintext highlighter-rouge">exp</code> = k bits =&gt; 8 bits, <code class="language-plaintext highlighter-rouge">frac</code> n bits =&gt; 23 bits</li>
<li>bit vector in memory:</li>
</ul>
</li>
</ul>
<h2 id="why-adding-1-to-frac-or-subtracting-1-from-m">Why adding 1 to frac (or subtracting 1 from M)?</h2>
<ul>
<li>Because the number (or value) V is first normalized before it is converted.
<ul>
<li>As part of this normalization process, we transform our binary number such that its significand M is within the range [1.0 .. 2.0 ε ]</li>
<li>Remember: M range for base 2 =&gt; [1.0 … 2.0 ε]</li>
<li>This implies that M is always at least 1.0, so its integral part always has the value 1</li>
<li>So since this bit is always part of M, IEEE 754 does not explicitly save it in its bit pattern (i.e., in memory)</li>
<li>Instead, this bit is implied!</li>
</ul>
</li>
</ul>
<h2 id="why-adding-1-to-frac-or-subtracting-1-from-m-1">Why adding 1 to frac (or subtracting 1 from M)?</h2>
<p>Implying this bit has the following effects:</p>
<p>We get the
leading bit
for free!</p>
<ol>
<li>We save 1 bit when we convert (represent) a fractional decimal number into a bit pattern using IEEE 754 floating point representation</li>
<li>We have to add this 1 bit back when we convert from a bit pattern (IEEE 754 floating point representation) back to a fractional decimal</li>
</ol>
<p>Example: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup><mo>=</mo><mn>1.01011010101</mn><mo>×</mo><msup><mn>2</mn><mn>8</mn></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E} = 1.01011010101 \times 2^{8}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0913309999999998em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8413309999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1.01011010101</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">8</span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>M = 1. 01011010101 =&gt; M = 1 + frac</p>
<p>This bit is implied hence not stored in the bit pattern produced
by the IEEE 754 floating point representation, and what we
store in the frac part of the IEEE 754 bit pattern is 01011010101</p>
<h2 id="ieee-floating-point-representation-single-precision">IEEE floating point representation (single precision)</h2>
<ul>
<li>What if the 4 bytes starting at M[0x0000] represented a fractional
decimal number (encoded as an IEEE floating point number) -&gt; value?
single precision</li>
</ul>
<p>Numerical Form: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0913309999999998em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8413309999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td> </td>
</tr>
<tr>
<td>10000111</td>
<td>k=8 bits, interpreted as unsigned</td>
</tr>
<tr>
<td>01011010101000000000000</td>
<td>n=23 bits, interpreted as unsigned</td>
</tr>
</tbody>
</table>
<ul>
<li>exp ≠ 0 and exp ≠ 111111112 -&gt; normalized</li>
<li>s = ___</li>
<li>E = exp bias where bias = <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mtext></mtext><mn>1</mn><mo>=</mo><msup><mn>2</mn><mn>7</mn></msup><mtext></mtext><mn>1</mn><mo>=</mo><mn>128</mn><mtext></mtext><mn>1</mn><mo>=</mo><mn>127</mn></mrow><annotation encoding="application/x-tex">2^{k-1} 1 = 2^{7} 1 = 128 1 = 127</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8491079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">7</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1281</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">127</span></span></span></span>$</li>
<li>E = ____ - 127 =</li>
<li>M = 1 + <code class="language-plaintext highlighter-rouge">frac</code> = 1 + ___</li>
<li>V = ____</li>
</ul>
<p>Little endian memory layout:</p>
<table>
<thead>
<tr>
<th>Address</th>
<th>M[]</th>
</tr>
</thead>
<tbody>
<tr>
<td>size-1</td>
<td> </td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>0x0003</td>
<td>11000011</td>
</tr>
<tr>
<td>0x0002</td>
<td>10101101</td>
</tr>
<tr>
<td>0x0001</td>
<td>01010000</td>
</tr>
<tr>
<td>0x0000</td>
<td>00000000</td>
</tr>
</tbody>
</table>
<h2 id="lets-give-it-a-go">Lets give it a go!</h2>
<ul>
<li>What if the 4 bytes starting at M[0x0000] represented a fractional
decimal number (encoded as an IEEE floating point number) -&gt; value?</li>
</ul>
<p>Numerical form: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mo></mo><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = (-1)^{s} M 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0913309999999998em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"></span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8413309999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>single precision</p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td> </td>
</tr>
<tr>
<td>10001100</td>
<td>k=8 bits, interpreted as unsigned</td>
</tr>
<tr>
<td>11011011011000000000000</td>
<td>n=23 bits, interpreted as unsigned</td>
</tr>
</tbody>
</table>
<ul>
<li>exp ≠ 0 and exp ≠ 111111112 -&gt; normalized</li>
<li>s = ____</li>
<li>E = exp - bias where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mn>7</mn></msup><mo></mo><mn>1</mn><mo>=</mo><mn>128</mn><mo></mo><mn>1</mn><mo>=</mo><mn>127</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{7} - 1 = 128 - 1 = 127</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.897438em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">7</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">128</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">127</span></span></span></span></li>
<li>E = ____ - 127 = ___</li>
<li>M = 1 + frac = 1 + ____</li>
<li>V = ____</li>
</ul>
<p>Little endian memory map:</p>
<table>
<thead>
<tr>
<th>Address</th>
<th>M[]</th>
</tr>
</thead>
<tbody>
<tr>
<td>size-1</td>
<td> </td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>0x0003</td>
<td>01000110</td>
</tr>
<tr>
<td>0x0002</td>
<td>01101101</td>
</tr>
<tr>
<td>0x0001</td>
<td>10110000</td>
</tr>
<tr>
<td>0x0000</td>
<td>00000000</td>
</tr>
</tbody>
</table>
<h2 id="ieee-floating-point-representation-single-precision-1">IEEE floating point representation (single precision)</h2>
<p>How would 47.21875 be encoded as IEEE floating point number?</p>
<ol>
<li>Convert 47.28 to binary (using the positional notation R2B(X)) =&gt;
<ul>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>47</mn><mo>=</mo><mn>10111</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">47 = 101111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">47</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">10111</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi mathvariant="normal">.</mi><mn>21875</mn><mo>=</mo><mi mathvariant="normal">.</mi><mn>0011</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">.21875 = .00111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">.21875</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">.0011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
</ul>
</li>
<li>Normalize binary number:
101111.00111 =&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1.011110011</mn><msub><mn>1</mn><mn>2</mn></msub><mo>×</mo><msup><mn>2</mn><mn>5</mn></msup></mrow><annotation encoding="application/x-tex">1.0111100111_{2} \times 2^{5}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1.011110011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">5</span></span></span></span></span></span></span></span></span></span></span></span>
<ul>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></span>
</li>
</ul>
</li>
<li>Determine …
<ul>
<li>s = 0</li>
<li>E = exp bias where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mtext></mtext><mn>1</mn><mo>=</mo><msup><mn>2</mn><mn>7</mn></msup><mtext></mtext><mn>1</mn><mo>=</mo><mn>128</mn><mtext></mtext><mn>1</mn><mo>=</mo><mn>127</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} 1 = 2^{7} 1 = 128 1 = 127</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8491079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">7</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1281</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">127</span></span></span></span></li>
<li>exp = E + bias = 5 + 127 = 132 =&gt; U2B(132) =&gt; 10000100</li>
<li>M = 1 + frac -&gt; frac = M - 1 =&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1.011110011</mn><msub><mn>1</mn><mn>2</mn></msub><mo></mo><mn>1</mn><mo>=</mo><mi mathvariant="normal">.</mi><mn>011110011</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">1.0111100111_{2} - 1 = .0111100111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1.011110011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">.011110011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></li>
</ul>
</li>
<li>32 bits organized in s|exp|frac: [0|10000100|01111001110000000000000}</li>
<li>0x423CE000</li>
</ol>
<h2 id="ieee-floating-point-representation-single-precision-2">IEEE floating point representation (single precision)</h2>
<p>How would 12345.75 be encoded as IEEE floating point number?</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mo></mo><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">
V = (-1)^{s} M 2^{E}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"></span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathdefault" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<ol>
<li>Convert 12345.75 to binary
<ul>
<li>12345 =&gt; ____ .75 =&gt; ____</li>
</ul>
</li>
<li>Normalize binary number:</li>
<li>Determine …
<ul>
<li>s = ____</li>
<li>E = exp bias where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mtext></mtext><mn>1</mn><mo>=</mo><msup><mn>2</mn><mn>7</mn></msup><mtext></mtext><mn>1</mn><mo>=</mo><mn>128</mn><mtext></mtext><mn>1</mn><mo>=</mo><mn>127</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} 1 = 2^{7} 1 = 128 1 = 127</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8491079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">7</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1281</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">127</span></span></span></span></li>
<li>exp = E + bias = ____</li>
<li>M = 1 + frac -&gt; frac = M - 1</li>
</ul>
</li>
<li>[_|________|_______________________]</li>
<li>Express in hex:</li>
</ol>
<h2 id="summary">Summary</h2>
<ul>
<li>IEEE Floating Point Representation
<ol>
<li>Denormalized</li>
<li>Special cases</li>
<li>Normalized =&gt; exp ≠ 000…0 and exp ≠ 111…1
<ul>
<li>Single precision: bias = 127, exp: [1..254], E: [-126..127] =&gt; [10-38 … 1038]</li>
<li>Called “normalized” because binary numbers are normalized</li>
</ul>
<ul>
<li>Effect: “We get the leading bit for free”
<ul>
<li>Leading bit is always assumed (never part of bit pattern)</li>
</ul>
</li>
</ul>
</li>
</ol>
</li>
<li>IEEE floating point number as encoding scheme
<ul>
<li>Fractional decimal number  IEEE 754 (bit pattern)</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></span>
<ul>
<li>s is sign bit, M = 1 + frac, E = exp bias, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mtext></mtext><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8491079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mord">1</span></span></span></span> and k is width of exp</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="next-lecture">Next Lecture</h2>
<ul>
<li>Representing data in memory Most of this is review
<ul>
<li>“Under the Hood” - Von Neumann architecture</li>
<li>Bits and bytes in memory
<ul>
<li>How to diagram memory -&gt; Used in this course and other references</li>
<li>How to represent series of bits -&gt; In binary, in hexadecimal (conversion)</li>
<li>What kind of information (data) do series of bits represent -&gt; Encoding scheme</li>
<li>Order of bytes in memory -&gt; Endian</li>
</ul>
</li>
<li>Bit manipulation bitwise operations
<ul>
<li>Boolean algebra + Shifting</li>
</ul>
</li>
</ul>
</li>
<li>Representing integral numbers in memory
<ul>
<li>Unsigned and signed</li>
<li>Converting, expanding and truncating</li>
<li>Arithmetic operations</li>
</ul>
</li>
<li>Representing real numbers in memory
<ul>
<li>IEEE floating point representation</li>
<li>Floating point in C casting, rounding, addition, …</li>
</ul>
</li>
</ul>
<footer>
</footer>
</div>
</main>
</body>
</html>

@ -1,308 +0,0 @@
---
layout: simple
math: true
---
# CMPT 295
* Unit - Data Representation
* Lecture 6 Representing fractional numbers in memory
* IEEE floating point representation contd
## Have you heard of that new band "1023 Megabytes"?
They're pretty good,
but they don't have a gig just yet.
&#x1F62D;
## Last Lecture
* Representing integral numbers in memory
* Can encode a small range of values exactly (in 1, 2, 4, 8 bytes)
* For example: We can represent the values -128 to 127 exactly in 1 byte using a signed char in C
* Representing fractional numbers in memory
1. Positional notation has some advantages, but also disadvantages -> so not used!
2. IEEE floating point representation: can encode a much larger range of e.g., single precision: [10-38..1038] values approximately (in 4 or 8 bytes)
* Overview of IEEE floating point representation
* Precision options (float 32-bit, double 64-bit)
* V = (-1)s x M x 2E
* s > sign bit
* exp encodes E (but != E)
* frac encodes M (but != M)
We interpret the bit vector (expressed in IEEE floating point encoding) stored in memory using this equation.
## Todays Menu
* Representing data in memory Most of this is review
* “Under the Hood” - Von Neumann architecture
* Bits and bytes in memory
* How to diagram memory -> Used in this course and other references
* How to represent series of bits -> In binary, in hexadecimal (conversion)
* What kind of information (data) do series of bits represent -> Encoding scheme
* Order of bytes in memory -> Endian
* Bit manipulation bitwise operations
* Boolean algebra + Shifting
* Representing integral numbers in memory
* Unsigned and signed
* Converting, expanding and truncating
* Arithmetic operations
* Representing real numbers in memory
4
* IEEE floating point representation
* Floating point in C casting, rounding, addition, …
## IEEE Floating Point Representation Three “kinds” of values
We interpret the bit vector
(expressed in IEEE floating point encoding) stored in memory using this equation:
{% katex display %}
V = (-1)^{s} M 2^{E}
{% endkatex %}
Bit breakdown--`exp` and `frac` interpreted as unsigned:
* s = 1 bit
* exp = k bits
1. If `exp` != 0 and `exp` != 11...11 (exp range: [0000001...11111110]). Equations:
* $$E = \text{exp} - \text{bias}$$ and $$\text{bias} = 2^{k-1} - 1$$
* $$M = 1 + \text{frac}$$
2. If `exp` = 00...00 (all 0's) => denormalized. Equations:
* $$E = 1 - \text{bias}$$ and $$\text{bias} = 2^{k-1} - 1$$
* $$M = \text{frac}$$
3. If `exp` 11...11 (all 1's) => special cases.
* Case 1: `frac` = 000...0
* Case 2: `frac` != 000...0
## IEEE floating point representation - normalized
Numerical Form: $$V = (1)^{s} M 2^{E}$$
Bit breakdown:
* s = 1 bit
* `exp` = k bits
* If `exp` != 0 and `exp` != 11...11 (`exp` range: [00000001...11111110]) => normalized. Equations:
* $$E = \text{exp} - \text{bias}$$ and $$\text{bias} = 2^{k-1} - 1$$
* $$M = 1 + \text{frac}$$
Why is `E` biased?
Using single precision as an example (s = 1 bit, exp = 8 bits, frac = 23 bits):
* (exp range: [00000001 .. 11111110]) => $$[1_{10}...254_{10}]$$
* If `E` is not biased (i.e. E = exp), then `E` range $$[1_{10} ... 254_{10}]$$
* `V` range [$$2^{1}$$...$$2^{254}$$] = \[2...$$\approx 2.89 \times 10^{76}$$\] (so cannot express numbers < 2)
* By biasing `E` (i.e. E = exp - bias), then `E` range: [1-127...254-127] == \[-126...127\] (since k = 8, bias = $$2^{8-1} - 1$$ = 127)
* `V` range: [$$2^{-126}$$...$$2^{127}$$] = [$$\approx 1.18 \times 10^{-38}$$ ... $$\approx 1.7 \times 10^{38}$$ (so can now express very small (and very large) numbers)
* Why adding 1 to `frac`? Because the number (or value) V is first normalized before it is converted.
## Review: Scientific Notation and normalization
* From Wikipedia:
* Scientific notation is a way of expressing numbers that are too large or too small to be conveniently written in decimal form (as they are long strings of digits).
* In scientific notation, nonzero numbers are written in the form +/- M × 10n
* In normalized notation, the exponent n is chosen such that the absolute value of the significand M is at least 1 (M = 1.0) but less than the base
* M range for base 10 => [1.0 .. 10.0 ε ]
* M range for base 2 => [1.0 .. 2.0 ε ]
* Examples:
* A proton's mass is 0.0000000000000000000000000016726 kg -> $$1.6726 \times 10^{27}$$ kg
* Speed of light is 299,792,458 m/s -> $$2.99792,458 \times 10^{8}$$ m/s
Syntax of normalized notation:
Name|Notation
---|---
Sign|+/-
Significant|$$d_{0}, d_{-1}, d_{-2}, d_{-3}$$...$$d_{-n}$$
Base|`b`
Exponent|$$^{\text{exp}}$$
* Lets try: $$101011010.101_{2}$$ -> \_\_\_
## Lets try normalizing these fractional binary numbers!
1. $$101011010.101_{2}$$
2. $$0.000000001101_{2}$$
3. $$11000000111001_{2}$$
## IEEE floating point representation
* Once V is normalized, we apply the equations
* $$V = (1)^{s} M 2^{E} = 1.01011010101_{2} \times 2^{8}$$
* `s` = ???
* $$E = \text{exp} - \text{bias}$$ where $$\text{bias} = 2^{k-1} - 1 = 2^{7} - 1 = 128 - 1 = 127$$
* `exp` = `E` + `bias` = \_\_\_
* `M` = 1 + `frac` = \_\_\_
* `s` = 1 bit, `exp` = k bits => 8 bits, `frac` n bits => 23 bits
* bit vector in memory:
## Why adding 1 to frac (or subtracting 1 from M)?
* Because the number (or value) V is first normalized before it is converted.
* As part of this normalization process, we transform our binary number such that its significand M is within the range [1.0 .. 2.0 ε ]
* Remember: M range for base 2 => [1.0 ... 2.0 ε]
* This implies that M is always at least 1.0, so its integral part always has the value 1
* So since this bit is always part of M, IEEE 754 does not explicitly save it in its bit pattern (i.e., in memory)
* Instead, this bit is implied!
## Why adding 1 to frac (or subtracting 1 from M)?
Implying this bit has the following effects:
We get the
leading bit
for free!
1. We save 1 bit when we convert (represent) a fractional decimal number into a bit pattern using IEEE 754 floating point representation
2. We have to add this 1 bit back when we convert from a bit pattern (IEEE 754 floating point representation) back to a fractional decimal
Example: $$V = (1)^{s} M 2^{E} = 1.01011010101 \times 2^{8}$$
M = 1. 01011010101 => M = 1 + frac
This bit is implied hence not stored in the bit pattern produced
by the IEEE 754 floating point representation, and what we
store in the frac part of the IEEE 754 bit pattern is 01011010101
## IEEE floating point representation (single precision)
* What if the 4 bytes starting at M[0x0000] represented a fractional
decimal number (encoded as an IEEE floating point number) -> value?
single precision
Numerical Form: $$V = (1)^{s} M 2^{E}$$
Value|Notes
---|---
1|
10000111|k=8 bits, interpreted as unsigned
01011010101000000000000|n=23 bits, interpreted as unsigned
* exp ≠ 0 and exp ≠ 111111112 -> normalized
* s = \_\_\_
* E = exp bias where bias = $$2^{k-1} 1 = 2^{7} 1 = 128 1 = 127$$$
* E = \_\_\_\_ - 127 =
* M = 1 + `frac` = 1 + \_\_\_
* V = \_\_\_\_
Little endian memory layout:
Address|M[]
---|---
size-1|
...|...
0x0003|11000011
0x0002|10101101
0x0001|01010000
0x0000|00000000
## Lets give it a go!
* What if the 4 bytes starting at M[0x0000] represented a fractional
decimal number (encoded as an IEEE floating point number) -> value?
Numerical form: $$V = (-1)^{s} M 2^{E}$$
single precision
Value|Notes
---|---
0|
10001100|k=8 bits, interpreted as unsigned
11011011011000000000000|n=23 bits, interpreted as unsigned
* exp ≠ 0 and exp ≠ 111111112 -> normalized
* s = \_\_\_\_
* E = exp - bias where $$\text{bias} = 2^{7} - 1 = 128 - 1 = 127$$
* E = \_\_\_\_ - 127 = \_\_\_
* M = 1 + frac = 1 + \_\_\_\_
* V = \_\_\_\_
Little endian memory map:
Address|M[]
---|---
size-1|
...|...
0x0003|01000110
0x0002|01101101
0x0001|10110000
0x0000|00000000
## IEEE floating point representation (single precision)
How would 47.21875 be encoded as IEEE floating point number?
1. Convert 47.28 to binary (using the positional notation R2B(X)) =>
* $$47 = 101111_{2}$$
* $$.21875 = .00111_{2}$$
2. Normalize binary number:
101111.00111 => $$1.0111100111_{2} \times 2^{5}$$
* $$V = (1)^{s} M 2^{E}$$
3. Determine …
* s = 0
* E = exp bias where $$\text{bias} = 2^{k-1} 1 = 2^{7} 1 = 128 1 = 127$$
* exp = E + bias = 5 + 127 = 132 => U2B(132) => 10000100
* M = 1 + frac -> frac = M - 1 => $$1.0111100111_{2} - 1 = .0111100111_{2}$$
4. 32 bits organized in s\|exp\|frac: \[0\|10000100\|01111001110000000000000\}
5. 0x423CE000
## IEEE floating point representation (single precision)
How would 12345.75 be encoded as IEEE floating point number?
{% katex display %}
V = (-1)^{s} M 2^{E}
{% endkatex %}
1. Convert 12345.75 to binary
* 12345 => \_\_\_\_ .75 => \_\_\_\_
2. Normalize binary number:
3. Determine …
* s = \_\_\_\_
* E = exp bias where $$\text{bias} = 2^{k-1} 1 = 2^{7} 1 = 128 1 = 127$$
* exp = E + bias = \_\_\_\_
* M = 1 + frac -> frac = M - 1
4. \[\_\|\_\_\_\_\_\_\_\_\|\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\]
5. Express in hex:
## Summary
* IEEE Floating Point Representation
1. Denormalized
2. Special cases
3. Normalized => exp ≠ 000…0 and exp ≠ 111…1
* Single precision: bias = 127, exp: [1..254], E: [-126..127] => [10-38 … 1038]
* Called “normalized” because binary numbers are normalized
* Effect: “We get the leading bit for free”
* Leading bit is always assumed (never part of bit pattern)
* IEEE floating point number as encoding scheme
* Fractional decimal number  IEEE 754 (bit pattern)
* $$V = (1)^{s} M 2^{E}$$
* s is sign bit, M = 1 + frac, E = exp bias, $$\text{bias} = 2^{k-1} 1$$ and k is width of exp
## Next Lecture
* Representing data in memory Most of this is review
* “Under the Hood” - Von Neumann architecture
* Bits and bytes in memory
* How to diagram memory -> Used in this course and other references
* How to represent series of bits -> In binary, in hexadecimal (conversion)
* What kind of information (data) do series of bits represent -> Encoding scheme
* Order of bytes in memory -> Endian
* Bit manipulation bitwise operations
* Boolean algebra + Shifting
* Representing integral numbers in memory
* Unsigned and signed
* Converting, expanding and truncating
* Arithmetic operations
* Representing real numbers in memory
* IEEE floating point representation
* Floating point in C casting, rounding, addition, …

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save