|
| 1 | +# How can we use logical operations? |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +# Why do we need to think about bitwise operations? |
| 7 | + |
| 8 | +Understanding them is prereq to what we will see today and that will help you understand hardware overall. |
| 9 | + |
| 10 | +You of course will not *need* every single thing we teach you in every single class. |
| 11 | + |
| 12 | +* Seeing topics once at least is the only way you can make an informed decision to study a topic deeper or not. |
| 13 | +* 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 |
| 14 | + |
| 15 | + |
| 16 | +## Bitwise operators |
| 17 | + |
| 18 | +- & : and |
| 19 | +- | : or |
| 20 | +- ^ : xor |
| 21 | +- ~ : not |
| 22 | +- >>: shift right |
| 23 | +- <<: shift left |
| 24 | + |
| 25 | +Let's review truth tables for and, or, and xor. |
| 26 | + |
| 27 | + |
| 28 | +```{list-table} AND |
| 29 | +:header-rows: 1 |
| 30 | +:name: truth-and |
| 31 | +
|
| 32 | +* - a |
| 33 | + - b |
| 34 | + - output |
| 35 | +* - 0 |
| 36 | + - 0 |
| 37 | + - 0 |
| 38 | +* - 0 |
| 39 | + - 1 |
| 40 | + - 0 |
| 41 | +* - 1 |
| 42 | + - 0 |
| 43 | + - 0 |
| 44 | +* - 1 |
| 45 | + - 1 |
| 46 | + - 1 |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +```{list-table} OR |
| 51 | +:header-rows: 1 |
| 52 | +:name: truth-or |
| 53 | +
|
| 54 | +* - a |
| 55 | + - b |
| 56 | + - output |
| 57 | +* - 0 |
| 58 | + - 0 |
| 59 | + - 0 |
| 60 | +* - 0 |
| 61 | + - 1 |
| 62 | + - 1 |
| 63 | +* - 1 |
| 64 | + - 0 |
| 65 | + - 1 |
| 66 | +* - 1 |
| 67 | + - 1 |
| 68 | + - 1 |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +```{list-table} XOR |
| 73 | +:header-rows: 1 |
| 74 | +:name: truth-xor |
| 75 | +
|
| 76 | +* - a |
| 77 | + - b |
| 78 | + - output |
| 79 | +* - 0 |
| 80 | + - 0 |
| 81 | + - 0 |
| 82 | +* - 0 |
| 83 | + - 1 |
| 84 | + - 1 |
| 85 | +* - 1 |
| 86 | + - 0 |
| 87 | + - 1 |
| 88 | +* - 1 |
| 89 | + - 1 |
| 90 | + - 0 |
| 91 | +``` |
| 92 | + |
| 93 | +In order to implement more complex calculations, using gates, we can use these tables as building blocks compared to the required output. |
| 94 | + |
| 95 | +There are more gate operations; you can see a simulation for [16 gates](https://lodev.org/logicemu/#id=gates16) |
| 96 | + |
| 97 | + |
| 98 | +## Adding with gates |
| 99 | + |
| 100 | +Let's review adding binary numbers |
| 101 | + |
| 102 | +- add the two bits |
| 103 | +- carry the next place like in adding multi-digit numbers otherwise |
| 104 | + |
| 105 | +$$ 101 + 100 = 1001 $$ |
| 106 | + |
| 107 | +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. |
| 108 | + |
| 109 | +$$ 010 + 011 = 101 $$ |
| 110 | + |
| 111 | +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. |
| 112 | + |
| 113 | + |
| 114 | +let's make a truth table for adding two bits. |
| 115 | + |
| 116 | + |
| 117 | +```{list-table} Add |
| 118 | +:header-rows: 1 |
| 119 | +:name: truth-add |
| 120 | +
|
| 121 | +* - a |
| 122 | + - b |
| 123 | + - out 2's |
| 124 | + - out 1's |
| 125 | +* - 0 |
| 126 | + - 0 |
| 127 | + - 0 |
| 128 | + - 0 |
| 129 | +* - 0 |
| 130 | + - 1 |
| 131 | + - 0 |
| 132 | + - 1 |
| 133 | +* - 1 |
| 134 | + - 0 |
| 135 | + - 0 |
| 136 | + - 1 |
| 137 | +* - 1 |
| 138 | + - 1 |
| 139 | + - 1 |
| 140 | + - 0 |
| 141 | +``` |
| 142 | + |
| 143 | +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. |
| 144 | + |
| 145 | +It turns out the one's place is an xor gate, and the two's place is an and gate. |
| 146 | + |
| 147 | +This makes up the [half adder, try one out at this simulator](https://lodev.org/logicemu/#id=half_adder). |
| 148 | + |
| 149 | + |
| 150 | +So this lets us as two bits, but what about adding a number with more bits? |
| 151 | + |
| 152 | +We can put multiple together, but there's one more wrinkle: the carry. |
| 153 | + |
| 154 | +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. |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +Then we can link many of those together to get an [8 bit ripple adder](https://lodev.org/logicemu/#id=ripple_carry_adder). |
| 159 | + |
| 160 | +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). |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +## Review today's class |
| 166 | + |
| 167 | +```{include} ../_review/2022-11-14.md |
| 168 | +``` |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +## Prepare for Next Class |
| 173 | + |
| 174 | +```{include} ../_prepare/2022-11-14.md |
| 175 | +``` |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +## More Practice |
| 180 | + |
| 181 | +```{include} ../_practice/2022-11-14.md |
| 182 | +``` |
| 183 | + |
| 184 | +## Questions after class |
| 185 | + |
| 186 | +### why would we need the carry bit if we can do the math fine without it? |
| 187 | + |
| 188 | +We have the carry bit in the circuit always so that it's available when we do need it. |
| 189 | + |
| 190 | +### these logic gates are used for making circuits right? |
| 191 | + |
| 192 | +yes |
| 193 | + |
| 194 | +### How do half-adders work? I am confused on how the 1 and 0's are represented, and how things are stored |
| 195 | + |
| 196 | +In a digital computer, these are stored in registers as high (on/1) and low (off/0). The gates are electronic devices that given different inputs have a specific output. |
| 197 | + |
| 198 | +However the gate can be represented using anything with on and off positions. See the [marble adder](https://www.youtube.com/watch?v=kPguktA674w) for example. |
| 199 | + |
| 200 | +### Why can't the OR logical expression in the full adder be an XOR? From testing, I can't get a scenario where either will output a different result. |
| 201 | + |
| 202 | +It could be an XOR. Good catch. The reason it is an or gate is because an or gate is cheaper to build and we would never see at the input the one state that the xor and or would be different. The two ands can never both be 1, which is the only case when or and xor are different. |
| 203 | + |
| 204 | +### Is bitwise useful in just embedded programming or other areas of industry? |
| 205 | + |
| 206 | +Bitwise operations are useful in understanding what the computer does and that helps build up understanding of everything we need to do in a computer and for anticipating when things could go wrong. It is directly applied primarily in working with hardware directly. But also sometimes in other representations and for compression. |
| 207 | + |
| 208 | +For example, in machine learning algorithms, we need the data to be all numerical, but often the inputs that we wish to use might be, fundamentally multiple categories that are most interpretable as a single word. Like `pet` for example, we might have values for `cat`, `dog`, `fish`. But we want to represent these for an algorithm, we don't want to put them in an order, so we switch from one column to three, one for each animal. Then we put a 1 in each row. |
| 209 | + |
| 210 | +```{list-table} |
| 211 | +
|
| 212 | +* - pet |
| 213 | + - cat |
| 214 | + - dog |
| 215 | + - fish |
| 216 | +* - dog |
| 217 | + - 0 |
| 218 | + - 1 |
| 219 | + - 0 |
| 220 | +* - cat |
| 221 | + - 1 |
| 222 | + - 0 |
| 223 | + - 0 |
| 224 | +* - dog |
| 225 | + - 0 |
| 226 | + - 1 |
| 227 | + - 0 |
| 228 | +* - fish |
| 229 | + - 0 |
| 230 | + - 0 |
| 231 | + - 1 |
| 232 | +* - cat |
| 233 | + - 1 |
| 234 | + - 0 |
| 235 | + - 0 |
| 236 | +* - cat |
| 237 | + - 1 |
| 238 | + - 0 |
| 239 | + - 0 |
| 240 | +* - fish |
| 241 | + - 0 |
| 242 | + - 0 |
| 243 | + - 1 |
| 244 | +``` |
| 245 | + |
| 246 | +Now I apply some of my understanding of these operations to create categories like cat_or_dog. I also could choose to compress the data from being represented using 24 bits per row (each character 0 or 1 would be stored using 8 bits) or possibly even more by compressing it to binary and then representing it as 3 bits. |
| 247 | + |
| 248 | +Cryptographic algorithms also sometimes employ these operations. |
0 commit comments