diff --git a/_practice/2023-11-16.md b/_practice/2023-11-16.md new file mode 100644 index 0000000..bac5e56 --- /dev/null +++ b/_practice/2023-11-16.md @@ -0,0 +1,20 @@ +1. While we saw many types of gates today, but we actually could get all of the operations needed using only NAND gates. Work out how to use NAND gates to implement a half adder and describe it in {index}`nandhalf.md` +2. In {index}`addertypes.md` compare ripple adders and lookahead adders. + ``` + 1. Give a synopsis of each adder type + 1. Compare them in terms of time (assume that each gate requires one unite of time) + 1. Compare them in terms of space/cost by counting the total number of gates required. + ``` +3. Add {index}`bitwise.md` to your kwl and write the bitwise operations required for the following transformations (replace the `(_)` with a bitwise operator (`&, |, ^, >>, <<`)): + ``` + 4 (_) 128 + 12493 (_) -12494 + 127 (_) 15 + 7 (_) 56 + 4 (_) -5 + 45 (_) 37 = 37 + 45 (_) 37 = 45 + 3 (_) 5 = 7 + 6 (_) 8 = 0 + 10 (_) 5 = 15 + ``` \ No newline at end of file diff --git a/_prepare/2023-11-21.md b/_prepare/2023-11-21.md new file mode 100644 index 0000000..52edb1c --- /dev/null +++ b/_prepare/2023-11-21.md @@ -0,0 +1,6 @@ +1. In {index}`fractionalbinary.md` use 8 bits to represent the following numbers by using 4 bits as usual (8,2,4,1) and the other 4 bits are 1/2, 1/4, 1/8, 1/16th: +- 3.75 +- 7.5 +- 11.625 +- 5.1875 +1. Add to your file some notes about the limitations of representing non integer values this way. How much would using more bits help with, what limitations are not resolved by adding more bits. For example, how could you represent .1? \ No newline at end of file diff --git a/_review/2023-11-16.md b/_review/2023-11-16.md new file mode 100644 index 0000000..e666a08 --- /dev/null +++ b/_review/2023-11-16.md @@ -0,0 +1,14 @@ +1. While we saw many types of gates today, but we actually could get all of the operations needed using only NAND gates. Work out how to use NAND gates to implement `and` and `or` in {index}`nandandor.md` +2. Add {index}`bitwise.md` to your kwl and write the bitwise operations required for the following transformations (replace the `(_)` with a bitwise operator (`&, |, ^, >>, <<`)): + ``` + 4 (_) 128 + 12493 (_) -12494 + 127 (_) 15 + 7 (_) 56 + 4 (_) -5 + 45 (_) 37 = 37 + 45 (_) 37 = 45 + 3 (_) 5 = 7 + 6 (_) 8 = 0 + 10 (_) 5 = 15 + ``` \ No newline at end of file diff --git a/activities/practice.md b/activities/practice.md index a09a19b..f58f182 100644 --- a/activities/practice.md +++ b/activities/practice.md @@ -131,4 +131,11 @@ Activities: Activities: ```{include} ../_practice/2023-11-14.md +``` +## 2023-11-16 + +[related notes](../notes/2023-11-16) + +Activities: +```{include} ../_practice/2023-11-16.md ``` \ No newline at end of file diff --git a/activities/prepare.md b/activities/prepare.md index 5bd6416..8e0663f 100644 --- a/activities/prepare.md +++ b/activities/prepare.md @@ -119,4 +119,11 @@ Activities: Activities: ```{include} ../_prepare/2023-11-14.md +``` +## 2023-11-16 + +[related notes](../notes/2023-11-16) + +Activities: +```{include} ../_prepare/2023-11-16.md ``` \ No newline at end of file diff --git a/activities/review.md b/activities/review.md index 79ddaed..34deb65 100644 --- a/activities/review.md +++ b/activities/review.md @@ -125,4 +125,11 @@ Activities: Activities: ```{include} ../_review/2023-11-14.md +``` +## 2023-11-16 + +[related notes](../notes/2023-11-16) + +Activities: +```{include} ../_review/2023-11-16.md ``` \ No newline at end of file diff --git a/img/xor_swap_0.svg b/img/xor_swap_0.svg new file mode 100644 index 0000000..54a5c0a --- /dev/null +++ b/img/xor_swap_0.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/img/xor_swap_1.svg b/img/xor_swap_1.svg new file mode 100644 index 0000000..92aa38b --- /dev/null +++ b/img/xor_swap_1.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/img/xor_swap_2.svg b/img/xor_swap_2.svg new file mode 100644 index 0000000..b999a94 --- /dev/null +++ b/img/xor_swap_2.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/img/xor_swap_3.svg b/img/xor_swap_3.svg new file mode 100644 index 0000000..59734c9 --- /dev/null +++ b/img/xor_swap_3.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/notes/2023-11-16.md b/notes/2023-11-16.md new file mode 100644 index 0000000..69679b6 --- /dev/null +++ b/notes/2023-11-16.md @@ -0,0 +1,337 @@ +--- +file_format: mystnb +kernelspec: + name: python3 +--- + +# How can we use logical operations? + +## Why do we need to think about bitwise operations? + + + +Understanding them is prereq to what we will see today and that will help you understand hardware overall. + + + +You of course will not *need* every single thing we teach you in every single class. + + + +* Seeing topics once at least is the only way you can make an informed decision to study a topic deeper or not. +* Seeing a topic in more detail than you will use all the time actually helps you build intuition, or deep understanding, of the topic overall, and help you remember what you need to remember + + + + +## Bitwise operators review + + + +- & : and +- | : or +- ^ : xor +- ~ : not +- >>: shift right +- <<: shift left + + + +Let's review truth tables for and, or, and xor. + + + + +```{list-table} AND +:header-rows: 1 +:name: truth-and + + + +* - a + - b + - AND (a&b) +* - 0 + - 0 + - 0 +* - 0 + - 1 + - 0 +* - 1 + - 0 + - 0 +* - 1 + - 1 + - 1 +``` + + + + +```{list-table} OR +:header-rows: 1 +:name: truth-or + + + +* - a + - b + - OR (a|b) +* - 0 + - 0 + - 0 +* - 0 + - 1 + - 1 +* - 1 + - 0 + - 1 +* - 1 + - 1 + - 1 +``` + + + + +```{list-table} XOR +:header-rows: 1 +:name: truth-xor + + + +* - a + - b + - XOR (a^b) +* - 0 + - 0 + - 0 +* - 0 + - 1 + - 1 +* - 1 + - 0 + - 1 +* - 1 + - 1 + - 0 +``` + + + +In order to implement more complex calculations, using gates, we can use these tables as building blocks compared to the required output. + + + +There are more gate operations; you can see a simulation for [16 gates](https://lodev.org/logicemu/#id=gates16) + + + + +## Adding as an Algorithm + + + +Let's review adding binary numbers. + +**remember, binary is a place-based system like the decimal placed based system you are likely familiar with** + + + +How do you add each of the following: +- 2 + 3 +- 14 + 17 +- 65 +37 + + +To add two or more digit numbers we add the right most place (ones) and if its' more than 10, we carry the 1 then add 3 numbers (1 and the other two digits). And proceed accordingly moving from right to left. + +Since binary is place-based adding with binary follows the same basic algorithm + +- add the two bits in the ones place +- carry to the next place if >=2 + + + +$$ 101 + 100 = 1001 $$ + + + +We first add the ones place and get a 1, then the two's place and get a zero then the 4's place and get 0 with a carried one. + + + +$$ 010 + 011 = 101 $$ + + + +In this case in the ones place we add 0 + 1 to get one, the two ones add to 0 with carry then 1 + 0 + 0 gives another 1. + + +## Adding as logic gates + + +let's make a truth table for adding two bits. + + + + +```{list-table} Add +:header-rows: 1 +:name: truth-add +* - a + - b + - out 2's + - out 1's +* - 0 + - 0 + - 0 + - 0 +* - 0 + - 1 + - 0 + - 1 +* - 1 + - 0 + - 0 + - 1 +* - 1 + - 1 + - 1 + - 0 +``` + + + +Now, what gate can we use to get the output 1's place bit and what gate can we use to get the output 2's place bit by comparing to the truth tables above. + + + +It turns out the one's place is an xor gate, and the two's place is an and gate. + + + +This makes up the [half adder, try one out at this simulator](https://lodev.org/logicemu/#id=half_adder). + + +## Implementing the carry + +So this lets us as two bits, but what about adding a number with more bits? + + + +We can put multiple together, but there's one more wrinkle: the carry. + + + +That's what makes a [full adder](https://lodev.org/logicemu/#id=full_adder) different. It adds three single bits, or a carry and two bits and outputs the result as a sum bit and a carry bit. + + + +## Adding more bits + + + +Then we can link many of those together to get an [8 bit ripple adder](https://lodev.org/logicemu/#id=ripple_carry_adder). + + + +Alternatively, we can "lookahead" with the carry bit, passing it forward multiple places all at once, as shown in this [4 bit carry lookahead adder](https://lodev.org/logicemu/#id=cla_adder). + + +## Why do this? + +Workign through this example also reinforces not only the facts of how the binary works so that you can understand *how* a computer works. Working with these abstractions to break down higher level operations into components like this (addition is a more complex operation than `and` or `xor`) help you see how this can be done. + +Sometimes you have low level problems like resource constraints and bitwise operations can be useful. + +We may not do this all the time, but when we need it, we need it. + +for example, consdier how to swap two values. + +Assume we have two variables `a` and `b` intitialized like: +```{code-cell} python +a = 4 +b=3 +a,b +``` + +We could swap them like +```{code-cell} python +tmp = a +a = b +b=a +``` + +then we can see the values to see that they are swapped + +```{code-cell} python +a,b +``` + +Let's reset them + +```{code-cell} python +a =4 +b=3 +a,b +``` + +With bitwise operations we can swap them without a 3rd variable. + +```{code-cell} python +a = a^b +b= a^b +a=a^b +``` + + +then we can see the values to see that they are swapped + +```{code-cell} python +a,b +``` + +If we implement this with only 3 bits we have a 1,2,4 places. + +![4 and 3 rpresented in binary in 2 registers labeled a and b](../img/xor_swap_0.svg) + +Then we xor each bit and sotre the result in the first register (our a variable) + +![4^3 and 3 represented in binary in 2 registers labeled a and b](../img/xor_swap_1.svg) + +and next we xor again and store in b + +![4^3 and 4 represented in binary in 2 registers labeled a and b](../img/xor_swap_2.svg) + +now the 4 has moved form A to B. If we xor one more time and store that in A, + +![3 and 4 represented in binary in 2 registers labeled a and b](../img/xor_swap_3.svg) + + + +## Prepare for Next Class + +```{include} ../_prepare/2023-11-21.md +``` + +## Review today's class + +```{include} ../_review/2023-11-16.md +``` + + + +## More Practice + +```{include} ../_practice/2023-11-16.md +``` + + +## Experience Report Evidence + +Nothing separate + +## Questions After Today's Class + +Post manually for a community badge. \ No newline at end of file