Master Bitwise Operators: OR, AND, NOT, XOR, NOR for Beginners

Master Bitwise Operators: OR, AND, NOT, XOR, NOR for Beginners

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

Hey everybody! In this video I’m going to talk about basic logical operators such as OR and AND.

Uh so let’s let’s dive right into it. What am I talking about with the OR and AND?

Suppose for the sake of argument that this notepad is totally dark and I wanted it to be really light

and then I have to go into the system settings to fix it. Sorry let me change my theme.

How about that?

Okay.

So suppose for the sake of argument, we’ve got, you know, a couple of bits.

These are going to be a bitwise operations for the most part.

In other languages, sometimes we will say logical operator, but actually we’re just

talking about values rather than bits individually.

So these are bitwise operators.

Imagine we have like a couple of bits.

Remember the possible values for bits are just, you know, a zero and a one binary.

So suppose we have a couple of values.

values, let’s say we have a zero here and a one.

So if I apply a logical or to the zero and the one,

the result is going to be a one because either the first bit or the second bit are a one.

You could also expand these operators to have multiple sets of bits,

but we’re just going to do, you know, one set against another set.

In this case, each set is just one bit long, but we’ll do, we’ll do a more complicated stuff in a minute.

If I have a zero and a one, then the answer there is, well, let me just put or maybe.

The answer is a one.

I’ll put or maybe at the top, just like or to let you know that we’re doing ors here

on the top.

So if we have a zero and a one, the answer is going to be a one.

If we have a one and a zero, the answer is going to be a one.

Also, if we have two ones, which looks like an L over there, I got to work on my penmanship.

Then the answer is also going to be a one.

if either of the bits in question are a one then the answer should be a one. The only case with an

OR that results in a zero is if both of the bits are zero because neither the first or the second

are actually a one right so this is the basic idea of using an OR operator and you can also do this

with multiple bits at the same time like for example we could say let’s take a bunch of random

work on the it’s the pressure dude the l’s are driving me crazy okay oh my god okay so i do that

and i let go okay i’m just going to put some random bits uh underneath the the first set of

random bits put like a zero here and then like a one here maybe and we’ll say that we’re going to

do a logical or just like you might think of doing a you know addition or subtraction we’ll just say

you know, addition or subtraction. We’ll just say we’re going to or these two sets of bits.

Actually, these are six on the top and six on the bottom. If I add two more, then it’ll be a nice

little bite for the top and the bottom. So I’m just going to do that, you know, to have eight

bits on the top and eight bits on the bottom. Okay. So if I just want to logically or two sets

of bits, literally, I just have to do one pair of bits at a time. So a one or zero is just a one.

One or zero is just a one, a zero or one is a one, a one or zero is a one, a zero or one

is a one.

And then these ones, everything is just a one.

Okay.

So that was too easy.

Maybe if I, I don’t know, maybe if I change one of these ones to a zero, then we would

actually be able to have a zero somewhere in here.

So that’s a logical or a bitwise or operator or operation.

So you can, you can kind of use this in various ways.

can kind of use this in various ways i mean obviously if you want to manipulate some bits

this can be pretty useful inside of the machine but there was kind of an old school way that

people did flags with arguments they would basically say something like this they would go

in their computer program they would say you know flag a name it something like turn on the display

or enable caching or just like whatever right so we’ll just have a flag we’ll call it flag a

that value is equal to a one and then maybe flag b its value is equal to a two and then flag c

its value would be a four and then we basically just double the value of the flag and the reason

for that c d e f i’ll just stop with f maybe oh wait that was supposed to be 16 and that’s 32.

the reason for that is if you double the value of each flag so that uh

and then you can do a bitwise or against all of these flags and sort of combine multiple flags.

Because if you think about it, if we’re talking about binary,

if we suppose this is just a regular eight bit number, then, you know, the first bit has a

strength of one. If you’ve seen my other videos for converting between binary and hex,

the second bit has a strength of two. The next bit has a strength of four and then eight and

and then 32 and 64 and then 128.

And so if you look at the flags,

1, 2, 4, 8, 16, 32,

they map to only one bit.

It’s not going to be some kind of random pattern for these numbers.

It’s literally going to be flag A is going to look like this.

1, 2, 3, 4, 5, 6, 7, 8.

And then flag B is going to look like this.

1, 2, 3, 4, 5, 6, 7, 8.

five, six, seven, eight. And flag D is going to look like this. Whoops. Let me make that green.

Still getting used to this. Oh, sorry. C is the next one. One, two, three, four, five,

six, seven, eight. Can you see the pattern here? Let me just do D and be done with it at this

point. One, two, three, four, five, six, seven, eight. So because we’re increasing the value of

the flags by a power of two, or by a power of two, they’ll always correspond to just one bit.

they’ll always correspond to just one bit, which means if you apply a logical or operation,

you can represent a bunch of different flags with just one number.

Think about this.

What if I wanted to have, oh, I don’t know.

Let me erase this.

What if I wanted to have some, let’s say I wanted flag A and flag D and flag F just for

the sake of argument.

Dang, this pen.

It’s actually my fault completely, but I need to practice.

but I need to practice F okay and DNF so that would basically be you know 1 2 3 4 5 6 7 8

and then D would be the 8 so a 1 0 0 0 there and then a bunch of zeros elsewhere and then the F

So if we did an OR between all of those, then the OR is just basically 00101001.

And if you convert that from binary to decimal, what is that?

That’s a 1248 plus 8 plus 1248 and then a 32.

So 41.

just write down the number 41 and if the programmer is smart enough to you know parse all the bits

and everything then they’ll know that you have flags a and d and f set that’s not necessarily

something that all the modern programs do anymore it was much more popular in the olden days but i

think some people still kind of do it especially if they’re programming in c and other languages

where they want speed it’s pretty fast to look at bits inside of the machine so keep that in mind

Keep that in mind now we know how to do an OR operation and we know one reason that it might be useful and

Yeah, okay. I’m gonna move on to the next operation

So clearing the screen now

Hello little doggy he barked and now he’s in the room

Let’s do

The logical AND operation okay, so suppose we have two bits here

in a one only if both of the input bits are a one i’m gonna whoops i accidentally cleared the whole

entire thing let me forget where my red pen is okay and okay so if we have a zero and a zero

the result is zero because neither of those are a one if we have a zero and a one the result is

still going to be zero because we need both bits to be a one to result in a one so then that means

would also result in a zero. So you get a bunch of zeros until finally you have an input pattern

that is just two ones. And then finally the result is one. So that’s a logical and.

Let me do a quick example so we can and a bunch of bits together. I’m just going to do more

random bit patterns. So there’s another L there. I’m cringing. I’m self-cringing. One, two, three,

okay so then i’ll do like another one there and like a zero there and a zero maybe a couple of

ones maybe three ones and then a zero and then one i guess and we’ll just say that we’re going

to end those together so and oh man i’m mixing uppercase and lowercase like crazy

so uh to end them together you know that bit position right here that’s going to be a one

and then we have a zero because they don’t match they’re not both one and then we have another zero

a one and then we have a zero and a zero and a zero and a one so this is a logical and you can

use logical ands for a wide variety of things but one thing you can kind of do somewhat easily is to

use one number as a mask against another number for example notice how uh suppose this was the

input number let’s just say we have the input number up top and maybe you wanted to make sure

because you’re you’re checking for those bits individually or maybe you want to just prevent

the input from having certain bits on for some other reason well you can you can apply the mask

as the second bit and use a logical and i’ll say mask here sorry you can apply a mask as the second

bit pattern and notice how in the answer there are only ones where both you know the input and

means if I wanted to just control only which ones are allowed I can use the mask notice how the mask

has ones in these positions and there are definitely no zeros sorry there are definitely

no ones in any position where a mask had a zero so the mask can kind of like you know mute or mask

or control the input pattern if that makes sense hopefully another thing we could do if we wanted

if one bit exists. Let me go ahead and clear this real fast. I’m going to just do maybe four

bits this time. So we could obviously do an and against four zeros, and this would result in just,

you know, a resulting pattern of zero. So this is not super useful. The result would just

definitely be four zeros. But notice again how the second pattern is sort of controlling what’s

allowed to go through from the first pattern. Suppose I wanted to check to see if that particular

particular bit was on or off.

All I have to do is mask it with a one in the correct pattern.

So, you know, the second bit that I just wrote down on the bottom row,

I just want to see if that second bit is on or off in the input pattern.

I don’t care about any of the other bits.

That means the result will be this.

So now it’s much more easy for me to see if that bit is on.

I could just compare the value of those bits to a regular integer.

Like, for example, if we consider that this was the one and this was the two

considered that this was the one and this was the two and this was the four or sorry four and then

the eight I could just ask now if the final value equals two and then I would know if that bit was

on or not in the original pattern and so if I wanted to check to see if this other you know

bit was on I can move this over here and set that back to a zero so the resulting pattern would be

zeros then I’d ask myself is the resulting value equal to four no it’s equal to zero and so then

Another thing you can do that I’m going to talk about in a future video is just sort of shift the bits over.

You could, instead of checking to see if the value was a 4 or like a direct integer,

you could just sort of shift over a certain number of spots and then check to see if it was equal to 1 or 0.

Let me put this 1 back over into its original position just so that we’ll have something that had a 1.

is one from uh you know the right side if it’s one to the left from the right side

then i could just shift all the bits one to the right and end up with a pattern that is zero zero

zero one i’m not going to talk about bit shifting operations in this video but just know that you can

and you’ll see more of it in a future video so then you know this is another way to check to

see if a bit was on first you do a mask for that bit and then you shift bits and then you just

check to see if the final value is a zero or one which may or may not feel easier than checking for

or may not feel easier than checking for an actual integer in your program.

Okay, so we’ve done OR and also AND. The next bit pattern is called a NOT, which is really easy.

It’s literally just an inversion of the original bit pattern. So for the NOT, we don’t really take

two bits against each other. We just take an input bit and we just invert it. We’ll just say

So that’s a knot. Pretty easy. We could also knot, you know, a random bit pattern with Ls.

So I swear, somebody’s going to show up and this is their first computer science video.

They’re going to go tell their parents, hey, there’s Ls in computer science.

So sorry. I’m so sorry. Okay. So I got four there. So if I knot those, then that’s pretty easy.

It’s just going to be the opposite, you know, an inversion.

you know, an inversion.

One, zero, one, zero.

Let me just double check that I did that correctly.

Yeah, so that’s the knot pattern.

No big deal.

Pretty easy.

So then maybe the next thing that we can talk about is the nor,

which just means not or.

So let me draw that out real fast just to show you what I’m talking about.

So if we do nor, pretty much you can imagine first taking the or and then knotting it.

So inverting the or.

the OR. So for example, if we have two bits here, actually, let me, let me do, let me do a

sequential counter. Cause I think that’s more fun. I’m counting right now from one to a three

or a actually, I do that in the last one. I started with zero. I hope let’s count from zero

to three. So zero and then one and then two and then three. So I’m counting in binary.

two input bits and we’re going to do a nor then the first thing we probably want to do is take

the or operator so that would be a zero here i’ll just put or in parentheses just to let you know

that this is kind of a nice first step you could memorize nor if you wanted to but i don’t i don’t

really remember how to do it without doing these steps so uh it’s going to be ones for everything

else because in the or you get a one if either of those is a one and then uh the next step is you

the or i’ll just put nor here just to let you well just to let you know that you’re uh well i

don’t know if i put nor then it kind of sounds like i’m norring that one bit doesn’t it so maybe

here i’ll say uh not just to let you know that this is just one step against the previous bit

so the knot is uh just going to be an inversion and so the nor of zero zero is one the nor of zero

nor of 1 1 is 0 and that’s how you do a nor and all of these logical or bitwise

operators can be used in circuits and circuit design and just you know other

types of logical operations I’m gonna move on let’s see the next one is gonna

be XOR that I wanted to talk about in this video so what is XOR it just means

exclusive or so it’s a little bit more complicated than or this is actually a

one because it has a history with neural networks. When you have a neural network that does not have

more than one, or sorry, if you have a neural network that does not have any hidden layers,

like if you just have an input layer and an output layer, then the neural network should

actually not be able to solve XOR. It should not be able to learn it. You have to increase the

topology of the neural network in some way, make it a little bit more complicated. For example,

one or more middle layers between the input and the output layers. If you understand neural

networks, it’s just kind of fun to know, oh, XOR stumps a shallow network and kind of starts to

prove that deep networks might be a little bit smarter or deepish at least. Okay. So XOR,

let’s start off by counting here. I’m going to do zero and zero, and then this is the number one,

and this is the number two, and this is the number three. And exclusive OR just basically means

just basically means an or but if both of the bits are one then it’s a zero so that means

either of the bits can be a one to produce a one but it must be exclusive the bit that is a one

can’t also be next to another bit that’s a one so let me show you so in is in a double zero

the answer is just going to be a zero in a zero and one the answer is going to be a one in a one

exclusive, the one is it has a, it has a buddy or a partner.

It’s not an exclusive one.

So we’ll say that that’s a zero.

So that’s the XOR.

I don’t know.

You could put like a, a, a not on top of that or an and on top of that or whatever

you wanted to do.

And we can make stuff that’s a lot more complicated, but these are the basics of

or and not XOR and NOR as bitwise operators.

So thank you for watching this video.

That’s all I have to say for now.

See you in the next one.

Hope you learned a little bit and had a little bit of fun.

I’m out.

social media website that you’re looking at right now it would really mean the world to me and it’ll

help make more videos and grow this community so we’ll be able to do more videos longer videos

better videos or just i’ll be able to keep making videos in general so please do do me a kindness

and uh and subscribe you know sometimes i’m sleeping in the middle of the night and i just

wake up because i know somebody subscribed or followed it just wakes me up and i get filled

with joy that’s exactly what happens every single time so you could do it as a nice favor to me or

it as a nice favor to me or you could you could troll me if you want to just wake me up in the

middle of the night just subscribe and then i’ll i’ll just wake up i promise that’s what will

happen also uh if you look at the middle of the screen right now you should see a qr code which

you can scan in order to go to the website which i think is also named somewhere at the bottom of

this video and it’ll take you to my main website where you can just kind of like see all the videos

i published and the services and tutorials and things that i offer and all that good stuff and

if you have a suggestion for uh uh clarifications or errata or just future videos that you want to

see please leave a comment or if you just want to say hey what’s up what’s going on you know

just send me a comment whatever i also wake up for those in the middle of the night i get i wake up

in a cold sweat and i’m like it would really it really mean the world to me i would really

appreciate it so again thank you so much for watching this video and um enjoy the cool music

the cool music as I fade into the darkness which is coming for us all.

Thank you.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply