How can we use logical operations?
Contents
18. How can we use logical operations?#
19. 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
19.1. Bitwise operators#
& : and
| : or
^ : xor
~ : not
: shift right
<<: shift left
Let’s review truth tables for and, or, and xor.
a |
b |
output |
---|---|---|
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
a |
b |
output |
---|---|---|
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
a |
b |
output |
---|---|---|
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
19.2. Adding with gates#
Let’s review adding binary numbers
add the two bits
carry the next place like in adding multi-digit numbers otherwise
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.
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.
let’s make a truth table for adding two bits.
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.
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 different. It adds three single bits, or a carry and two bits and outputs the result as a sum bit and a carry bit.
Then we can link many of those together to get an 8 bit ripple 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.
19.3. Review today’s class#
Compare the 2 bit multiplier to the full adder in multiplication.md
. Use comparison with the full adder to explain how the 2bit multiplier works works, relative to the fact that multiplication can be thought of like repeated addition.
19.4. Prepare for Next Class#
Read gates out of anything and watch the marble adder. Create
gates.md
and answer the questions below:1. What do all of the gates described have in common? 1. How does the marble adder compare to the half adder?
Study the 8 bit ALU. Try it out and be prepared to answer questions about it in class. Some questions to guide your exploration: What can it do? Try to compare it to the adder that we have seen. What components does it have that we have not yet seen? How does it represent different operations?
19.5. More Practice#
(priority) 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
nandhalf.md
In
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.
19.6. Questions after class#
19.6.1. why would we need the carry bit if we can do the math fine without it?#
We have the carry bit in the circuit always so that it’s available when we do need it.
19.6.2. these logic gates are used for making circuits right?#
yes
19.6.3. How do half-adders work? I am confused on how the 1 and 0’s are represented, and how things are stored#
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.
However the gate can be represented using anything with on and off positions. See the marble adder for example.
19.6.4. 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.#
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.
19.6.5. Is bitwise useful in just embedded programming or other areas of industry?#
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.
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.
pet |
cat |
dog |
fish |
dog |
0 |
1 |
0 |
cat |
1 |
0 |
0 |
dog |
0 |
1 |
0 |
fish |
0 |
0 |
1 |
cat |
1 |
0 |
0 |
cat |
1 |
0 |
0 |
fish |
0 |
0 |
1 |
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.
Cryptographic algorithms also sometimes employ these operations.