Bitwise Operators

12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€96.21
List Price:  €134.70
You save:  €38.49
£79.77
List Price:  £111.68
You save:  £31.91
CA$143.95
List Price:  CA$201.54
You save:  CA$57.58
A$160.33
List Price:  A$224.47
You save:  A$64.14
S$135.93
List Price:  S$190.31
You save:  S$54.37
HK$776.63
List Price:  HK$1,087.32
You save:  HK$310.68
CHF 90.06
List Price:  CHF 126.09
You save:  CHF 36.02
NOK kr1,139.30
List Price:  NOK kr1,595.07
You save:  NOK kr455.76
DKK kr717.87
List Price:  DKK kr1,005.05
You save:  DKK kr287.17
NZ$177.29
List Price:  NZ$248.21
You save:  NZ$70.92
د.إ367.26
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,945.58
List Price:  ৳16,724.30
You save:  ৳4,778.71
₹8,518.63
List Price:  ₹11,926.43
You save:  ₹3,407.79
RM448.65
List Price:  RM628.13
You save:  RM179.48
₦154,771.52
List Price:  ₦216,686.32
You save:  ₦61,914.80
₨27,852.21
List Price:  ₨38,994.21
You save:  ₨11,141.99
฿3,415.60
List Price:  ฿4,781.97
You save:  ฿1,366.37
₺3,525.07
List Price:  ₺4,935.24
You save:  ₺1,410.17
B$620.23
List Price:  B$868.35
You save:  B$248.12
R1,852.48
List Price:  R2,593.54
You save:  R741.06
Лв188.12
List Price:  Лв263.38
You save:  Лв75.25
₩146,004.73
List Price:  ₩204,412.46
You save:  ₩58,407.73
₪366.53
List Price:  ₪513.16
You save:  ₪146.62
₱5,844.26
List Price:  ₱8,182.20
You save:  ₱2,337.93
¥15,707.80
List Price:  ¥21,991.55
You save:  ¥6,283.75
MX$2,012.91
List Price:  MX$2,818.16
You save:  MX$805.24
QR364.43
List Price:  QR510.22
You save:  QR145.78
P1,382.66
List Price:  P1,935.78
You save:  P553.12
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,089.49
List Price:  E£7,125.49
You save:  E£2,036
ብር12,725.10
List Price:  ብር17,815.66
You save:  ብር5,090.55
Kz91,190.88
List Price:  Kz127,670.88
You save:  Kz36,480
CLP$99,038.09
List Price:  CLP$138,657.29
You save:  CLP$39,619.20
CN¥729.75
List Price:  CN¥1,021.68
You save:  CN¥291.93
RD$6,065.06
List Price:  RD$8,491.33
You save:  RD$2,426.27
DA13,522.10
List Price:  DA18,931.49
You save:  DA5,409.38
FJ$232
List Price:  FJ$324.81
You save:  FJ$92.81
Q770.19
List Price:  Q1,078.30
You save:  Q308.10
GY$20,913.42
List Price:  GY$29,279.63
You save:  GY$8,366.20
ISK kr13,961.60
List Price:  ISK kr19,546.80
You save:  ISK kr5,585.20
DH1,005.73
List Price:  DH1,408.06
You save:  DH402.33
L1,844.84
List Price:  L2,582.85
You save:  L738.01
ден5,920.21
List Price:  ден8,288.54
You save:  ден2,368.32
MOP$800
List Price:  MOP$1,120.03
You save:  MOP$320.03
N$1,842.15
List Price:  N$2,579.09
You save:  N$736.93
C$3,678.44
List Price:  C$5,149.97
You save:  C$1,471.52
रु13,621.60
List Price:  रु19,070.79
You save:  रु5,449.18
S/372.86
List Price:  S/522.02
You save:  S/149.15
K405.52
List Price:  K567.74
You save:  K162.22
SAR375.60
List Price:  SAR525.86
You save:  SAR150.25
ZK2,766.47
List Price:  ZK3,873.18
You save:  ZK1,106.70
L478.77
List Price:  L670.30
You save:  L191.52
Kč2,420.15
List Price:  Kč3,388.31
You save:  Kč968.16
Ft39,675.18
List Price:  Ft55,546.85
You save:  Ft15,871.66
SEK kr1,106.30
List Price:  SEK kr1,548.87
You save:  SEK kr442.56
ARS$102,565.23
List Price:  ARS$143,595.42
You save:  ARS$41,030.19
Bs692.24
List Price:  Bs969.16
You save:  Bs276.92
COP$438,826.54
List Price:  COP$614,374.71
You save:  COP$175,548.17
₡50,760.09
List Price:  ₡71,066.16
You save:  ₡20,306.06
L2,539.15
List Price:  L3,554.92
You save:  L1,015.76
₲780,390.60
List Price:  ₲1,092,578.06
You save:  ₲312,187.46
$U4,451.49
List Price:  $U6,232.27
You save:  $U1,780.77
zł411.19
List Price:  zł575.68
You save:  zł164.49
Already have an account? Log In

Transcript

Hello, welcome to the 18th tutorial on the c++ programming series. In this part we're going to look at bitwise operators. So they was operator as the name suggests, so if you've done computer science programming before is we're affecting the bits of our variable values and so forth bit ik ones and zeros binary. And they the lowest level in computers because everything eventually just goes down to one. And they all have this is what you're using computers and whatnot. And microwave within the electronics is all built on top of ones and zeros on top of several of the layers, which we would generally interact with, but their ROI, then the need may arise to interact with the bit itself.

So let's go ahead and open up our project to show you the different bitwise operators. To truly get grasp of binary and bitwise. You really need to look into that two's complement. And cetera and the general binary a quick Google search and a general read for half an hour an hour will give you a good overview of what it's all about this tutorial is to show you how to actually use it within c++. So let's get rid of this second. And the first one we're gonna look at is the AND operator which uses the ampersand.

Next one is the OR operator, which is the vertical line, then our vertical lines code. But the next one, we're going to use the exclusive OR operator, which uses this upside down triangle or something called a carrot. Our thing and we're going to use the tilde I believe is called a tilde, which is the NOT operator is basically a unary compliment. It's a bit inversion and is tilde going to use the chef The bits to the left to the left, like the less than less than operator, sort of what we're using to try and see him, but it actually usage in here in this instance will be different from when we look at a ship it's to the right. And as you guessed it's to the right right. So let's just do a C out STD column and line and in here we are simply going to put five and free data in brackets now run this bad boy and we get a result of one and generally on the outside if not aware of binary you might think of it with five and free wine Michael a somehow because maybe I didn't cover everything.

And that's just quitted, show you what it's all about. Like I said, generally if you're doing this, I recommend you look up into computer science and then a bit about binary. But here's a quick overview. So we're just going to be using four binary digits for the purpose of this little simple example. So 0000 is zero. And the first digit we're starting to write represents the value one, then the next one represents the value two, therefore represents the value for you can get an excellent representative value eight, and so it says 1248 1632 64 and 48, so forth, and to build a number below five.

What you do is you look at it like this and from the left, you look okay, the first one represents eight because eight go into the value five knows that you put zero, does four go into the value five. Yes, there you go. to number one if you will want to zero on Raw source or like Boolean, so what is remaining is just one does to go into our no says zero. does one go into one? Yes, it does. And now we've made our num number five in binary.

Let's do the same for free. So the eight go into one. No, we're not done for going to a no it not the to load one it does indeed, what we have left is what 2.21 does, indeed. So that's just sort of separate this. What we're going to do is the result of the SOP for the AND operator, it goes through all of these and compares each binary bit with each other. So what it does is list we can distinct from the left, zero and 04.

And both of them need to be one for the result to be one 10001 and zero only one of them don't. So it's 001. Only one of them one, which is 01 and one is one. So let's just go over this again. Eight times zero is 04 times zero is 02 times zero is zero was this, we got for the one section one time long, because this bit it's done. So the result of this is one, you can easily apply it if you have a large number or a different number.

That's a rough General, just the an operator will comment this as and get rid of this. And that is to show you the OR operator lock set. If you want more information, you can message us and we will help as much as possible, but did a quick Google search should suffice as well. Let's just do the OR operator. Comment this ad for a moment, run it when we got seven. So let's just show you how this worked.

This is similar to be quite a long tutorial for quite a short one but one of the things is, let's just center this up. And for the OR operator, it looks for you and checks are only one of them needs to be on at the bare minimum. So zero and zero, none of none of me the answer is 01 and 01 is on Okay, the top one, the 1011 and then we'll put the other 1111 of them is on put the other one. So we got zero there, we got four plus two plus one, which equals seven Hope you're sort of seeing the patterns in how it works now as the exclusive OR rid of this second. So let's get the upside down triangle charactering you run this, we got six an exclusive OR basically say for the result to Be on, only one of them can actually be on.

So we'll show you by using this example. So the first one is 00. And nothing is on. So it's 01 of the results. So it's one because at least on amin, only one of them can be on. There are one is one was one on one is zero because it's Exclusive OR only one of them can be on at any given time.

So it's zero plus four plus two plus zero, so four plus two, which equals six. So let's just copy and paste lists. That's true, NOT operator, please did not even there it is. The total should say so this is minus six. This is somebody gonna look into a little more basically, this is two's complement is make a little note in here. You can check it out and not close.

Not Check out more info on that complement, complement. And you will understand what this is going on about but this just basically inverts the bit so ones become zeros and zeros become ones. And obviously because using the sign bit at the end is a negative because we're using too comfortable again and a lot of that might just mean who the bully to you how you're going to just sit there and you might have just understood the same Amanda Kay no failover Let's hope so it's something that you're gonna want to research because I'm going to showing you the feature when people first are late to these operators. She wants a modulation is best if you do believe or just messages. So let's just show you the last two quickly. The big boys, I mean this shift bits, put it in brackets, we're going to do Five, no six, shift that a name specify how many times you want to shift it to the left, and this is pointing to the left of a note or shifting it to the left though, we are shifting the value six.

I'm going to shift this bit, two points to the left. So let's run this little valley 24. Let's show you how this got to the value of 24. Get rid of that is shift left, shift bits to left, SPT shifted to left and six I believe is 00110. Don't put an excellent zero at the end, simply because that's what we're going to be dealing with the What this does, and it goes through them and basically shifts each bit, one to the left. So, this remains a zero because this one goes there.

The one goes to where this one is a one, then that one goes to where the other one was a one, zero goes to there, and anything that pops in the void, you consume that zero as well. So this is called is how we this produces 24. So it's one to four. Pay for Oh, sorry, my bad, meant to be shifting it to places that won't place Okay, no, no, we can leave that. I was gonna say, first shift. And then we can put second shift VM is shifted in two places.

So we have 124. The last two are eight and 1616 plus eight is 24. Hence what is printed out 24. So let's just show you the final which is shifting into the right, I was going to show you shift in one place and run it should be like free. So let's just copy and paste this shift bit, right. Alright, let's get rid of this.

Get rid of the second guy also get rid of this. I worried that it shifts all the bits to the right to now instead what's best for the members we have one so we go 0011 and we know that if the value one then the value of cherries are two plus one equals free, so hence white it prints out free. That's it for this tutorial. This was quite a long tutorial compared to the other tutorials in this series. If you have any questions feel free to message us at support at Sumner systems coda UK. The email will be in the description you can comment on the videos directly message us on YouTube.

Wherever you feel comfortable with all the requirements or source code will also be in the description. In the next tutorial we'll be looking at the size or size of operator. And as usual, thanks for watching and I hope you have a nice day.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.