Add melody's notes

master
Tait Hoyem 3 years ago
parent fe5eec5551
commit 4cc4202008

@ -0,0 +1,88 @@
---
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

@ -0,0 +1,357 @@
---
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

@ -0,0 +1,383 @@
---
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, …

@ -0,0 +1,401 @@
---
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 %}
00111011_{2} + 01011010_{2} = ?
{% endkatex %}
Decimal:
{% katex display %}
59_{10} + 90_{10} = 149_{10}
{% endkatex %}
b)
Binary:
{% katex display %}
10101110_{2} + 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!

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