19. How can we use logical operations?#
19.1. 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.2. Bitwise operators review#
&
: 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.3. Adding with gates#
Let’s review adding binary numbers.
remember, binary is a place-based system like the decimal placed based system you are likely familiar with
Since binary is place-based adding with binary follows the same basic algorithm
work right to left (smallest to largest place values)
add the two values in a given place
carry to the next place if >=2
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 |
a+b 2’s |
a+b 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.1. Time vs space#
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.4. 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, consider how to swap two values.
Assume we have two variables a
and b
intitialized like:
a =4
b =3
a,b
(4, 3)
The sort of intro to programming way to swap them uses a third variable:
tmp = a
a= b
b = tmp
a,b
(3, 4)
Let’s reset them
a =4
b =3
a,b
(4, 3)
Tip
Little tricks like this are sometimes the type of questions that get used in interviews, not that you should memorize all of the puzzle question answers, but that if you understand all of the concepts, you will be able to solve these sort of things in an interview
With bitwise operations we can swap them without a 3rd variable.
a = a^b
b = b^a
a = a^b
a,b
(3, 4)
If we implement this with only 3 bits we have a 1,2,4 places.
Then we xor each bit and store the result in the first register (our a variable)
and next we xor again and store in b
now the 4 has moved form A to B. If we xor one more time and store that in A,
b =874
a = 87435
a = a^b
b = b^a
a = a^b
a,b
(874, 87435)
Tip
An explore idea on this topic is to work with a hardware descriptvie language like verilog or vhdl
19.5. Experience Report Evidence#
19.6. Questions after class#
19.6.1. How does a computer relate this process to decimals/negative numbers; basically things that aren’t just integers?#
We saw floats last week, it’s also a place based even in floating point, so it still works.
19.6.2. Can a full adder be cascaded to add numbers larger than three bits?#
A full adder adds, 3 1-bit numbers, and yess it can be cascaed to add more than one bit.
19.6.3. Does the alignment of logic gates whether series/parallel impact performance, as in other electrical components like resistors, transistors, etc, alignment does matter?#
They need to be organized in the right way to execute the operation that you want.
19.6.4. Why did we learn temp varialbe swap?#
That requires less contextual knowledge, like xor, in some way and less math. It also helps teach the concept of variables, which is the main thing we want people to learn in intro classes.
19.6.5. When will physical limitations of computing be reached?#
This is a good explore topic.
19.6.6. is doing 64 bits just the same expanded?#
Yes, we only looked at up to 8 bits because it is tractable to look at.