### Check out a free preview of the full A Tour of Web 3: Ethereum & Smart Contracts with Solidity course

The "Logical Operations on Bits" Lesson is part of the full, A Tour of Web 3: Ethereum & Smart Contracts with Solidity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen demonstrates how logical operators can be used to swap values or determine if a variable contains a certain value.

### Transcript from the "Logical Operations on Bits" Lesson

[00:00:00]

>> So let's work through some logic operations. So just like how I did Boolean logic, right? With 2 ampersand signs, doing bitwise logic is simply a single ampersand sign, just like the double or is a Boolean or a single or is a Boolean or is a bitwise or.

[00:00:19]

Just like doing a bang is a not, a tilde is a bitwise not. And just like doing, I did this, even though this symbol doesn't exist, doing a single hat is an exclusive or. So let's kind of go through a couple of those operations. Let's go like this.

[00:00:38]

a=1001 in binary, b = 0101 in binary. If we were to end these two numbers together, we'd go 1 and 1, 1, 0 and 0, 0, 0 and 1, 0, 1 and 0, 0, just like our truth table that we did before. So the answer of 9 and what is this, 5, the answer is 1, it's because there's only one common, 1, notice that what does ans do?

[00:01:12]

Everything that has 1s on both sides becomes a 1 in the solution. Everything else becomes a 0. This is very important because we use ans for selecting out values. Say you wanted only the first two bits to a number. Well, what would you do say a is a very, very long number, we'd simply make b equal to 0x3.

[00:01:37]

Because 3 is represented by 2 to the 0 + 2 to the 1. So it's 2 1. You could translate that into binary, 1 1. And so then if we ended that with a, all of the bits over here would all become 0. But only the first 2 bits would be either a 0 or 1, because we already have 1s right here.

[00:01:58]

So if they happen to be both 0s, the answer would be both 0s. If they happen to be 1 of them's 1, 1 of them would be 1. Do you see where this is going? This makes a lot of sense, I would say, ans are used for selecting out information.

[00:02:11]

That is why if you ever see Boolean flags where binary hat or a number has flags in it, you will always see that single end is because that flag represents a single binary number that's on and you have your flag set that's coming in. And we're just seeing is this non 0.

[00:02:27]

If it's nonzero, that means we both have the same bits on or at least the same bit in the second that is in the first. So let's do it again with or, or just like before, only one has to be true. So if we have the value 001, and of course 0101.

[00:02:46]

And we or it, we get 1011 because this one of these are 1s so ors is how you write into a number, right? So if I wanted to write in 2 bits into the first two positions of a number, what would I do? So a is a big long, long number.

[00:03:06]

I would simply make b equal to 0x3, and I would order them together. I would write the 3 into it. It wouldn't matter what a has in that position, they're one at the end of this operation, right? Right, so hopefully everyone feels like they've been paying attention to understand this.

[00:03:26]

I'm gonna give a quick formula. If you know the answer chat, please do not type it. Everyone needs to experience this in their own hearts cuz it is one of the most magical things that you'll ever have, all right, so I'm gonna do this, classic a, being it's 0 or 1001, classic b being it's 0101.

[00:03:48]

I want you to apply these three operations to it in order. I want a to equal a, exclusive or remember only one can be true at a time. b, I want b to equal a exclusive or b. I want a to equal a exclusive or b. Work this out.

[00:04:13]

Walk through it if you have like a drawing program, go through it. This is a very magical experience. Please don't type the answer. It's a very great example and I'll go through it in just a moment. All right, so let's work through what happens. I don't wanna give anything away.

[00:04:28]

Let's work through what happens here. So, a equals this, b equals this. Let's take the x or of that and assign it to a. So a is gonna equal this. What is 1x over 1? That's 0. What is 0x or 0, 0. What is 1x or 0x or 1 1?

[00:04:48]

What is 1x or 0 1. Awesome. b equals what is 1x or 0 1. What is 0x or 0? Yes, here's some excitement. What is 1x or 1 0. What is 0x or 1, 1? Alright, let's do a again. Alright, 011000011110, look at that. Now take a quick moment, what the heck just happened to a and b?

[00:05:22]

Is your mind not just blown? We swapped the values of a and b without creating a temporary value. Come on, tell me that is not just like incredible that has to at least make you kind of like, my goodness XR has some fundamental property of memory in it.

[00:05:40]

It's why raid and a lot of the basis of raid use XR as a way cuz you can restore a disk by having an x or copy of something else somewhere. And you can actually bring things back to life cuz of this. This is just like, just like mind blowingly awesome.

[00:05:54]

I wanted to show this because I really wanted to make sure you understood bits, you understand the operation, you know that end is for selecting something out. Or is for putting something in XOR you can obviously do some really neat stuff here. I'm just hoping someone got XOR, I feel like no one I just see smiles.

[00:06:13]

I think they're just mostly think I'm awkward cuz of how excited I am at this. But this is fantastic. So there you go. Your mind is blown, right? It's fantastic. So let's rename this function, now that we have all of the brains in the world for this. What should the name b of this?

[00:06:35]

So first, a and b, let's write that out. So I'm gonna jump back to here and I'm gonna go like this. So we have a and b equals b, right? There we go. So let's just say a classic 1001. Let's say b classic 0101. The answer to this would be 1 1 as 1, 0 0, 0, 0 1 0 0 or 1 0 is 0.

[00:07:05]

Does this equal b? No, it does not equal b. Let's say a equals this and b equals this. It's 1 1 1, 0 0 0, 1 1 1, 1 0 0. Does this equal b? Yes it does, look b is that value, b is this value, that function should be called contains, does a contain b, it does contain b completely within it, [SOUND] how great is that, that was fantastic, right?

[00:07:42]

This is also just flag checking, right? So if you had a that had a bunch of a bunch of 1s in it, and you wanna see is the third bit true. All you'd have to do is go like this. Okay, I need the third bit, which that would be 0x4.

[00:07:58]

Does that equal a right? And 0x4. I wrote that a little bit terrible, but you get the idea. You're just seeing, does this one exist within this bigger pool? It's actually a pretty neat little function right here. And so there you go. You now hopefully at least get what's happening there.

### Learn Straight from the Experts Who Shape the Modern Web

- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops