Skip to playerSkip to main contentSkip to footer
  • 2 days ago
Want to understand bitwise operators? This video dives into OR, AND, NOT, XOR, and NOR with easy-to-follow examples. Learn how to use these operators for flags, masks, and more in programming. Perfect for beginners or anyone wanting to master binary logic in C or other languages. I break it down with real examples, a touch of humor, and no fluff. Subscribe for more coding tutorials, and check out my site for extra resources. Drop a comment with your questions or video ideas!

Introduction to Logical Operators 00:00:00
Explanation of Bitwise OR 00:00:35
OR Operation Examples 00:01:01
Bitwise OR with Multiple Bits 00:02:12
Using OR for Flags 00:03:52
Introduction to Logical AND 00:08:06
AND Operation Examples 00:08:29
Using AND as a Mask 00:10:00
Checking Specific Bits with AND 00:11:17
Introduction to NOT Operation 00:13:36
Introduction to NOR Operation 00:14:38
Introduction to XOR Operation 00:16:37
XOR and Neural Networks 00:16:53
Conclusion and Call to Action 00:18:38

Thanks for watching!

Find us on other social media here:
- https://www.NeuralLantern.com/social

Please help support us!

- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras

Transcript
00:00Hey everybody.
00:01In this video I'm going to talk about basic logical operators such as OR and AND.
00:13So let's dive right into it.
00:15What am I talking about with an OR and an AND?
00:17Suppose for the sake of argument that this notepad is totally dark and I wanted it to
00:21be really light and then I have to go into the system settings to fix it.
00:25Sorry, let me change my theme.
00:27Graybird, how about that, okay.
00:31So suppose for the sake of argument we've got, you know, a couple of bits.
00:35These are going to be bitwise operations for the most part.
00:39In other languages sometimes we will say logical operator but actually we're just talking about
00:43values rather than bits individually.
00:46So these are bitwise operators.
00:49Imagine we have like a couple of bits.
00:50Remember the possible values for bits are just, you know, a zero and a one binary.
00:55So suppose we have a couple of values.
00:57Let's say we have a zero here and a one.
01:01So if I apply a logical OR to the zero and the one, the result is going to be a one because
01:08either the first bit or the second bit are a one.
01:12You could also expand these operators to have multiple sets of bits, but we're just going
01:16to do, you know, one set against another set.
01:19In this case each set is just one bit long, but we'll do, we'll do a more complicated stuff
01:23in a minute.
01:24So if I have a zero and a one, then the answer there is a, well, let me just put OR maybe.
01:31The answer is a one.
01:32I'll put OR maybe at the top.
01:33Just like OR to let you know that we're doing ORs here on the top.
01:37So if we have a zero and a one, the answer is going to be a one.
01:40If we have a one and a zero, the answer is going to be a one.
01:44Also if we have two ones, which looks like an L over there, I got to work on my penmanship,
01:50then the answer is also going to be a one.
01:51So the rule is if either of the bits in question are a one, then the answer should be a one.
01:57The only case with an OR that results in a zero is if both of the bits are zero because
02:03neither the first or the second are actually a one, right?
02:07So this is the basic idea of using an OR operator.
02:11And you can also do this with multiple bits at the same time.
02:14Like for example, we could say, let's take a bunch of random bits here.
02:18Oh, dude, I really need to work on the, it's the pressure, dude, the Ls are driving me crazy.
02:26Okay.
02:27Oh my God.
02:31Okay.
02:32So I do that and then I let go.
02:34Okay.
02:35I'm just going to put some random bits underneath the first set of random bits, but like a zero
02:40here and then like a one here, maybe.
02:43And we'll say that we're going to do a logical OR, just like you might think of doing a addition
02:48or subtraction.
02:49We'll just say, we're going to OR these two sets of bits.
02:53Usually these are six on the top and six on the bottom.
02:56If I add two more, then it'll be a nice little, uh, bite for the top and the bottom.
03:02So I'm just going to do that, you know, to have eight bits on the top and eight bits on
03:06the bottom.
03:07Okay.
03:08Okay.
03:09So if I just want to logically OR two sets of bits, literally, I just have to do one pair
03:13of bits at a time.
03:15So a one or zero is just a one, the zero or one is a one, a one or zero is a one, zero
03:23or one is a one.
03:24And then these ones, everything is just a one.
03:27Okay.
03:28So that was too easy.
03:29Maybe if I, uh, I dunno, maybe if I change one of these ones to a zero, then we would
03:33actually be able to have a zero somewhere in here.
03:37So that's a logical or a bitwise OR operator or operation.
03:42So uh, you can, you can kind of use this in various ways.
03:45I mean, obviously if you want to manipulate some bits, uh, this can be pretty useful inside
03:49of the machine.
03:50Um, there was kind of an old school way that people did flags with arguments.
03:55They would basically say something like this.
03:57They would go in their computer program.
04:00They would say, you know, flag a name it something like turn on the display or enable caching or
04:08just like whatever.
04:09Right?
04:10So we'll just have a flag.
04:11We'll call it flag.
04:12A we could say that that value is equal to a one and then maybe flag B its value is equal
04:16to a two and then flag C its value would be a four.
04:19And then we basically just double the value of the flag.
04:23And the reason for that C D E F I'll just stop with F maybe, oh wait, that was supposed
04:29to be 16 and that's 32.
04:31The reason for that is if you double the value of each flag so that, uh, uh, every flags actual
04:38value has a power of two, then you can, then you can do a bitwise OR against all of these
04:42flags and sort of combine multiple flags.
04:45Because if you think about it, if we're talking about binary, uh, if we suppose this is just
04:51a regular eight bit, uh, a number, then, you know, the first bit has a strength of one.
04:57If you've seen my other videos for converting between binary and hex, the second bit has
05:01a strength of two.
05:02The next bit has a strength of four and then eight and then 16 and then 32 and 64 and then
05:13128.
05:15And so if you look at the flags, one, two, four, eight, uh, uh, 1632, they map to only
05:21one bit.
05:22It's not going to be some kind of random pattern for these numbers.
05:25It's literally going to be flag A is going to look like this.
05:28One, two, three, four, five, six, seven, eight.
05:31And then flag B is going to look like this one, two, three, four, five, six, seven, eight.
05:38And flag D is going to look like this.
05:40Whoops.
05:41Let me make that green.
05:42Still getting used to this.
05:43Oh, sorry.
05:44Uh, C is the next one.
05:46One, two, three, four, five, six, seven, eight.
05:51Can you see the pattern here?
05:52Let me just do D and be done with it at this point.
05:54One, two, three, four, five, six, seven, eight.
05:58And because we're increasing the value of the flags by a power of two, they'll, they'll
06:04always correspond to just one bit, which means if you apply a logical or operation, you can
06:10represent a bunch of different flags with just one number.
06:13Think about this.
06:14What if I wanted to have, Oh, I don't know.
06:18Let me erase this.
06:19What if I wanted to have some, uh, let's say I wanted flag, uh, A and flag D and flag
06:27F just for the sake of argument, dang, this pen.
06:30It's actually my fault completely, but I need to practice F and D and F.
06:40So that would basically be, you know, one, two, three, four, five, six, seven, eight.
06:46And then D would be the eight.
06:47So a one, zero, zero, zero there, and then a bunch of zeros elsewhere.
06:52And then the F would be 32.
06:55So that's a zero, zero, one, zero, zero, one, and then a bunch of zeros.
07:00So if we did an, or between all of those, then the, or is just basically zero, zero, one,
07:09zero, one, zero, zero, one.
07:12And if you convert that, uh, from binary to decimal, what is that?
07:16It's a one, two, four, eight plus eight plus a one, two, four, eight, and then, um, a 32.
07:24So 41.
07:27So literally you can just write down the number 41.
07:30And if the programmer is smart enough to, you know, parse all the bits and everything,
07:34then, uh, they'll know that you have flags A and D and F set.
07:39That's not necessarily something that all the modern programs do anymore.
07:42That was much more popular in the olden days, but, uh, I think some people still kind of
07:46do it, especially if they're programming in C and, uh, other languages where they want
07:51speed.
07:52Uh, it's pretty fast to look at bits inside of the machine.
07:55So keep that in mind.
07:57Now we know how to do an or operation and we know one reason that it might be useful and,
08:01uh, yeah.
08:02Okay.
08:03I'm going to move on to the next operation.
08:06So clearing the screen now, hello, little doggy.
08:10He barked and now he's in the room, um, let's do the logical and operation.
08:18Okay.
08:19So suppose we have a two bits here.
08:22The end operation will result in a one only if both of the input bits are a one.
08:27I'm going to, whoops.
08:28I accidentally cleared the whole entire thing.
08:30Let me figure out where my red pen is.
08:33Okay.
08:34And okay.
08:35So if we have a zero and a zero, the result is zero because neither of those are a one.
08:40If we have a zero and a one, the result is still going to be zero because we need both
08:46bits to be a one to result in a one.
08:48So then that means the one and a zero input bit, not L one, uh, would also result in a zero.
08:55So you get a bunch of zeros until finally you have an input pattern that is just two ones.
09:02And then finally the result is one.
09:04So that's a logical and let me do a quick example so we can add a bunch of bits together.
09:10I'm just going to do more random bit patterns.
09:13So there's another L there.
09:16I'm cringing himself cringing one, two, three, four, one, two, three, four.
09:20Okay.
09:21I'm going to do like another one there and like a zero there and a zero, maybe a couple
09:25of ones, maybe three ones and then a zero and then one, I guess.
09:29And we'll just say that we're going to end those together.
09:32So, and oh man, I'm mixing uppercase and lowercase like crazy.
09:39So to end them together, you know, that bit position right here, that's going to be a one.
09:44And then we have a zero cause they don't match.
09:45They're not both one.
09:46And then we have another zero and then we have a one and then we have a zero and a zero
09:50and a zero and a one.
09:52So this is a logical end.
09:54You can use logical ends for a wide variety of things.
09:57But one thing you can kind of do somewhat easily is to use one number as a mask against
10:03another number.
10:04For example, notice how, uh, suppose this was the input number.
10:08Let's just say, uh, we have the input number up top and maybe you wanted to make sure that
10:14only certain bits were turned on maybe because you're, you're checking for those bits individually,
10:19or maybe you want to just prevent the input from having certain bits on for some other
10:23reason.
10:24Well, you can, you can apply the mask as the second bit and use a logical end I'll say mask
10:30here.
10:31Sorry.
10:32You can play a mask as the second bit pattern and notice how in the answer, there are
10:38there are only ones where both, you know, the input and the mask pattern had ones, which
10:44means if I wanted to just control only which ones are allowed, I can use the mask.
10:49Notice how the mask has ones in these positions.
10:54And there are definitely no zeros, uh, sorry, there are definitely no ones in any position
10:59where a mask had a zero.
11:00So the mask can kind of like, you know, mute or mask or control, uh, the input pattern.
11:07If that makes sense.
11:08Hopefully.
11:09Another thing we could do if we wanted to just check to see if one bit exists, uh, let
11:14me go ahead and clear this real fast.
11:16I'm going to just do maybe four bits this time.
11:20So we could obviously do an and against four zeros and this would result in just, you know,
11:25a resulting pattern of zero.
11:28So this is not super useful.
11:29The result would just definitely be four zeros, but notice again, how the second pattern
11:34is sort of controlling what's allowed to go through from the first pattern.
11:38Suppose I wanted to check to see if that particular bit was on or off.
11:41All I have to do is mask it with, with a one in the correct pattern.
11:45So, you know, the second bit that I just wrote down on the bottom row, I just want to see
11:50if that second bit is on or off in the input pattern.
11:52I don't care about any of the other bits.
11:54That means the result will be this.
11:59So now it's much more easy for me to see if that bit is on.
12:03I could just compare the value of those bits to a regular integer.
12:06Like for example, if we considered that this was the one and this was the two and this was
12:10the four or sorry, four and then the eight, I could just ask now if the final value equals
12:16two.
12:17And then I would know if that bit was on or not in the original pattern.
12:21And so if I wanted to check to see if this other, you know, bit was on, I can move this
12:24over here and set that back to a zero.
12:26So the resulting pattern would be zeros.
12:28Then I'd ask myself, is the resulting value equal to four?
12:32No, it's equal to zero.
12:33And so then I could know definitely that bit was on or off.
12:37Another thing you can do that I'm going to talk about in a future video is just sort
12:39of shift the bits over.
12:42You could, instead of checking to see if the value was a four or like a direct integer,
12:48you could just sort of shift over a certain number of spots and then check to see if it
12:51was equal to one or zero.
12:53Let me put this one back over into its original position just so that we'll have something
12:57that had a one.
12:59So if I have zero, zero, one, zero, if I know that the bit that I'm checking for is, is
13:04one from, uh, you know, the right side, if it's one to the left from the right side,
13:09then I could just shift all the bits one to the right and end up with a pattern that is
13:12zero, zero, zero, one, I'm not going to talk about a bit shifting operations in this video,
13:18but just know that you can and you'll see more of it in a future video.
13:21So then, you know, this is another way to check to see if a bit was on first, you do
13:25a mask for that bit and then you shift bits and then you just check to see if the final
13:30value is a zero or a one, which may or may not feel easier than checking for an actual
13:34integer in your program.
13:37So we've done or and also and the next bit pattern is called a knot, which is really easy.
13:45It's literally just an inversion of the original bit pattern.
13:50So for the knot, we don't really take two bits against each other.
13:53We just take an input bit and we just invert it.
13:56We'll just say a zero becomes a one and a one becomes a zero.
14:00So that's a knot.
14:02Pretty easy.
14:03It's also not, you know, a random bit pattern with L's.
14:10So I swear somebody is going to show up and this is their first computer science video
14:13and they're going to go tell their parents, Hey, there's L's in computer science.
14:20So sorry.
14:21I'm so sorry.
14:22Okay.
14:23So I got four there.
14:24So if I knot those, then that's pretty easy.
14:26It's just going to be the opposite.
14:28You know, an inversion one, zero, one, zero, let me just double check that I did that correctly.
14:34Yeah.
14:35So that's the knot pattern.
14:36No big deal.
14:37Pretty easy.
14:38So then maybe the next thing that we can talk about is the nor, which just means not or.
14:47So let me draw that out real fast just to show you what I'm talking about.
14:49We do nor pretty much, you can imagine first taking the or and then knotting it.
14:55So inverting the or.
14:57So for example, if we have a two bits here, actually, let me, let me do, let me do a sequential
15:04counter because I think that's more fun.
15:06I'm counting right now from one to a three or a actually I do that in the last one.
15:13I started with zero, I hope let's count from zero to three.
15:17So zero and then one and then two and then three.
15:21So I'm counting in binary.
15:23So if we have two input bits and we're going to do a nor, then the first thing we probably
15:27want to do is take the or operator.
15:29So that would be a zero here.
15:30I'll just put or in parentheses just to let you know that this is kind of a nice first
15:35step.
15:36You could memorize nor if you wanted to, but I don't, I don't really remember how to do
15:38it without doing these steps.
15:40So it's going to be ones for everything else because in the or you get a one, if either
15:47of those is a one.
15:49And then the next step is you, you knot the or I'll just put nor here just to let you,
15:56just to let you know that you're a, well, I don't know if I put nor, then it kind of
16:00sounds like I'm gnawing that one bit, doesn't it?
16:02So maybe here I'll say a knot just to let you know that this is just one step against the
16:07previous bit.
16:08So the knot is just going to be an inversion.
16:13And so the nor of zero, zero is one.
16:16The nor of zero, one is zero.
16:18The nor is one, zero is zero.
16:20The nor of one, one is zero.
16:22And that's how you do a nor.
16:25And all of these logical or bitwise operators can be used in circuits and circuit design and
16:31just, you know, other types of logical operations.
16:33So I'm going to move on.
16:36Let's see.
16:37The next one is going to be XOR that I wanted to talk about in this video.
16:39So what is XOR?
16:41It just means exclusive or.
16:44So it's a little bit more complicated than or.
16:46This is actually a fun one because it has a history with neural networks.
16:51With when you have a neural network that does not have more than one or sorry, if you have
16:56a neural network that does not have any hidden layers, like if you just have an input layer
16:59and an output layer, then the neural network should actually not be able to solve XOR.
17:05It should not be able to learn it.
17:08You have to increase the topology of the neural network in some way, make it a little bit more
17:12complicated.
17:13For example, adding a middle layer, one or more middle layers between the input and the
17:18output layers.
17:19If you understand neural networks, it's just kind of fun to know, oh, XOR stumps a shallow
17:23network and kind of starts to prove that deep networks might be a little bit smarter.
17:29Or deepish, at least.
17:33Okay, so XOR, let's start off by counting here.
17:35I'm going to do zero and zero.
17:37And then this is the number one.
17:39And this is the number two.
17:40And this is the number three.
17:42And exclusive or just basically means an or.
17:46But if both of the bits are one, then it's a zero.
17:50So that means either of the bits can be a one to produce a one.
17:57But it must be exclusive.
17:58The bit that is a one can't also be next to another bit that's a one.
18:02So let me show you.
18:03So in a double zero, the answer is just going to be a zero.
18:06In a zero and one, the answer is going to be a one.
18:08In a one zero, the answer is going to be a one.
18:10And when we have two ones, it's not exclusive.
18:13The one is it has a it has a buddy or a partner.
18:16It's not an exclusive one.
18:17And so we'll say that that's a zero.
18:19So that's the XOR.
18:23I don't know.
18:24You could put like a not on top of that or an and on top of that or whatever you wanted
18:28to do.
18:28And we can make stuff that's a lot more complicated.
18:30But these are the basics of or and not XOR and NOR as bitwise operators.
18:37So thank you for watching this video.
18:40That's all I have to say for now.
18:41See you in the next one.
18:42Hope you learned a little bit and had a little bit of fun.
18:45I'm out.
18:46Hey, everybody.
18:52Thanks for watching this video again from the bottom of my heart.
18:55I really appreciate it.
18:56I do hope you did learn something and have some fun.
18:59If you could do me a please a small little favor, could you please subscribe and follow
19:04this channel or these videos or whatever it is you do on the current social media website
19:09that you're looking at right now?
19:10It would really mean the world to me and it'll help make more videos and grow this community.
19:15So we'll be able to do more videos, longer videos, better videos, or just I'll be able
19:19to keep making videos in general.
19:21So please do do me a kindness and and subscribe.
19:25You know, sometimes I'm sleeping in the middle of the night and I just wake up because I know
19:29somebody subscribed or followed.
19:31It just wakes me up and I get filled with joy.
19:33That's exactly what happens every single time.
19:35So you could do it as a nice favor to me or you could you could troll me if you want to
19:38just wake me up in the middle of the night, just subscribe and then I'll just wake up.
19:42I promise that's what will happen.
19:43Also, if you look at the middle of the screen right now, you should see a QR code, which
19:49you can scan in order to go to the website, which I think is also named somewhere at the
19:53bottom of this video.
19:54And it'll take you to my main website where you can just kind of like see all the videos
19:58I published and the services and tutorials and things that I offer and all that good stuff.
20:02And if you have a suggestion for clarifications or errata or just future videos that you want
20:12to see, please leave a comment or if you just want to say, hey, what's up, what's going on?
20:16You know, just send me a comment, whatever.
20:18I also wake up for those in the middle of the night.
20:20I get, I wake up in a cold sweat and I'm like, it would really, it really mean the world
20:25to me.
20:25I would really appreciate it.
20:26So again, thank you so much for watching this video and enjoy the cool music as, as I fade
20:34into the darkness, which is coming for us all.

Recommended