Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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.

Bitwise operators review

Let’s review truth tables for and, or, and xor.

Table 1:AND

a

b

output

0

0

0

0

1

0

1

0

0

1

1

1

Table 2:OR

a

b

output

0

0

0

0

1

1

1

0

1

1

1

1

Table 3:XOR

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

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

101+100=1001101 + 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=101010 + 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.

let’s make a truth table for adding two bits.

Table 4:Add

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.

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.

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:

a=4
b=3
a,b
(4, 3)
a,b = b,a
a,b
(3, 4)

The sort of intro to programming way to swap them uses a third variable:

tmp = a
a=b
b=tmp
a,b
(4, 3)

Let’s reset them

a =4
b =3
a,b
(4, 3)

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.

4 and 3 rpresented in binary in 2 registers labeled a and b

Then we xor each bit and store the result in the first register (our a variable)

4^3 and 3 represented in binary in 2 registers labeled a and b

and next we xor again and store in b

4^3 and 4 represented in binary in 2 registers labeled a and b

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
a=16
b=55
a=a^b
b= b^a
a=a^b
a,b
(55, 16)

Prepare for Next Class

there will be time to work on this in lab after break, get some rest and come back refreshed

  1. In fractionalbinary.md use 8 bits to represent the following numbers by using 4 bits as usual (8,4,2,1) and the other 4 bits are 1/2, 1/4, 1/8, 1/16th:

  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?

Badges

Review
Practice
  1. Review the notes from today

  2. 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 nandandor.md

  3. Find what bitwise operation would complete each of these transformations (eg 1 << 3 = 4 and 4^4 = 0) that is replace the ? with a bitwise operator. For each show work to explain how you found it in bitwise.md:

    4 ? 3 = 32
    11 ? 7 = 3
    8 ? 2 = 2

Experience Report Evidence

Questions After Today’s Class

On what components of the computer does this all happen in?

That’s what we’ll learning that next!

Is there an example within programming where one would utilize xor comparisons with n comparisons, where n >= 3? Or is there a niche and weird use for using xor on a single variable (if possible)?

I’m not quite sure what you’re asking here, but there might be an explore badge here.

how the other basic operations would look in a circuit, would they be extensions of the full adder?

We’ll be looking at a few more over the next few weeks! We’ll be doing more of this and we’ll answer.