Podcast Lookup

Java Operators Explained | Arithmetic, Unary, Logical, Bitwise & Assignment Operators

The Java Developers Hub

  • December 3, 2026
  • 2 h 8 min
  • Episode 15
Episode page
About this episodeOperators are fundamental elements of Java programming that allow developers to perform calculations, compare values, make logical…
Operators are fundamental elements of Java programming that allow developers to perform calculations, compare values, make logical decisions, and manipulate data.In this episode, we explore the major categories of Java operators, including arithmetic, unary, logical, bitwise, and assignment operators. You'll learn how each operator works, when to use it, and how operators influence the execution of Java programs.Through practical examples, this tutorial will help you understand how to write expressions, perform calculations, compare values, and build more effective program logic.Whether you're a complete beginner or strengthening your Java programming fundamentals, mastering operators is essential for writing efficient Java applications.What You'll Learn
  • Understanding operators and operands in Java.

  • Introduction to arithmetic operators (+, -, *, /, %).

  • Performing mathematical calculations using Java.

  • Understanding unary operators (++, --, +, -, !).

  • Working with logical operators (&&, ||, !).

  • Understanding bitwise operators (&, |, ^, ~).

  • Using bitwise shift operators (<<, >>, >>>).

  • Understanding assignment operators (=, +=, -=, *=, /=).

  • Comparing values using relational operators.

  • Understanding operator precedence and associativity.

  • Combining multiple operators in Java expressions.

  • Practical examples of Java operators.

  • Common operator mistakes and best practices.

Who Should Watch?
  • Beginners learning Java programming.

  • Computer science and software engineering students.

  • Developers transitioning to Java.

  • Students preparing for programming examinations.

  • Anyone interested in improving programming logic and problem-solving skills.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Operators Explained | Arithmetic, Unary, Logical, Bitwise & Assignment Operators

00:00—
  1. Speaker 1

    Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to read a new topic, Operators in Java. Before starting the topic, let me tell you again. Since we are working hard to make this playlist, I hope you also watch this playlist well. Read a video properly every day, understand it, make its notes. I will share my notes with you. But apart from that, I want you to make your own notes and share it on social media, on LinkedIn, on Twitter. Share it wherever you feel like. So that you keep making progress. Second, you can promise yourself that you want to watch videos every day and share them. You have to keep updating your progress on LinkedIn and Twitter like a diary.

  2. Speaker 2

    Okay? I have told you so many times. You get views. Second, you get inner motivation.

  3. Speaker 1

    If you want to know your work, He will directly visit your LinkedIn and see how much you have understood, how many things you know.

  4. Speaker 2

    Okay.

  5. Speaker 1

    Rest, like this video if you like the video. If you are enjoying to understand in-depth about the topic in this way, then it was good to leave a comment that it was fun to read this.

  6. Speaker 2

    Or if you have any query, you can ask that too. Okay.

  7. Speaker 1

    So let's start today's topic. So come to the screen. So look, it's a very simple topic.

  8. Speaker 2

    What are operators? Right.

  9. Speaker 1

    Now whenever you will read any programming language, right? If you go one step ahead from a variable, then you will have to use operators. Like in mathematics, you use operators to do plus, minus, multiply, divide. So, you use it in any programming language. So, today we will first study the simplest type of operators, which are your arithmetic operators.

  10. Speaker 2

    Okay? Now, which are the arithmetic operators? This is not a special operator.

  11. Speaker 1

    This is what you must have studied in your Mathematics or if you come to any other programming language, then you must have studied it.

  12. Speaker 2

    Okay. What are the things that come in this? Very simple.

  13. Speaker 1

    Plus, minus, your multiply, your divide. These four are basic. Apart from this, we have the modulus operator. In the modulus operator, we had understood that, in fact, we have used.

  14. Speaker 2

    It as well.

  15. Speaker 1

    What does the modulus operator do? It divides like a divide. But the remainder that is left is your answer. We will use it now. So, these are our main arithmetic operators. Plus, minus, multiply, divide. And your modulus operator. Now, in Java, in fact, in many programming languages, You can also add some operators.

  16. Speaker 2

    We will see that too.

  17. Speaker 1

    For example, I will write plus equal to, minus equal to, multiply equal to and divide equal to and in this way modulo equal to.

  18. Speaker 2

    You can do this in Java. You can add some two operators together. Now we will see how we can do this.

  19. Speaker 1

    And after this, there are two more types of our arithmetic operators. Which we call increment + + and decrement + + operator. Increment and decrement operator.

  20. Speaker 2

    Okay. We will see this too.

  21. Speaker 1

    If you come from CC + +, then you will know what all the operators are. Because we use it the same way. In any other language also. And if you are studying for the first time, then also you will understand. Rest, we will do it together. You will have more fun. I have told you that we have these types of arithmetic operators. Now, in order to explain one by one how the operators work, it won't be fun. So, let's go straight to the code and see its use. So, I have made a demo.java raw file and we have written our boilerplate code. Which we are not understanding now because we will understand it later. What are we learning today? We are learning operators in Java. So, again we are talking about Arithmetic operators.

  22. Speaker 2

    Okay.

  23. Speaker 1

    So arithmetic operators. Now, which are the arithmetic operators we saw? We saw plus, minus, multiply, divide. Apart from this, we saw modulo operator. Apart from that, what did we talk about? We talked about some joint operators. Plus equal to, minus equal to, divide equal to, modulo equal to.

  24. Speaker 2

    Right?

  25. Speaker 1

    And apart from this, we talked about increment and decrement operators. So, let's use all of them one by one. So, let's suppose, what did I do? I took an int variable a, which is equal to 5.

  26. Speaker 2

    Right? Similarly, I took an int variable b, which is equal to, let's say, 10. Okay. So, now what I want is, suppose I want.

  27. Speaker 1

    To perform some arithmetic operation on it. And suppose I want to store its result in a third variable c. So I can say A plus B is simple. So you know the answer to this. 10 plus 5 is 15. And this 15 will be stored in our memory. Similarly, if I write something in D. Something like A minus B. So you know what will happen. 5 minus 10 means minus 5 should.

  28. Speaker 2

    Be stored in memory.

  29. Speaker 1

    And you know how to store negative numbers. If I write int E equal to A into B. You know that 5 into 10 will be 50. And 50 will be stored in your memory. And by doing this, I can use other operations.

  30. Speaker 2

    Let's say I do B divided by A.

  31. Speaker 1

    You know that 10 divided by 5 will be 2. And sorry, I will write this by commenting. you will get 2 and it will be printed in the memory. So, you understand all this very well. Let's say, I will also write another int g which is let's say b modulo a. If I divide b by a, then suppose I have divided 10 by 5, then what will be my remainder?

  32. Speaker 2

    It will be 0, so 0 should be printed here.

  33. Speaker 1

    So, here I have shown you all the basic uses. This is simple. If I print it also, then how do we print it? We write system.out.println. I will explain you the meaning of this command later. Now you know that you just have to write this. So let's say, first I write C, sorry, let's write C inside, plus comma, so once I write comma separately, so see what I did here, C, D, E, F, G, I printed comma separately, and if I run this program, So, I will get the same output. 15, minus 5, 52, and 0. Which we were expecting.

  34. Speaker 2

    Very good.

  35. Speaker 1

    So, I understood all this. That I have used plus, minus, multiply, divide, and modulo. They have a simple use. You have been studying plus and minus all your life. So, you will do this. Now let's look at some new types of variables. So let's comment out all of them. Let's start from the beginning. Let's say we have used the a, b, c, d, e, f, g variables. Let's start with the h. Let's say I said the variable is h.

  36. Speaker 2

    What do I want to store in this h? Let's say I want to store 5 plus 2. I can write a.

  37. Speaker 1

    Plus 2 5 plus 2 means a plus 2 a has the value of 5 right? so if I write a plus 2 here and if I print h with this command then what should be your answer?

  38. Speaker 2

    Answer should be 7 right? so if we look at it quickly then assume I have written system.out.println h l h right? so first this line will be printed.

  39. Speaker 1

    And in the next line my h means 7 should come so let's do this too yes 7 right? ok this was very easy understood right? Now, assume that my h is already 7. If I want to increase h by 2, then I can write h equal to H plus 2, now see this is unique, right?

  40. Speaker 2

    This is interesting.

  41. Speaker 1

    You can do this in programming language, right? You can never do this in mathematics, right? Because in mathematics, assignment means that you are comparing left side with right side, right? But in computer science, when you write 1 equal to, it doesn't mean that you are comparing it. It always means that the value of right side, H plus 2, You are trying to assign it to the left side.

  42. Speaker 2

    Simple.

  43. Speaker 1

    Now we will see the equal to operator. This is also a proper operator. This is called assignment operator. This means that you are assigning the value of the right side to the left side. This does not mean equal to. You are comparing it according to mathematics. For that, we have a special operator. It is double equal to. Which we will see later. But for now, you can assume that this can be written in computer science. So the value of h was a plus 2 and a was 5, so the value of h is 7. Now I increased the value of h by 2. So now the value of h is 9. Now 9 should be printed here. In fact, whenever I use h in the future, it will always be 9. So I can write this. So now because I can write this, Since this is okay in computer science. So if I can write like this, then any programming language, in fact C + +, Java and many others have thought that why not I give it a shortcut keyword, a shortcut operator. So if I have to write h equal to h plus 2, then I directly write h plus equal to 2. Which operator did I use here? Plus equal to. So whenever you see a plus equal to, understand the meaning of this. This means that you want to write h plus equal to 2.

  44. Speaker 2

    H equal to h plus 2.

  45. Speaker 1

    So whatever operator is written here, you are adding that to 2.

  46. Speaker 2

    This is the meaning of this line. So if you see, if I.

  47. Speaker 1

    Comment this line, my 9 should come again.

  48. Speaker 2

    Same.

  49. Speaker 1

    So, h plus equal to works like this. If you understand h plus equal to, then you will also understand minus equal to. What will minus equal to do? Let's say, here the value of h is 9. h is equal to 9.

  50. Speaker 2

    Till here. So, let's say, now what do I do? h minus equal to 2.

  51. Speaker 1

    So, h should be 7 again. Now, you know the meaning of this. h minus equal to 2 means h equal to h minus 2. What do you sign on h? Reduce H to 2 and assign it in H itself. Now its value should be 7 again. That means when I want to print it, I should get 7. Which will come. Absolutely correct. In the same way, you can use into equal to Where is it?

  52. Speaker 2

    I haven't written it yet.

  53. Speaker 1

    So multiply equal to and divide equal to and your modulo equal to. You can use these three operators in the same way as we have written. In that case, what will you do? You will write h into equal to let's say 3.

  54. Speaker 2

    What does this mean?

  55. Speaker 1

    You took original h and multiplied 3 in h. You can write h divide equal to let's say 5.

  56. Speaker 2

    What does this mean? You took h and.

  57. Speaker 1

    Assigned as h divided by 5 And you can write h modulo equal to Let's say again 5 So this also means that you took h and made it modulo from 5 So you can perform all these operations This is exactly the same This plus equal to, minus equal to, multiply equal to is exactly the same So you can write it like this or like that This saves a little space and is a little fast So generally we prefer this in programming If you look at any code, if you pick any real world project, then if you want to add anything in it, then generally we prefer plus equal to minus equal to. If you want to add the same number, if you want to add H in H itself, then right. Okay, very good. It was simple, we understood all this. How far did we understand? We understood this far. There are two more things left, your increment and decrement operator. Okay, what is this? It is very simple. Look, again I will take a new variable. Let's make it a little higher. Let's take g. Suppose I take a variable g equal to 6.

  58. Speaker 2

    Simple. I have to take a new variable. Let's take i. Int i equal to 6. Very good.

  59. Speaker 1

    Now we have to see increment and decrement operator. Increment operator says that I write plus plus in front of simple i. What does this mean? This means nothing.

  60. Speaker 2

    I took i. And assign it with i plus 1. That's it.

  61. Speaker 1

    If I assign i plus 1 with i again, it means i plus plus. So, this is the short version of this.

  62. Speaker 2

    Right?

  63. Speaker 1

    You could have written another version of this above as well. You could have written this as well. I plus equal to 1, right? We saw this above. If we give a little gap here, which is optional, you can also not give a gap. But it looks a little nice. So if I write H plus equal to, I can also write I plus equal to 1.

  64. Speaker 2

    Right?

  65. Speaker 1

    So this is its short form and I plus plus is also its short form.

  66. Speaker 2

    Right?

  67. Speaker 1

    So if you just want to increment from 1, which you generally have to do in programming, like if we read loops ahead, So you have to increment from 1 many times, not always. So there will be many such scenarios when you will increment an integer value from 1. So in that case, this I + + operator is very useful. So now if I try to print this again. So first the value of I is 60, here I print I. And now I have added i to the final value i.e. i now is equal to 7. This value will be final. I will write it here.

  68. Speaker 2

    What should come here? 7. So let's see if 7 is really being printed.

  69. Speaker 1

    Yes, it is. i is equal to 7. What is 4 printed on it? H is printed on it. Now we have done a lot of operations in H. Plus, minus, multiply, divide, modulo. So you can see that 4 should come after doing everything. That's why 4 is being printed.

  70. Speaker 2

    What is happening below? 7 is being printed below. Very good.

  71. Speaker 1

    If you have understood increment operator, then decrement is also the opposite of it. You just write i minus minus. Now you can tell that what does it mean? i equal to i minus 1. We can write it in another way which is i minus equal to 1. Okay, as we have written above. We did 6 plus 7 and then did minus again.

  72. Speaker 2

    So, we will get 6 again. If we print, then our 6 will come. Very good.

  73. Speaker 1

    So now you have understood all the operators. Plus, minus, multiply, divide, modulo. And these are your assignments. And increment and decrement operators too.

  74. Speaker 2

    Now. Is the story of arithmetic operators over? Yes, it is over. There is only one thing left. Let's understand that. That is. Pre-increment.

  75. Speaker 1

    And post-increment. Which is also called pre-fix increment and post-fix increment. And same applies for decrement too. What is this rule?

  76. Speaker 2

    Decrement. Okay. Look. Again, let's take a variable, where was it? It was till i. So, let's take j.

  77. Speaker 1

    We have taken a variable j equal to let's say 7.

  78. Speaker 2

    This time, let's take 7.

  79. Speaker 1

    If I want to increment it, then I can write j + + which I told you earlier.

  80. Speaker 2

    Let's add it together and write j + +. This is absolutely valid. And I can write this many times. I can write j + + in the next line. Again, one will be added in j.

  81. Speaker 1

    Then, I will write j + + in the next line. Again, it will be added in j. So, first it will be 8, then 9, then 10. There is one more way to write plus plus first and then j later. And all these rules are applicable for minus minus as well. I write like this, minus minus j and j minus minus. I can do all these things. So, I can do what I can do for plus plus for minus minus as well. Yes, remember one thing. Intu intu and divide divide.

  82. Speaker 2

    This is not an operator.

  83. Speaker 1

    So, this is not a valid operator. So, we don't use it. That's why it is increment and decrement operator. Because these are the most used operators. We always have to increment or decrement from 1. Because in computer science, if we read arrays or loops, you always go to the next memory. Multiplication, first of all, it doesn't matter what you multiply from 1.

  84. Speaker 2

    It will be the same, the output will always be the same. You will multiply from 2, right? Bare minimum. And you can't keep multiplying from 2 every time. It can change. Same divide can change. But plus or minus.

  85. Speaker 1

    Many times in computer science, you increment or decrement from 1. So to optimize it, we have created new operators for it.

  86. Speaker 2

    Simple. Now I was telling.

  87. Speaker 1

    You that you can write j + + or you can write plus plus j too. So I write twice, once I write j + + and once I write plus plus j. Now this is also an increment, the one above is also an increment. We call this prefix increment. And we call this postfix. Because what does prefix mean? First you incremented it. Means first you wrote plus plus and then j. And postfix means first you wrote the name of the variable and then wrote plus plus. This is the difference. Both do nothing. Both do increment only. As it was 7, so j plus plus must have made it 8. And then plus plus j must have made it 9. If I print this here. If I print this here. What do I have to print? j. Sorry, not i divided by j, j. So what should I print? My 9 should be printed. Let's see. Absolutely right, 9 is being printed here. So, what is the difference between the two? Why are there two operators when both are working on the same thing? There is a very small difference and that is also a very useful difference.

  88. Speaker 2

    I will show you that difference with a new variable. So, I took j till j.

  89. Speaker 1

    So, let's do one thing. a, b, c, d, e, f, g, h, i, j, k. Now, let's take k. So, int k equal to, let's say I write j + +.

  90. Speaker 2

    What do you think? What will be the answer?

  91. Speaker 1

    Okay? We have to guess the answer of this. And in the next line, let's make another L operator. And in this, I write, plus plus j. What will be the answer? We have to guess this. So, let's understand the meaning of both. Maybe you think that the meaning of both will be the same, but no. First of all, what is j? j was 7, here it was 8, here it was 9. So, we can assume that the value of j is 9. Okay, j is 9. Now, here we said k is equal to j + +. So, first of all, I know that j will be 10 from j + +. I can prove this too. Let's do one thing. Here, I will print j. I am not talking about k and l. I am just talking about j. Now, I know that j was 7.

  92. Speaker 2

    Then 8, then 9. Here, j is equal to 9.

  93. Speaker 1

    I'll bring it down Now I have to guess what will be the value of k But first I'll print j Which I think by doing plus plus here j should be 10 So in the last I should see 10 It's showing 10, meaning j is increasing correctly Then what did I do? L equal to plus plus j I'll print j again So that there is no doubt that what will be the value of j J should increase again So now j should be 11 Let's remove it from here So in the last I should see 11, the value of j 11 is correct. So, j is increasing well, which we had thought. Whether write j + + or write + + j, j keeps increasing. What will be its side effect on k? Look, let's understand this. Whenever I write k equal to j + +, then it means that I am.

  94. Speaker 2

    Saying, first, before the value of j is plus plus, do k cosine. And then increase j from 1. What does it mean?

  95. Speaker 1

    It means that if I break this in 2 lines, k equal to j plus plus, then it will look something like this. First I write k equal to j, this is 1 line. And then I write j equal to j plus 1.

  96. Speaker 2

    Something like this.

  97. Speaker 1

    First assign k to j and then increment j because we know that j is being incremented but in k, do you know the value of j? the old one, which means 9 in j, before being plus plus, there was 9 and above it was 9 so the value of k will be 9, not 10 but j will be 10 right? so what was the proof? the proof was that I printed j and also printed k Basically, what we do is, we separate both commas again. And we printed K as well.

  98. Speaker 2

    Okay. So, look. I told you, here will be a plus. Okay. than 1.

  99. Speaker 1

    So, we will get 10 commas and k will be 9. So, let's see if this is happening. 10 comma 9, absolutely.

  100. Speaker 2

    Okay?

  101. Speaker 1

    Now, this becomes completely upside down.

  102. Speaker 2

    Okay?

  103. Speaker 1

    Which we call prefix. So, what does prefix mean? Prefix means, first increment j, and then assign that big value, l. Meaning, l is equal to j plus 1. This means, this is simple. Not that you are incrementing first, sorry, you are assigning first, then incrementing.

  104. Speaker 2

    No.

  105. Speaker 1

    Increment it first and then assign L. This time I will print J and L. So what will happen? First J became 10 and then 11 again.

  106. Speaker 2

    So J is 11.

  107. Speaker 1

    But L will also become 11 instead of 9. Because we got the new value of J. So 11 11 should be printed.

  108. Speaker 2

    Which is happening.

  109. Speaker 1

    11 11. So, these are all your arithmetic operators. I hope you understood.

  110. Speaker 2

    It's very easy.

  111. Speaker 1

    You just need to see this small difference in increment and decrement.

  112. Speaker 2

    What is your pre- and post-fix?

  113. Speaker 1

    Now, what we said for plus plus, you can say the same thing for minus minus.

  114. Speaker 2

    Just say the opposite of that.

  115. Speaker 1

    If I write j minus minus, then j would be assigned first and then j would be decrement. And here, j would be decrement first and then assigned. These are very useful things. Sometimes when you write J + + in a loop, it matters a lot. We will see that in the future.

  116. Speaker 2

    So you have to understand this. It's a small thing.

  117. Speaker 1

    If you understood this, then it's great. Now let's move on to our next operators. So here we understood all the arithmetic operators. They were very simple. Now if we.

  118. Speaker 2

    Move on to our new operators, then we call the rest of the new operators as relational operators. Relational operators. Okay, what are relational operators? Let's see this once.

  119. Speaker 1

    Okay, this is also very simple. First, let me tell you which operators come in this. In this, equal equal to. You will say two equal. Yes, this is the same equal which we were talking about in mathematics. That you are comparing left and right value. Okay, let's see about this too. Not equal to. The exclamation mark means not. We will see what it is. Apart from this, your simple less than, greater than. And we can add this too. Less than equal to and greater than equal to. So, these are all our relational operators.

  120. Speaker 2

    Okay, I understood. But where do these relational operators come in use?

  121. Speaker 1

    Look, these are going to be very useful to you. We haven't read statements like if etc. But these are going to be very useful in if and many other conditions. So suppose we want to see the use of equal to.

  122. Speaker 2

    Okay?

  123. Speaker 1

    So let's see it from the starting. Suppose I declared a variable a which I gave 5. And I declared another variable b which I gave 10.

  124. Speaker 2

    Okay? If I write.

  125. Speaker 1

    A equal equal B. What does this line mean? If I am writing this in my code, then it means that I am asking from the compiler. What am I asking? I am asking, is A is equal to B? I am asking.

  126. Speaker 2

    This question from the compiler.

  127. Speaker 1

    Is it so that A is equal to B? If it is so, then the compiler will say true. If it is not, then the compiler will say false.

  128. Speaker 2

    Simple. But if I write.

  129. Speaker 1

    A equal to b instead of a, then what would that mean? That would mean, whatever we read above, I am saying to the compiler.

  130. Speaker 2

    Assign.

  131. Speaker 1

    Value of b to a. Which means, whatever is in B, give it to A. I am not saying that A is equal to B.

  132. Speaker 2

    I am saying that whatever is in B. What is there in B? 10.

  133. Speaker 1

    Give that to A. So if I go down and type system.out.println A, then you know what will be printed? My print will be 10.

  134. Speaker 2

    Simple.

  135. Speaker 1

    This means that the value in B has come to A. This is an assignment operator. So what do we call this? We call this assignment. And this is called relational. Here I am taking out the relation of both. I have put a little more L. Relational.

  136. Speaker 2

    That's it. Okay.

  137. Speaker 1

    You said that I am asking its answer from the computer or compiler.

  138. Speaker 2

    Is A is equal to B? This one, right?

  139. Speaker 1

    So, either I will get the answer as true or false, but I will get it as Boolean result. We have read that we also have Boolean data type.

  140. Speaker 2

    Boolean, right? Which is either true or false. So, we can get the answer as that too. Okay?

  141. Speaker 1

    So, we can see that too. How can we see that? We will do one thing. We will declare a Boolean variable.

  142. Speaker 2

    Boolean, let's say, c. Okay?

  143. Speaker 1

    And what will I put in that boolean? I will put a equal equal b. So whatever result it gets, I will put it in c. And what will I do with that result? I will print that result and see. I will print c here. I should understand the answer, right? So we will do this and see. Okay, our second assignment operator is not equal to. Now this is exactly the opposite of this. If I write a not equal to b. So, I am asking the opposite question from the computer. What am I asking from the computer? Is A not equal to B? Is it so? And again, I will either get the answer as true or false. The question I asked here, sorry, I asked here, where did it go? The question I asked here, I am asking the opposite of that question here.

  144. Speaker 2

    Simple?

  145. Speaker 1

    So we will also get the answer in the Boolean form, which I can represent in this way.

  146. Speaker 2

    Okay?

  147. Speaker 1

    So what we will do is, we will write this statement in our code, and we can write the opposite of this one as well.

  148. Speaker 2

    That C is equal to A not equal to B. Not equal to B. Okay?

  149. Speaker 1

    So we will also get some Boolean answer for this.

  150. Speaker 2

    Right? So C is also Boolean here and here is also Boolean. Okay, very good.

  151. Speaker 1

    So, we understood which operators are equal to and not equal to. Now, less than and greater than are the same, which you must have used in mathematics. So, if I ask, again, let's say, I said, int c equal to a less than b.

  152. Speaker 2

    So, what am I asking the compiler? In this, I am asking the compiler, is a Less than b.

  153. Speaker 1

    It's a simple question. Again, either I will get true or false. Again, you can see that I can write a greater than b. Either I will get true or false. So, you must have understood till here. I have seen less than and greater than. Now, what are these two? They are exactly the same. Less than equal to, greater than equal to. We have used this too. a less than equal to b. So, am I asking a question? I am asking a question from compiler. Is a less than or equal to b. So I am asking this question. Is it less than or equal to a? Again, what can be the answer? Either true or false. So what I mean to say is, and I can also do the opposite of this, greater than or equal to, you would have also known that. So what I mean to say is, that all these operators, These all, equal, equal, not equal, less than, greater than, less than equal and greater than equal. These all finally give me a Boolean value.

  154. Speaker 2

    What do they give me? A Boolean value. Okay.

  155. Speaker 1

    And here, what I was writing, these all, which I was assigning to C, or these all, which I was assigning to this, these all, in the language of computer science, we.

  156. Speaker 2

    Call them expressions. Okay. Now, you might have understood. This link might be linked with math. What is expression?

  157. Speaker 1

    It's a simple thing. I am saying that I am performing something. I called it expression. And some result of that expression will come. Which I am assigning here on the left side. I am assigning C.

  158. Speaker 2

    Okay. So, I called it expressions.

  159. Speaker 1

    So, here I just said that all our relational operators.

  160. Speaker 2

    Especially these. These all give me results in Boolean values. The result of all these expressions is Boolean.

  161. Speaker 1

    I hope you have understood what relational operators are and what output they give. So I have told you that they give a Boolean output. So let's quickly see this in the code. So this is the same code that I was running. All the arithmetic operators I had shown you here. Now if we keep on doing this in this class, it will keep on mixing up. So let's do one thing, let's make a new file. So like demo.java was made, let me make a file named demo2.

  162. Speaker 2

    Java. This is done. I am writing it by.

  163. Speaker 1

    Myself because VS Code auto completes it. In fact, the way to auto complete Public Static Void Main is to press PSVM and Tab in VS Code.

  164. Speaker 2

    This becomes a boilerplate.

  165. Speaker 1

    If you are using VS Code, you can do this.

  166. Speaker 2

    What are we going to see?

  167. Speaker 1

    Relational Operators. Let's see Relational Operators. What are these? These are equal equal to.

  168. Speaker 2

    Okay. Apart from equal equal to, what did we say? Not equal to. Okay. Less than. Greater than.

  169. Speaker 1

    And what are these? Less than equal to. Greater than equal to.

  170. Speaker 2

    Etc. Etc. These are all. So, let's see their use. So, what did I say? I'll do a simple work. I'll take an A, 5. And I'll take a B, 10.

  171. Speaker 1

    I have taken two simple variables and I will take a third variable boolean because it will give me boolean value c and I will do a equal equal b see this I can do this right no compiler error has come now I will put a bracket first because it looks a little beautiful and understands what is happening ok what does this mean I am signing the result of this statement in this so now if I write system dot out dot println, I will try to print this as c, then what should I get? I should get false because a is not equal to b, right? So I should get false.

  172. Speaker 2

    False, right? Absolutely correct.

  173. Speaker 1

    If a was also 10, let's say a was also 10, in that case, we should get true.

  174. Speaker 2

    True.

  175. Speaker 1

    So I hope you understood. If you understood A, then the rest will come. I will just show by doing not. Let's say I said boolean, this time D equal to A not equal to B. So obviously whatever will come up, its opposite will be printed here. So let's say I print D here. So D should be true. Because C is false, D should be true, that A is not equal to B.

  176. Speaker 2

    Is A not equal to B? Absolutely not.

  177. Speaker 1

    So its answer is true. That I have said the right statement. So true. See, first false, second true.

  178. Speaker 2

    Absolutely right.

  179. Speaker 1

    So now you understood equal, equal and not equal. Now you will understand less than and greater than also. So you don't need to do that too. If I just write that A, sorry, is A less than B? So what is its answer?

  180. Speaker 2

    Is A small? Yes, it is. Is it true? Okay. If I make F like this, is A greater than? Yes, it is. The answer will be false. Less than or equal to? What will it do? Is A less than or equal to? Okay.

  181. Speaker 1

    So, if I write less than or equal to here, it means less than or equal to. One of the two conditions should be true.

  182. Speaker 2

    Okay.

  183. Speaker 1

    If A's value was 10, then also this condition would be true. But then this condition would not be true.

  184. Speaker 2

    Right?

  185. Speaker 1

    A less than is also strict. So, if we look at this, let's take Boolean F, that would be a less than b. And in this case, sorry, it will come out as b. In this case, I will make both as 10. So, let's make it less than or equal to. Now, see, here it will be true. a less than b will be false. Not equal to b will be false. And it will be equal to true. So it should be printed like this.

  186. Speaker 2

    True, false, false, true. Okay. Why? Because a is less than b. No. a and b are equal. So it is not less than. But is a equal to b? Yes. So this is true. This is true. a is not equal to b. Not at all. False.

  187. Speaker 1

    Now look at the last one. It says a less than equal to b. Either it should be less than or equal. One of the two is true.

  188. Speaker 2

    So it will be true. So our true, false, false, true. It should be printed like this. Yes. Look.

  189. Speaker 1

    Okay, it's true and false because we didn't print them. So, let's print them first. So, let's do one thing. Let's write two print commands. After one, don't separate them by commas. I'm writing them in the next line.

  190. Speaker 2

    Okay. And one is E and one is F. E and F. Okay. So true, false, false, true. Just like I was expecting. True, false, false, true. Absolutely correct. Okay.

  191. Speaker 1

    So you understood all these relational operators. That they always give us boolean variables.

  192. Speaker 2

    Okay.

  193. Speaker 1

    Where do we use this? When we will read the conditionals, you will see its use a lot.

  194. Speaker 2

    Okay.

  195. Speaker 1

    Till then you can understand it like this. Now let's jump to our next operators.

  196. Speaker 2

    Okay.

  197. Speaker 1

    Now see, our next operators, which we call bitwise operators, that.

  198. Speaker 2

    Is more interesting. Okay. It will be more fun to read. Why?

  199. Speaker 1

    Because they help us for bit manipulations. Bit manipulation. Basically, it means that they work at the bit level. Let's see how they work. First of all, let's see the operators. For bit manipulation, we have bitwise operators. And, bitwise and, bitwise or, bitwise exclusive or, bitwise not. Now you must be thinking how are there so many operators? These are the same in every language. This is your simple AND. Bitwise AND, bitwise OR, bitwise EXCLUSIVE OR. If you have heard all this for the first time, it will be easy for you to understand.

  200. Speaker 2

    This is the NOT operator. Apart from this, what else do we have?

  201. Speaker 1

    Apart from this, we have SHIFT RIGHT, SHIFT LEFT. What else do we have? Apart from this, we have SHIFT RIGHT WITH ZERO FILL. Everyone will understand, it's very easy, no problem. Now obviously we can add two operators like and equal to, or equal to, exclusive or equal to, bitwise equal to, sorry shift right equal to, shift left equal to, shift right with zero fill equal to.

  202. Speaker 2

    Right?

  203. Speaker 1

    So what we did is we added equal to to all the ones we used till now.

  204. Speaker 2

    Okay?

  205. Speaker 1

    What else can we add? That's it. So these are all our bit manipulation operators. It might look like a lot, but when you use it, all your mind will be at ease because this is all repeating. From here to here, we have repeated them. Why did we repeat them? Because we have joined two operators. Just like we were doing plus equal to, we have also done and equal to. It's simple. Now, what are bitwise operators? I said, let's play with bits. How do we play?

  206. Speaker 2

    Look, let's understand this carefully. Let's say I have two numbers, A And B. Simple. Now I have these two numbers A and B. Let's say I have A. What do we take? Let's take 2. And I have B. 3. So if I write in a result int C equal to A and B. So what will be its answer? First of all, what type of answer will it be? It won't be boolean.

  207. Speaker 1

    Because it is not a relational operator. Its answer will be integer only. If you are holding two integers and ending it, then the final answer will also be an integer only.

  208. Speaker 2

    But what does it do? What does it end?

  209. Speaker 1

    Whenever we talk about bit operations, bit manipulations, and these operators are used, So these operators are applied at the bits level, not at the decimal level. Now this 2 and this 3, they are 2 and 3 in decimal. But if we convert them into bits, then you know, let's move ahead, I'll show you the calculation. So you know how we call 2 in bits. We call 2 as 1, 0. And how do we call 3 in bits?

  210. Speaker 2

    We call 3 as 1, 1.

  211. Speaker 1

    Because this is your first place, this is your second place. But let's assume we are representing in 8 bits, so the integer is 32 bits and if we take byte it would be better. So let's assume we have byte instead of int because byte is of 8 bits.

  212. Speaker 2

    Byte B. Okay? To explain, 8 bits are enough. So I'll take 8 spaces. 1, 2, 3, 4, 5, 6, 7, 8. Okay? Now here, this is 1st place, 2nd, 4th, 8th, 16th, 32th, 64th, and your 128th place. Right?

  213. Speaker 1

    Now here, how will I represent A, which is 2? So I'll make 2 as 1, the rest will be 0.

  214. Speaker 2

    Simple.

  215. Speaker 1

    Now just like this, this is my A. Similarly, how will I represent b, which is 3? So again, all my places will be 0. Only 2 and 1 will be 1.

  216. Speaker 2

    Right?

  217. Speaker 1

    I will represent it like this. Now let's do one thing. Let's remove the middle parts of it. So I understood how we write this.

  218. Speaker 2

    Okay? There is no need to write again and again. So I will remove them. Yes. Yes. So, I wrote this.

  219. Speaker 1

    Now, what am I doing in A and B? I am doing AND in A and B. What is the meaning of AND? It means that I will play something with these bits. Same, if I had not done AND here, I would have done MORE. this operator, then also I would play with these bits, right, basically all these operators that we have written above, they will play with these bits only, okay, they will play with these bits only, now how will they play, there are some rules for that, okay, if you have read some of its rules in discrete mathematics, then it is good, otherwise we will learn here, so for example, what do I do, for example, I am making a table here for you, Let's take two values, A and B. We'll do the AND of A and B once. We'll do A and B once. We'll do A and B once. And yes, we have this operator, not operator. Where did we make it? Here it is, not operator. This is unary operator. What does unary operator mean? It depends on one operand. Like till now we were writing a plus b. So this plus is a binary operator because it needs two operands, a and b. But like a + + was there, unary because it needs only one operand but our not is also a unary operator I simply write not a it needs only one operand let's see what is its value so I write here not a.

  220. Speaker 2

    Okay?

  221. Speaker 1

    Let's see with this. For which values? First, a can be 0 and b can be 0. Once 0, b is 1. a is 1, b is 0. Or both are 1.

  222. Speaker 2

    Okay? Something like this. So what did I do?

  223. Speaker 1

    I tried to predict their values for every possible value. Now let's understand this. Now look, a and b are very simple. Let's understand the rule first. If a is 0, And B is 0. So the answer will be 0. How do I tell you?

  224. Speaker 2

    A is 1. A is 0. And B is 1. Answer will be 0. A is 1. B is 0. Answer will be 0. Both 1. Answer will be 1. Why? Because AND operator says that if....

  225. Speaker 1

    If I want to be 1, then both operators have to be 1. If any one of A and B is 0, then I am 0 for that case. My final value is 0. But if both are 1, then I am 1. This is called AND. In fact, if we use this a little bit, if we understand 1 and 0 in true and false, then you will understand things. A and B say that if both are false, 0 means let's say I say false, And 1 means true. So, A and B says that if both are false or if one of them is false, then my final value will be false. But if both are true, then only I will be true.

  226. Speaker 2

    Okay. Very good.

  227. Speaker 1

    What does A and B say? A and B says the opposite. A and B says that if one of them is 1, then I will be 1.

  228. Speaker 2

    Okay. If one of them is 1. Okay. So, this means that both are 0 here.

  229. Speaker 1

    So, A and B will be 0 because one of them is not 1. What will happen in 0 and 1? 1 because it is 1. What will happen in 1 and 0? 1. And if both are there, then it will be 1.

  230. Speaker 2

    Okay? So, A and B will get the answer. Right? Right? Okay, this is very interesting.

  231. Speaker 1

    A exclusive or B. Or you might have read this in logical gates. But if you haven't read it, exclusive or says that in A and B, if there is odd number of 1, then only I will be 1. Whenever I will get odd number of 1, then I will be 1. Now see, in the first one, what is 0, 0? Are there odd ones?

  232. Speaker 2

    No. There are 0 ones. So 0 is even number.

  233. Speaker 1

    So in that case, this is your 0. So the second line is 0, 1. Is there an odd number of 1s here? Yes, there is one 1. So in that case, we have 1 here. Sorry, we have 1 here. So the next one is 1, 0, 1, 0, again an odd number of 1s. So what will happen in 1, 1? Is there an odd number of 1s here? No, there are two 1s here. So this will again be 0. So this is your final output of exclusive OR. Not gate, I am saying gate again and again because we read logical gates, right? So we use these operators in that too. But we don't realize that when we talk about bits in computer science, then what is happening inside? There is a processor, we saw in the first lecture, now the processor is also working on bits, there are logical gates inside, right? So what is actually working? This is making logical gates, working on 0s and 1s. We just represent it programmatically with this operator. We are doing the same thing. Now what will be not a? Not a will be, which will not be a, which means a will be upside down. So what does it mean?

  234. Speaker 2

    Reverse a.

  235. Speaker 1

    If a is 0, then 1, sorry, let's write it in pencil. If a is 0, then 1, here, sorry, if a is 0, then 1, here also 1. Here if it was 1, then 0. Here if it was again 1, then 0. Something like this. 0.

  236. Speaker 2

    Okay?

  237. Speaker 1

    And if I had written B naught in the same way, then you would have understood that too. Let's write here. Let's say not B, then we would have done B upside down. We would have done 0 for 1, 1 for 0, 0 for 1, 1 for 0. So this would have been done.

  238. Speaker 2

    In this way, the table we make using these 0s and 1s, we call this table truth table. Truth.

  239. Speaker 1

    Again, you must have read discrete mathematics and gates.

  240. Speaker 2

    If you haven't, then it's okay. I'll tell you.

  241. Speaker 1

    When we call this table, we call it truth table.

  242. Speaker 2

    Why?

  243. Speaker 1

    Because it tells the true value of a and b. What will be the true value of a and b? a and b, a and b, sorry, a exclusive and b, not a, not b, etc. Anyway, operators we have are these main main apart from this see what we have covered and or exclusive or not we have covered 4 operators apart from this we have shift operators shift left sorry shift right shift left and shift right with zeros now let's see what happens and after that we will repeat this again using assignment assignment you must have understood so let's say if I write c is equal to C and 2. Assign C to 2 and assign it back to C. So I could have written C and equal to 2. Like we did for plus. So basically what we were doing in plus minus multiply divide, we did the same thing. We can also do it with all these. With AND, OR, EXCLUSIVE OR, and now with the shifts we will see.

  244. Speaker 2

    Okay? So suppose these operators are also covered and these operators are also covered. Now our work remains to see these shift operators. Okay?

  245. Speaker 1

    So let's see the shift operators quickly and then we will move ahead.

  246. Speaker 2

    Okay?

  247. Speaker 1

    Yes, so we start with our left shift operator. How do we denote it? Something like this.

  248. Speaker 2

    Okay? First of all, I have written a number. Let's say byte B is equal to 8. Simple.

  249. Speaker 1

    A simple number is written. Byte B is equal to 8. And I write B is equal to B left shift 1.

  250. Speaker 2

    Simple.

  251. Speaker 1

    I just wrote B left shift 1. Now let's understand how to left shift. But first let's understand the output of this expression. First of all, what is B? B is our 8, right?

  252. Speaker 2

    It is an integer.

  253. Speaker 1

    If I am shifting this B to the left from 1, then the result of this expression will also be an integer, right?

  254. Speaker 2

    It will not be a Boolean.

  255. Speaker 1

    So, we got to know that the result of this expression is an integer and I can represent it in any other integer format.

  256. Speaker 2

    First thing, okay?

  257. Speaker 1

    So, first of all, we represent our 8 in binary using 8 bits, okay?

  258. Speaker 2

    So, first of all, I will say 8 bits. 1, 2, 3, 4, 5, 6, 7, and 8. Okay?

  259. Speaker 1

    8 bits are done. Now, I'll give them numbering. This time, I'll give numbering a little higher. 1, 2, this is our 4. Let's make it a little lower.

  260. Speaker 2

    Okay?

  261. Speaker 1

    This is our 8, this is our 16, this is our 32, this is our 64, this is our 128. I've just done their numbering higher because I want to show you something below. So, let's say, how can I represent 8? I'll make 1 here. I'll make all the rest of the bits 0.

  262. Speaker 2

    Simple.

  263. Speaker 1

    Something like this. And after doing this, our number will become that number. 8. Very good. Now if I have written here, B left shift 1. What does this mean? This means that if I make all the positions one more time, then this means that all the positions, all the bits, all the positions will be left shift. Something like this. This will come here, this will come here, and this will come here. Something like this.

  264. Speaker 2

    Okay. What does it mean? Let me show you first. First of all, this zero will rise and come from here to this position. On its left position. In the same way, this zero will rise and come to this position. In the same way, this zero will rise and come to this position. And this zero will rise and come to this position. This zero will come here. This will come here. And finally, this will come here. And what will happen to this? This will come out of our bit. It will come out of our 8-bit range.

  265. Speaker 1

    And finally, the empty space left here, the least significant bit which is empty, a new 0 will come here.

  266. Speaker 2

    Simple. A new 0 will come.

  267. Speaker 1

    So we introduced a new 0 in our least significant bit. So our final answer will be this. You can ignore this because we are on the 9th bit and our bit range is 8. So our final output will be this. Okay, this was a little wrong. Here this shift was done, not 0, 1. This is 1. The rest will be 0 obviously. But this is our 1 shift.

  268. Speaker 2

    Okay? Because all the bits have shifted. Okay.

  269. Speaker 1

    Now what is the answer to this?

  270. Speaker 2

    This is a binary number.

  271. Speaker 1

    If we take out its decimal number, then see, here is 1.

  272. Speaker 2

    Our overall number is 16.

  273. Speaker 1

    So when we shifted 8 from 1 to 1, our number became 16.

  274. Speaker 2

    Very good.

  275. Speaker 1

    Now what if I write it again? Again I wrote b equal to b left shift 1. Again I want to left shift b.

  276. Speaker 2

    Okay.

  277. Speaker 1

    So let's repeat the same procedure. First let's make 8 bits. 3, 4, 5, 6, 7 and 8. And let's do it in the same way. This bit will come here.

  278. Speaker 2

    This here. This here. This here.

  279. Speaker 1

    This 1 will come here, this 0 here, this 0 here, and this 0 will come out. Then this bit will be empty, and a new 0 will come here. So what will be our final number? Our final number will be this one.

  280. Speaker 2

    This is out of range.

  281. Speaker 1

    If we convert this final number back to decimal, Very good. So what did we do? We shifted 8 to the left from 1 and it became 16. We shifted 16 to the left from 1 and it became 32. So here you must be seeing a pattern that whenever we shift to the left, our number becomes 2. If we do it again, it will become 64. Why is this happening? It's very simple. See what's happening. Wherever there is a 1, like there was a 1 on the 8th bit and the rest of the bits were 0, it moves one position left from its position. And as soon as that one position moves to the left, double position is present in the left. So if the number was 8, double position is present in the left. So if this 1 shifts here, then the overall number will be 16.

  282. Speaker 2

    It's simple.

  283. Speaker 1

    So it happens with every number. If we represent 10, then 8 would also be 1 and 2 would also be 1, then the number would be 10. But if 8 moves towards 16, 2 moves towards 4, then 16 plus 4 becomes 20, right? That means 10 is also doubled. So all the ones, one of their positions is left shift and the original number doubles. Very simple. So you can keep doing this. Now you can make 32 to 64 and you can go ahead. So first we understood this story, okay? What does your left shift do? We understood the basics. All the bits shift to the left. Not one by one, but the same as we have written here. We have shifted B from 1, so one position shifted. If we shifted from 2, two positions would have shifted. So this would have shifted to this position. And the two spaces left, we would have shifted to two new ones. If we shift B from 3, then this 0 would come directly to its 3rd position. And if we take 3 new 0s from these 3 positions, then we get 3 new 0s.

  284. Speaker 2

    Simple.

  285. Speaker 1

    So I hope you got the idea of left shift. Now let's understand a little more about it. One more thing we understood is that left shift is equivalent to multiplying any number by 2. This means B left shift 1 is equivalent.

  286. Speaker 2

    To B into 2. This is what we are talking about.

  287. Speaker 1

    If you shift from 1 to the left, that means you have multiplied it by 2. Similarly, if you shift from 2, then it is equivalent to that you have multiplied B by 2 into 2. 2 to the power of 2, that means you have multiplied it by 4.

  288. Speaker 2

    Isn't it?

  289. Speaker 1

    That is what we were looking at here. So, I hope you have understood this much. But can we keep doing this? What does it mean? It means that if I write again, let's write it in white, B is equal to B left shift 1. So, if I shift it again to the left, What does it mean to do it again? I make 8 positions and left shift these positions again. This comes here, this here, this here, this here, this here, this 1 comes here, this 0 comes here and this 0 comes out. This new 0 comes, I fill my 1 on it. So, our new value has become this. Which one part is this?

  290. Speaker 2

    64. So, here our number has become 64. Very good. Till now, the number has been doubling. 8, 16, 32, 64. Okay.

  291. Speaker 1

    If I try to do this one more time, then I will write, very simple, B one more time.

  292. Speaker 2

    Left shift one time.

  293. Speaker 1

    That means, I will make 8 positions again. 1, 2, 3, 4, 5, 6, 7, and 8. Now let's quickly shift it. By the way, 0 will come here. I don't know why I wrote 1. Now let's shift it from 1 to 1. So this zero came.

  294. Speaker 2

    Here, this came here, this came here, this came here, and this one finally came here and this zero came out. And this spot was empty, here a new zero came. Simple. So now our number became this. And what is this number? Where is 1? At 128. So our number became 128. Right? Right? Absolutely wrong. How?

  295. Speaker 1

    We just read in the last video that our bit is MSB. Most significant bit.

  296. Speaker 2

    Simple thing. And this is your LSB.

  297. Speaker 1

    Least significant bit. Now, since we are talking about a byte, so here 8th bit, i.e. if we see from 0, then 7th bit will be your MSB and 0th bit will be your LSB.

  298. Speaker 2

    Right? From right side to 1st bit. Okay.

  299. Speaker 1

    If your MSB is 1, then it means this number is a negative number. And if it is 0, then it means this number is a positive number. We had read this too. As soon as you shift this 1 here, then this 1 will come on your most significant bit and our compiler will understand it as a negative number. And what does it do when it understands a negative number? It takes out its 2's compliment whenever you try to get its value. We have already studied this. Now you know that if we take out its 2's compliment, then our answer will be minus 128.

  300. Speaker 2

    You can do it. Let's do it quickly. What is our number? Our number is 1. Sorry.

  301. Speaker 1

    Let's write it like this, our number is 1 and 7 times 0, 4, 5, 6, 7. If we take out 2's complement, so in 2's, first we take out the first complement. So let's reverse everything, its 0, 1, 1, 1, 1, 1, 1, 1. And then to take out the second complement, what we do is add 1 to it. You have done this many times, 1 and 1's 0 becomes carry 1, then 0, carry 1. 0, 1, 0, here also 1, 0, here also 1, 0, here also 1, 0. And finally here 1, which will come here. Number will be this. If you take out its decimal, you will get 128. In the original number, our MSB was 1. In the original number, our MSB was 1. That means, if we put a negative sign here, then the final answer will be negative 128.

  302. Speaker 2

    Very good. That means, our value will become minus 128. Okay.

  303. Speaker 1

    Now, I will remove this for once. We know how to do everything. We will go back to our definition. So I hope you understood that we kept doubling it but till a limit. What was that limit?

  304. Speaker 2

    It was till here.

  305. Speaker 1

    Everything doubled till this limit but as soon as 1 came here, our whole number became negative.

  306. Speaker 2

    We know this.

  307. Speaker 1

    What is the range of a byte? From minus 128 to plus 127. This is the range of your byte. If you keep 1 till here, then everything is fine. But as soon as 1 comes to this MSV, your whole number will become negative. Okay, very good. I understood this too. If I keep doing B left shift 1, then it will become negative after a limit. Okay, what is that limit?

  308. Speaker 2

    Think carefully. A byte has 8 bits, okay? And it was safe till this bit, till this bit position. And what is this bit position? This is the 0th position, this is the 1st position, 2nd, 3rd, 4th, 5th, 6th.

  309. Speaker 1

    It was safe till the 6th position. And it became negative at 7th. Nikki, if we have a number of 8 bits, then till the 6th bit, will be positive and on 7th bit.

  310. Speaker 2

    It will be negative. Right?

  311. Speaker 1

    That means if the number is of n bit then till n-2 bit it will be positive and on n-1 bit it will be negative.

  312. Speaker 2

    Very good. I also understood that it will be negative on n-1 bit. Okay? Great. Now let's talk about what happens if I write B left shift 1 again. What will happen? Now this will happen.

  313. Speaker 1

    Common sense tells us that if we shift again to the left, then all the bits will shift to the left again. And this 1 will come out again. And the number that is left empty, a new 0 will come here. This means that our range is this only, because it is outside the range. So finally, the number that will be our number, that number will be again 0.

  314. Speaker 2

    Right? Because the whole number is 0. Right?

  315. Speaker 1

    Now, the interesting thing is that after this, no matter how many times you do left shift, if you think logically, then if I write again, B left shift 1, So what will happen? This zero will come here and all the bits will shift to the left and the new one will also come to zero. That means even here everything remained zero. Again the answer is zero. Again I will shift to 1, again everything will be zero and again our answer will be zero.

  316. Speaker 2

    So what does this mean?

  317. Speaker 1

    Now my left shift has stopped working because all my starting bits have become zero.

  318. Speaker 2

    And what about these numbers? The ones coming out of range, the ones coming on the 9th bit.

  319. Speaker 1

    Similarly, some will be on the 10th bit, some will be on the 11th bit.

  320. Speaker 2

    What about these numbers? Okay?

  321. Speaker 1

    So, let's understand the bitwise operation in a bit in-depth in Java.

  322. Speaker 2

    Because if you.

  323. Speaker 1

    Watch any bitwise videos on YouTube, they will tell you what is left shift and what is right shift.

  324. Speaker 2

    Okay? We just read right shift, but right shift is the opposite. We will see that now. But you must have understood left shift. What is happening?

  325. Speaker 1

    If I write B left shift 1, then all our bits shift one by one. If I write B left shift 2, then all the bits, 2 bits will shift.

  326. Speaker 2

    Very good.

  327. Speaker 1

    But the question is, what is its limit? Like I shifted till here, then everything became 0. But if I shift this many times, then our answer will always be 0.

  328. Speaker 2

    Right? And what about the rest of the bits? The 9th bit, 10th bit, 11th bit, what about all of them?

  329. Speaker 1

    So now we will go a bit deeper in Bitwise.

  330. Speaker 2

    You will enjoy understanding. Look.

  331. Speaker 1

    First of all, let me remind you of one thing. We had talked about type promotions. Do you remember? What did your type promotions say?

  332. Speaker 2

    We know.

  333. Speaker 1

    Our type promotion used to say that our bite, that our Byte or Short is promoted in Int in any expression.

  334. Speaker 2

    Right?

  335. Speaker 1

    What does it mean? Suppose I have written Byte B is equal to 5. And I write B is equal to B into 5. If I write something like this. So this means that I am trying to assign the result of b into 5 to b.

  336. Speaker 2

    Here I get compiler error. Why?

  337. Speaker 1

    Because it will say that I will promote this b into integer. And then I will do int into 5, whose result will be an integer. And you are trying to put that integer in byte, which is a big number. You are trying to put it in a small number, which is an error. Now, how did we solve this? We used to do type casting here. We used to write byte. To tell the computer that what I am doing, I am doing this intentionally. We had also discussed that why is this type promotion? So now we will not go there, you can watch that video again. But now I am telling that because of type promotions, what difference does it make on our bitwise operations?

  338. Speaker 2

    Okay.

  339. Speaker 1

    So I hope you must have understood that type promotion happens. Now let's see whenever you do type promotion on left shift operator or on right shift operator.

  340. Speaker 2

    So what happens?

  341. Speaker 1

    So it happens that these operations are either on int or on long. This is correct, but they never apply on bite and shot. This means that what we were doing above was correct, but it was a half story.

  342. Speaker 2

    The full story is that if you try to.

  343. Speaker 1

    Apply them on bite and shot, then what will happen is that the tie will be promoted on int. They will be less on long when the limit of int also goes out, but we will not talk about that now, we will talk about int. If you perform these shift operations on byte or short, then they will be performed on integer from inside like this line. When you do bx5, then byte is not multiplied by 5 from inside, but byte is first promoted towards int and then we multiply this integer by 5. So similarly, when you were writing here, B left shift 1, B left shift 1, then the B shift that was happening here was actually not a byte shift. First, this byte shift was happening in the integer and then the integer was shifting left and right. Obviously, your answer will still be the same. If you assume an integer instead of a bit, then also 8 to 16, 16 to 32, 64 will be the same.

  344. Speaker 2

    The real story will start when we.

  345. Speaker 1

    When we have shifted everything to this range and our 9th bit has become 1, what happens after that? For that, you have to understand the rule of type promotion carefully. Your shift operations are performed in int and long. If you try to do it in byte or short, then type will be promoted in int and will perform above it. Then you can type cast it again and put it in byte. That is your choice. You can't write this line directly on the compiler. You have to typecast byte here as we did earlier. This is very important. Now see how the game changes completely. The game changes like this. If I tell you again that I had a number.

  346. Speaker 2

    Byte. B is equal to 1. I had this number. Simple. I had represented this in 8 bits. 1, 2, 3, 4, 5, 6, 7, 8 bits. Okay.

  347. Speaker 1

    So, here I write 1, 2, 4, 8, 16, 32, 64, and 128. And what I had said, how do we represent 1?

  348. Speaker 2

    Here we write 1. Rest, everything becomes 0. Okay. Very good. Right? And this answer was 1.

  349. Speaker 1

    Now, I had said, That I will shift this, first I will shift once, then twice, better than this I will write B left shift 7 times As soon as I shifted 7 times, this 1 came up and here everything else is 0 Very good, right? And what would be our final number? Minus 128, we saw it above Now understand this story carefully. The story is that when I have written in the memory, let's say b is equal to b left shift 7. So what would have happened from inside? First of all, this b would have been converted into an integer. And I would have shifted that integer to 7 bit. And again I would have assigned it to a bit. That means here I would have written byte. Here I would have done type casting. So actually if I write this statement correctly, then it would be something like this.

  350. Speaker 2

    Sorry. It will be something like this. B equal to type cast to byte. B left shift 7. Something like this will be our statement. See, what I said. This is our byte. So it would have happened that Byte takes 8 bits So I have represented it in 8 bits Very good But as it was typecast in integer Our integer takes 32 bits Do you remember that integer is of 32 bits?

  351. Speaker 1

    This means To represent our integer We would need 32 bits like this We would need 32 sections But here we have only 8 sections So how would we have done this?

  352. Speaker 2

    So let's see it again.

  353. Speaker 1

    Okay that suppose we had this number, we had bit B equal to 1, and this is its binary, I have expressed it. What will happen, as soon as I wrote B equal to B left shift 7, what will happen from inside? So from inside this will happen, first of all, our byte will be converted into integer, that means now we need 32 positions. So now if I do this and do this, then 32 positions will become very big. So first I will show you only 8 positions, 1, 2, 3.

  354. Speaker 2

    4, 5, 6, 7, 8 positions.

  355. Speaker 1

    Now you can assume that we have made 8 positions here Similarly, some 8 positions are here So 8 plus 8 is 16 Then 8 positions are here That is 24 And then 8 positions are here So total is 32 So 8 plus 8 plus 8 And this is 8 So I am just showing you the starting 8 positions Meaning the lowest positions I am just showing you that So that it is easy You can assume that there are 8 positions here as well Now this is our int I will write it here Now this type is our integer type of which I am teaching 8 bits of starting. Now, on this integer, these 7 bits will be moved, right? 7 bits will be moved on the integer, which means this 1 will come up here and the rest will be 0. Like this, like this, like.

  356. Speaker 2

    This, like this. Something like this, okay?

  357. Speaker 1

    Now, if B left shift 7 When I did this, its output was this Now obviously, this 0 moved here, it moved on 9th bit And 9th bit exists here because we have 32 bits 9th.

  358. Speaker 2

    Bit also has a 0, it moved on 10th bit 10th bit also has a 0, it moved on 11th bit So all the remaining bits are 0, just remember This bit is 1 This means that definition wise, our MSB is not this This is our starting 8 bit, our MSB must exist somewhere here Our 31st bit will be here and this is our 0th bit So our 31st bit will be our MSB And our 31st bit will be 0 this time Because this bit is 1 and this bit is 0 So this whole number is a positive number.

  359. Speaker 1

    So its answer should be plus 128 But then we type cast this operation in byte See here it is written that type cast the answer of B left shift 7 in byte Because you can't write it simply, you will get compiler error As soon as we type cast it in byte What will our computer do? The 32 bits will be truncated with the 8 bits of the starting. And this byte will be truncated with the 8 bits of the starting. We discussed earlier that this is an integer of 32 bits. If you want to represent it in a byte, which is of 8 bits, then the previous 8 bits of this 32-bit will come here and the rest of the values will be lost. These least significant 8 bits will come here.

  360. Speaker 2

    This is called truncation.

  361. Speaker 1

    So I took the starting 8 bits and put them here. The rest of the bits got lost and my number became this. Now as soon as this number is created and it is stored in B and I wrote getB which is my statement system.out.println when I write this and now I have written B then what will happen? When we go to the memory and check the number, we will get this number. Now according to this number, this is its MSB because now only 8 bits are left here and this is its LSB. So now the compiler will think that this is a negative number because here our most significant bit is 1. So it will tell this number negative. It will tell us minus 128. Which is also correct because we typecast it. So this was the story. How did we simply explain it? That if we keep shifting each.

  362. Speaker 2

    Bit left and right, then.

  363. Speaker 1

    Finally this number will become negative. But it doesn't happen like that. Remember that the shifting is always in integer and then we truncate it. So this is how that negative number is made. Now this story is important because let's say if you again... I will shift one more position in B. I will write B left shift 1. First 7th position has been shifted. I am shifting one more position and I am typing it from 1. As soon as I have written this statement, now you have to understand this is a very important concept. So those people who don't know bitwise very well, they will say that as soon as I shift left again from 1, then what will happen?

  364. Speaker 2

    All the positions.

  365. Speaker 1

    Will move to the left and here we will have only 0, 0 left. What I mean to say is that if I first make their position 1, sorry. 1, 2, 3, 4, 5, 6, 7, 8 So what will happen after shifting? This 0 will come here, this 0 will come here Like this everything will move to the left This 0 will come here and this 1 will come out And a new 0 will come here And our overall number will become simply 0 Right? If you do this in compiler also then only 0 will be the answer So if you write b first, b left shift 7 And then you will do B left shift 1 again And typecast here and here as well So 0 will come But we will have to understand why 0 will come It is very important Look carefully When our number was made When we were at this stage And we wrote B left shift 1 So what will happen in the background? In the background this will happen That our byte will be typecast in integer Now how will it be typecast in integer?

  366. Speaker 2

    This is our byte And this is our integer.

  367. Speaker 1

    Now imagine, how can I convert this byte into this integer?

  368. Speaker 2

    I will take the starting 8 bit and put it here. And what will I do with the rest of the bits? I can make the rest of the bits 0 0 0 here. When can I do that?

  369. Speaker 1

    If I have the original number here, the MSB here, if that is also 0, if the number is 0 here, then I can put 0 everywhere, right? Because that is a positive number. But you imagine that the number here is 1. If the MSB is 1 in bytes, then can I blindly put 0 here? Not at all. If I put 0 here, then this number will become positive again. But here 1 means that this whole number should be negative. So I have to put 1, 1, 1, 1, 1 here instead of 0.

  370. Speaker 2

    Right?

  371. Speaker 1

    That means if I want to represent this minus 128 in the integer, then it should also be minus 128 in the integer.

  372. Speaker 2

    Right?

  373. Speaker 1

    If I put 0, 0, 0, 0 everywhere here, and kept the same here, then this number will become a positive number overall. In the same way, to represent minus 128 in the integer, If I keep it like this and make all the zeros here, then this overall number will become my plus 128, which I don't want. That's why I have to do the same here. I have to store 111 in all the remaining 24 bits. And the remaining 8 bits will remain this. So that our overall number becomes minus 128 only. You can take it out. If all 24 bits are 1, And the remaining 8 bits are the same which were 1 and the remaining 7 bits are 0 When you will complement these 2's then the answer will be minus 128 And this is what we wanted So you understood this first Now this is important because if you understood this That when we did B left shift 7 Right, till here our work is done, so we saved it in B. Then we did B left shift 1. Means our number, when we left shift 1 again, so first this B converted in integer. And when it converted in integer, what will happen?

  374. Speaker 2

    I told you.

  375. Speaker 1

    First 1, 1, 1, doing like this, 24 times 1 will happen. I will write this 24 times. Okay, after that what will be our number? After that our number will be, what is written here, 1.

  376. Speaker 2

    Rest 7 times 0. 3, 4, 5, 6, 7.

  377. Speaker 1

    Something like this. So this is 8 bits. And this is 24 bits. Let's do it like this. So our overall number will be this. Now look at this number. Left shift 1 will be performed, i.e.

  378. Speaker 2

    This operation will be performed. Now if we do left shift 1 from here, then you know what will happen?

  379. Speaker 1

    This means that all the bits in the front have become 0 Which we had already shown you, all the bits in the front have become 0 But all the 24 bits in the back are still 1 All the 24 bits in the back are still 1 and this is our number And when we again typecast this in bytes Then we again truncate the starting 8 bit and we show that the number is 0 Okay, this means that what would happen if I had not written this Let's say int I equal to B left shift 1. If I had written like this, what would have happened? Now you will understand why you have read what you have read till now. And how they are different from those kids who understand.

  380. Speaker 2

    Bitwise very well.

  381. Speaker 1

    Now if you had written like this, that B left shift 1 and put it in I. So you're saying that when you typecast B into int, sorry, when you typepromote it, you'll do left shift 1 in that, so this number will be made, whatever we've made, that the starting 8 bits.

  382. Speaker 2

    Are all 0s, rest of the bits are all 1s, right?

  383. Speaker 1

    You are saying, now I have to assign this entire number to an int, not a byte. Now if I have to assign this to an int, then I don't need to do any truncation. I can assign this entire number as it is to an integer variable i.

  384. Speaker 2

    Absolutely right.

  385. Speaker 1

    Now when I will try to print i, then you see, because my 24 bits starting is 1, and rest of the 8 bits are 0, then this number will become a negative number, and this number will become minus 256. This number will be minus 256.

  386. Speaker 2

    Right?

  387. Speaker 1

    Because see, it was here till 128. Jumped on this. See, it was 128 here. Jumped on this. So, what is the next double number?

  388. Speaker 2

    256.

  389. Speaker 1

    But the number will be negative. So, our number will be negative 256.

  390. Speaker 2

    Understood? Here, what was our number till now? It was 0.

  391. Speaker 1

    It was 0 because the starting 8 bit was truncated. But if we look at the integer number beyond 8 bit, then our number will be minus 256. I hope you have understood this concept. This is a very important concept because people don't understand bitwise so well. You just have to understand that bitwise always performs on integer or long. People make this mistake that if I had a number which was 1 and I kept on left shifting it and if it was a byte then I will write b is equal to b left shift 7. As soon as I write this, my number will become negative. If I write 8 then my number will become 0. See here in the starting all the bits became 0 because all the 8 bits went left. Now, after this, whether I do 9 or 10, basically means, B left shift 9, B left shift 10, B left shift 11, no matter how much I do, now my number will be just 0 here. Meaning, no operation of shifting is performing now. But actually, it is not like that. All our bits are not lost, they are stored in integer. And if you don't typecast it, don't truncate it, try to print the entire integer Then you will get the double of minus 128, minus 256 Then the double of this minus 512 Then the double of this minus 1024 And then the double of this. By doing this, you will keep getting it. Now, till when will we get this? That is a very good question to ask. So, I told you this. All our bitwise operations are performed on an integer. And I told you that we were just truncating the data in the byte. Which was wrong because we were not able to see the whole picture. So if you want to see the real picture, you have to print the entire integer. So now let's assume that I already have the number int I equal to 1. And I will shift to I, let's say I just shifted once. So if I have I1, then you know, I means 32 bits, so I have to represent 32 bits. So I don't do it now, let's assume these are the first 8 bits, then these remaining 8 bits, these remaining 8 bits, 8 plus 8 is 16, plus 8 is 24, and I represent the remaining 8 bits like this, 1, 2, 3, 4, 5, 6, 7, 8. I will represent something like this.

  392. Speaker 2

    Okay, very good.

  393. Speaker 1

    Now what did I say? I left shifted i from 1. So nothing will happen. Earlier my i was 1. I mean it was only 1 here. I will make it white.

  394. Speaker 2

    It was 1 here. Rest here it was 0 on all. Something like this. Here it was 0 on all. Here it was.

  395. Speaker 1

    0 on all. And here it was 0 on all. This is my 20... Sorry. This is my 31st bit. And this is my 0th bit.

  396. Speaker 2

    Okay?

  397. Speaker 1

    So I hope you have understood till here. Now you tell me, how long can I shift this? I can do left shift 1 again and again.

  398. Speaker 2

    Right?

  399. Speaker 1

    How long can I shift this? This 1 will come here, here, here, here. How long can I move this? The answer is, I can do it straight. If I had not written this here, I would have written it straight. I left shift 30.

  400. Speaker 2

    If I would have written 30 directly, do you know how far I would have moved? I would have moved to this bit. The bit right next to the MSB. I will make this too. Let's say, I will write it here. There will be some numbers like 0, 2, 3, 4, 5, 6, 7, 8. I have written 8 zeros here. They are very small, but you can adjust them. And which is our 31st bit? This is 31st bit. Okay?

  401. Speaker 1

    So when I do I left shift 30, then basically I... could shift till this bit. So this bit would be 1 and rest of the bits would be 0. Now this would be a very big number because my 31st bit is still 0 and this number is 1 and the rest are 0s. So this would be a very big number. Now if I write I left shift 1 one more time, then my 1 would come up here. That means my MSB would be this 1 and the rest would be 0. Now as soon as I write this, then again what would happen? Compiler would think that there is a negative number and my answer would come in negative. That means whatever the biggest negative number is, which can store integer, I would get that. Interestingly, what can I write by combining these two statements? I left shift 31 times, right? So if you do I left shift 31 times, then the number you get, the negative number you get, is the maximum negative number of the.

  402. Speaker 2

    Integer that it can hold. Right?

  403. Speaker 1

    As you know, in 8 bit, if I do 1 here and the rest are zeros, then this number would be minus 128.

  404. Speaker 2

    And what was the range of bytes? Minus 128 to plus 127. Meaning the most....

  405. Speaker 1

    Small negative number, not big, I was saying the opposite, the smallest negative number, which it can hold, we get that, right, in the same way, if I left shift an integer, 31 times, then I will get integer, which can hold the smallest number, right, the smallest number, which can hold, this number, in java, if you write, integer, dot min value if you write something like this then you will get the same number which we are talking about again this is already written in java we also call this negative infinity in many cases we will discuss further why we call it okay now you can assume that this is the smallest number which can hold an integer which we will get if we left shift i 31 times now the last question the most interesting question if I write i left shift 32 then is this also applicable Then I wrote I left shift 33 or 34 or any other big number, is that applicable? Look, the answer to this is very interesting. The answer is no, not applicable.

  406. Speaker 2

    Why?

  407. Speaker 1

    Look, you know, we do actual shift operations on integer, right? Byte and short were always type cast, type promote in integer. We can do actual shift operations on integer. Now if we do an actual shift operation in integer and java thinks that if you make a least significant bit as MSB by moving it 31 times. If 1 was here and you moved it 31 times then it came to MSB. This was LSB.

  408. Speaker 2

    After this, there is no sense in moving it. Because your LSB will be lost. Right?

  409. Speaker 1

    Your LSB will be lost. That means, your least significant bit, if it also goes out of this range, then your whole number will be gone forever.

  410. Speaker 2

    Everything will become zeros. Right? So, what does Java do?

  411. Speaker 1

    To keep any processor optimized, because if you shift from LSB to MSB, even after that, if you start shifting, then the number is completely gone. So, there is no logic in doing the calculation after that. So, Java modifies all the shift operations in integers or even in long. For example, whenever you shift something, it passes through mod32. That means, if I write I left shift 31, then it will remove I left shift 31 mod 32. And do you know the special thing about mod operation? What does mod do? It takes out the remainder, right? So if you mod anything from 32, then the remainder can come from 0 to 31. No one can get a bigger number than this. That means your integer is always capped. That means you can left shift from 0 to 31 in any integer. After that, you can't do it. Because if you tried to do 32, then 32 mod 32 will become 0. That means you will come back to this. If you try to do 33, then 33 mod 32 will become 1. That means you will come to this and you will start rotating again. So 0 to 31 went, then again you will rotate 0 to 31, then again 0 to 31. You will not be able to get out of this cycle. And why did you do mod 32?

  412. Speaker 2

    Because there is a range of integer 32.

  413. Speaker 1

    It will not let you get out of that range. This means to say, and this is very interesting. If I write I mod 32, then it is equivalent to I mod 0 because 32 mod 32 is 0. And what does I mod 0 mean? The original number, I left shifted 0 times on it. It means I didn't left shift. So I left my original number there. So it is equivalent to I. And similarly, I left shift 33 is equivalent to I left shift 1. So I hope you understood the whole story If you can shift left to maximum You can do it till 31 Because 31 is the maximum range See here on 31st is your MSB, most significant bit So you can put any LSB to maximum and put it to MSB After that if you try to shift again Then it will say that I am applying mod operation of mod 32 So if you shifted I to left 32 times then mod 32 means You left shifted I zero times. So the original I will remain the same.

  414. Speaker 2

    Simple.

  415. Speaker 1

    I hope you understood the whole story. What is left shift? How does it work? And how does left shift actually work in depth? You must have understood that. I also told you that in byte and short, left shift operations don't work. Even right shift. In general, any shift operation doesn't work in byte and short. It only works in integer and long. And when we express it in bytes, then we see a truncated form of it. We see the starting 8 bits. The whole operation is going on in the remaining 24 bits. And we also saw that in integer, you can shift a maximum of 31 times. Whether you shift it left or right. Because after that, at the 32nd time, your shifting becomes modular.

  416. Speaker 2

    Why?

  417. Speaker 1

    So that your processors keep running at a speed.

  418. Speaker 2

    Because it's a useless calculation.

  419. Speaker 1

    Once it reaches LSB, MSB, then that number will be over.

  420. Speaker 2

    So it's not there, that's why it runs it in mod operations. Okay?

  421. Speaker 1

    So if you understood the whole story of left shift, then right shift is the opposite of this.

  422. Speaker 2

    Okay? We don't need to waste time in that. So if we see, right shift....

  423. Speaker 1

    So, this is completely opposite. Again, I will take an example from bytes. Let's say I have a number, byte B equal to 8.

  424. Speaker 2

    Okay. So, how do I represent 8? From 1, 2, 3, 4, 5, 6, 7, 8. And if I write everyone's range, then 1, 2, 4, 8, 16, 32, 64, and this is our 128. Now, what does 8 mean? Here 1, rest of it is 0. Okay, very good.

  425. Speaker 1

    Now, if I write B is equal to B, right shift 1. Obviously, you know that I have to type cast here. Because it is not allowed. Because B will be in integer first. And if you do any operation in integer, then the final result is integer. And you can't express that integer in byte. So you have to type cast. You know that.

  426. Speaker 2

    Okay. Very good.

  427. Speaker 1

    So I wrote this. Now see, right shift means the opposite. Now move all the bits right. So what will happen? This bit will come here. This bit here. This will be 1 here and the rest will be 0. This is here. Now what will come here? Here we can't write directly 0. Again same rule because this is your MSB. And MSB termines the sign of the number. If we write 0 directly, then even if your number was negative before, it will become positive. We will have to see what was the original sign there. So, before there was 0 on MSB, so we took 0 for the new bit. If it was 1 here, then we would have taken 1 for the new bit.

  428. Speaker 2

    Okay? So, I hope you understood this too. So, for now, we will take 0. Okay? So, look.

  429. Speaker 1

    Earlier, our number was 8. We did right shift. Our number became 4. Because here, 1 is on 4.

  430. Speaker 2

    Right?

  431. Speaker 1

    If you right shift it again, the number will come here and the rest will be 0. And the number will become 2 because there is 1 here. That means if you right shift it, it looks like you are dividing it by 2. It's the opposite. You must have understood the reason for this. Because we move the number to the right side where half the number is present. So if there was 8 here, it would be 4. It would be the 4th place. If there was 4 here, it would be the 2nd place. So the overall number becomes half.

  432. Speaker 2

    Simple.

  433. Speaker 1

    Now, if I would have divided it by one more, then here I would have got 1 and the rest would have been 0.

  434. Speaker 2

    And here I would have got 1. Isn't it? Same concept.

  435. Speaker 1

    Now, if I look at it for negative numbers, then let's suppose, here I would have got 1 and the rest would have been 0.

  436. Speaker 2

    Something like this. Okay? Meaning there would be a negative number.

  437. Speaker 1

    Now in this negative number, if I would have shifted, then here would have shifted 0, here would have shifted next 0.

  438. Speaker 2

    By doing this, here would have shifted 1.

  439. Speaker 1

    And the new number would have come here, that too 1 would have come.

  440. Speaker 2

    Right? To maintain that negativity.

  441. Speaker 1

    So if you look carefully, then we have in byte, the biggest negative, the smallest negative number is minus 128. How do we represent it? 6, 7, 8. So here we write 1 and the rest will be 0. So the value of this will be minus 128. Now if we right shift it from 1, what will be our number?

  442. Speaker 2

    Let's see. Our number will be 1 here.

  443. Speaker 1

    Now if we right shift it from 1, what will be our number?

  444. Speaker 2

    Let's see. .

  445. Speaker 1

    And what will be the new number here? Here also the new number will be 1. Because here it was 1. Our number will be this. If you subtract this number from the decimal again, then what will be your number? Minus 64. You can subtract it and see. Take its two complements. First the first complement, then the second complement. You will get minus 64. Which is right, isn't it? If we do half of minus 128, then minus 64. Then if you subtract it again, you will get minus 32. Then if you subtract it again, you will get minus 16, then minus 8, then minus 4. And it will continue like this. Now, the interesting thing is that if you write b equal to b right shift 1, then there is no problem. Truncation will also work here. But the best thing is that your LSB is always lost. And the new value always comes towards the most significant bit. This means that if you write a byte b right shift 1, sorry, this means If you keep doing right shift operation in byte or in short, then you will get expected result. You will never feel that there is any wrong output which is not in left shift. You can apply the same concept if I do all this on int. So if I apply the same operation on int, let's say I write int i equal to 128, so if I right shift that too, then overall we would get the same result. What I said earlier is that actual shifting operations are performed on int and long only. Because on byte and short, truncation happens. So you can apply the same logic here as well. The actual shifting will be on long or integer. After that, if you try to put it in byte or short, then you will only get truncated value.

  446. Speaker 2

    It's simple.

  447. Speaker 1

    And your mod32 rule is always applied. That you are more than 32 bits. Mod 32 bit rule is also applied here. Obviously, you can't move anything more than 32 bits. 32 bit means that you have moved something from MSB to LSB. This is your 31 moves. After this, if you take the 32nd move, then what will happen? This will also come here, which will be lost. The whole number has changed. This means you do mod 32. So, this means if you try to shift outside 31 to 32, then it is equivalent to that you are.

  448. Speaker 2

    Shifting 0 times, which we saw earlier also. Okay. Same thing, same rule will be applied. No problem.

  449. Speaker 1

    Now, one last thing is left. We saw left shift, we saw right shift. We had one more operation.

  450. Speaker 2

    This one. This is called shift right with zeros. Okay. What does this mean? It is very easy.

  451. Speaker 1

    It says that I am a dumb shift. This was a smart shift.

  452. Speaker 2

    Why?

  453. Speaker 1

    Because what we were doing in the smart shift, we were saying that before doing the right shift, we will see what is the number on the first MSB. If there was 1 there, then we would introduce 1 on the MSB. If there was 0 here, then we would introduce 0 here.

  454. Speaker 2

    Simple. That's why it was smart.

  455. Speaker 1

    Now, right shift with zeroes says, even if there is any number on MSB, I will always introduce 0. What does it mean?

  456. Speaker 2

    It is very simple.

  457. Speaker 1

    If I represent 8 bits of byte, 3, 4, 5, 6, 7, 8. And if we had, let's say, this number. The rest would be 0. On our MSB, it would be 1. So, its value would be, you know, what is its value? It would be minus 128.

  458. Speaker 2

    Right?

  459. Speaker 1

    Here, our number would be byte B.

  460. Speaker 2

    Let's say, now I....

  461. Speaker 1

    Byte b is equal to minus 128, this number. Now if I would have done b right shift 1.

  462. Speaker 2

    Right?

  463. Speaker 1

    So what would we have? We would have a number like this. This would have come here, this would have come here. By doing this, this 1 would have come here. The rest would have become 0. And the new number that would have been introduced would have been 1.

  464. Speaker 2

    Why?

  465. Speaker 1

    Because we have 1 here. So it would have come here. It's a simple thing. So you would have understood this. We had already studied this. But if I do the same thing. Byte right-shift with 0s 1 operation So what would happen? So we would get an output like this See, obviously all these bits would move, this would be 0 here, this would be 0 here Similarly, this 1 would be here, the rest would be 0s It looks exactly the same till here. But instead of 1, I would have simply put 0 here.

  466. Speaker 2

    Without thinking.

  467. Speaker 1

    It doesn't see what was the number on my original MSB. It would have simply put 0. It would have made a negative number a positive number too. If you do minus 128, it would be minus 64. Because you did half, but it would be plus 64.

  468. Speaker 2

    Right? Because look at this one.

  469. Speaker 1

    This is a normal right shift and this one is a right shift with zeros. Now what is the use of this operator? Sometimes there is no sense of negative values. For example, if you talk about pixels, you have an image and you know that there are small pixels and one pixel that there is an integer value. In that case, we always need a positive value. We don't have a concept of negative value in that. In that case, right shift with zeros is useful because we know that if we right shift, then even if MSB has 1 or 0, we don't care. We always have to introduce a new zero. It has a very specific use case. When you get that use case, you will understand it better. But there is nothing else in it. Right shift with zeros is just like that. So let's quickly go to the code and finish it. So first of all, let's see our bitwise operations. We have to write the code for.

  470. Speaker 2

    That. Okay.

  471. Speaker 1

    So let's do one thing, first let's take A which we call 2 and let's take B which we call 3.

  472. Speaker 2

    So what operations should we do?

  473. Speaker 1

    First let's do an operation let's say A and B. Then let's do an operation let's say D which is equal to A.

  474. Speaker 2

    Or B.

  475. Speaker 1

    Then let's do E which is equal to A exclusive or B simple. And let's do F which is equal to let's say not A. So we have done all this. Now we will print it. So we have printed it. C, D, E and F. All commas will be printed separately. So let's quickly see what the answer should be. Now you know what is the binary of 2. The binary of 2 is 1, 0. What is the binary of 3?

  476. Speaker 2

    It is 1, 1.

  477. Speaker 1

    Right now, we are writing their binary in 2 bits. 1, 0, 1, 1. But actually, the integer is of 32 bits. So, we should write their binary in 32 bits. But you will say, brother, before that, all the zeros will be there. So, what difference does it make?

  478. Speaker 2

    It makes a difference.

  479. Speaker 1

    When we will do 0, A, then all the zeros will be 1s.

  480. Speaker 2

    Right? So, we will make a difference. So, let's do one thing.

  481. Speaker 1

    First, let's write its binary in 32 bits.

  482. Speaker 2

    Okay? So, what does it mean? First, 4, 5, 6, 7, 8. 8 bits like this. Okay?

  483. Speaker 1

    So 8 bit 8 plus 8 is 16 plus 24 plus 32. So we have written it. And if we want to represent 2, then we will write 1 0 here at the end. What will happen for this as well? That first 8 bit plus 8 plus 8 is 24 and then plus 32. And if we want to represent 3, then we will write 1 1 at the end.

  484. Speaker 2

    Simple.

  485. Speaker 1

    Here 1 0 was there, so here also 1 0 is there. Here 1 1 was there, so we also wrote 1 1 here.

  486. Speaker 2

    Simple. Now we can do all the operations. Now see, A and B. What does A and B mean? Sorry.

  487. Speaker 1

    What does A and B mean? See, here is 0 and here is 1. What is the end of 0 and 1 if you do it? The end of 0 and 1 is 1. Sorry, 0.

  488. Speaker 2

    Right?

  489. Speaker 1

    And 1, here is 1 and here is 1. If you do the end of these two, what will come?

  490. Speaker 2

    1 will come. Right?

  491. Speaker 1

    And if you do the end of all these, with so many zeros, then it will always remain 0. So, 0 will be placed at the back. So, there is no problem. Our total answer is 1, 0. What is this?

  492. Speaker 2

    This is again 2.

  493. Speaker 1

    So, the answer of A and B is 2. Now you can think about A and B. If you add 0 and 1, you get 1.

  494. Speaker 2

    1 and 1, sorry, 1, sorry, number changed. 1 and if you add this 1, you get 1 again. Here also 1.

  495. Speaker 1

    If you add all these zeros, then there will be no difference.

  496. Speaker 2

    So, overall our answer will be 1, which is 3. Okay.

  497. Speaker 1

    If you do A exclusive and B, then you know what is exclusive and what is not. If there is an odd number of 1, then it is 1, otherwise it is 0. So, here is 0 and 1. So, if there is an odd number of 1, then the answer will be 1. Here also 1. And here also 1. Even number of 1s. Means the answer will be 0.

  498. Speaker 2

    So 0, 1. And the rest, you can see for yourself. Here all are 0s.

  499. Speaker 1

    And here also all are 0s. Means even number of 0s. Even number of 1s. Because 0s are 1s. So the answer for all will be 0. So there is no meaning to write.

  500. Speaker 2

    And 0, 1 means 1. So the answer for this is 2, 3, 1. Now this is interesting. Not A.

  501. Speaker 1

    Now what will not A do? Just reverse 1, right? That is, you do all these zeros once. That is, 1, 2, 3, 4, 5, 6, 7, 8.

  502. Speaker 2

    This is 8 times 1. Okay?

  503. Speaker 1

    Then let's copy this. Then see, all these zeros will again become 1 here. All these zeros will become 1 here. And this 6 zeros will again.

  504. Speaker 2

    Become 1 here. 6 times. Then this 1's zero. And, sorry. Yes.

  505. Speaker 1

    I have written 0 for this 1 and 1 for the final 0. So this will be the output. Now if you convert it to decimal again, then it will be minus 3. If you convert it to decimal again, then your answer will be minus 3. So your total answer is 2, 3, 1 and minus 3. Let's see quickly, this is being printed. So 2, 3, 1 and minus 3. make it to G.

  506. Speaker 2

    G equal to 1. and g is equal to g left shift 1. Let's do this.

  507. Speaker 1

    So you know that if g is 1 and this is of integer type then what is the meaning of integer?

  508. Speaker 2

    Let's copy this. Our g will look something like this. Let's comment this. Okay. This will be 0 and this will be your final bit 1. Okay. Very good.

  509. Speaker 1

    When we shift this once to the left side then what will be our output?

  510. Speaker 2

    Our output will be Wait a second, let's start it.

  511. Speaker 1

    From here So this 1 will come here And the new 0 will come here And the rest will remain 0 So if this was 1 Then this will be its double which is 2 And if we print G Then our output should be 2 So let's print G once Look, G is 2.

  512. Speaker 2

    Very good.

  513. Speaker 1

    If I shift again, it will be 4. If I do it again, it will be 8. So, let's shift it to 30.

  514. Speaker 2

    This 1 should be here. And the rest should be 0. Sorry.

  515. Speaker 1

    Yes, the rest should be 0. So, something like this. So, something like this answer should come. Now, what is this answer? See, I have moved this bit here. Meaning, here comes 1.

  516. Speaker 2

    Right?

  517. Speaker 1

    So, something like this big number will come. Now, even I don't remember that number. What is that number?

  518. Speaker 2

    So, let's see once.

  519. Speaker 1

    30 times shift, so our answer is this big. Now if I shift 31 times, this 1 will come here, and this will be 0, and this will be your smallest number, which can store integers, which is a minimum value. So let's see, 31 times a negative number should come, we talked about it. Yes, see this came, negative. Now if I shift this 32 times, So what will happen?

  520. Speaker 2

    What did I say?

  521. Speaker 1

    It means to shift 32 times. If we look carefully, then what does this mean? That you are doing 32 mod 32, which is equal to 0. Which is equivalent to, that you are saying g equal to g mod, sorry not mod, g left shift 0 times. Which means you haven't left shifted g. So our answer will be again 1. Which was g originally. Now, if we see, our 1 should be printed.

  522. Speaker 2

    See this, right?

  523. Speaker 1

    So, what we have read is absolutely correct about shifting. Now, if you shift it again 33 times, then 33 mod 32 1 will come. So, your answer will be 2 again. Because this means that we have shifted it only once. So, I will write it as G is equivalent to Sorry, I'll write it like this again.

  524. Speaker 2

    G equal to G left shift 33 is equivalent to G left shift 1.

  525. Speaker 1

    Okay, so the answer of 1 will be the same as this answer. And the answer of.

  526. Speaker 2

    1 is 2. So its answer should be 2. See this. Right?

  527. Speaker 1

    So I hope you understood all this. Now you can use your right shift in the same way. Right shift you can use.

  528. Speaker 2

    Right?

  529. Speaker 1

    And your right shift also has two shifts. One is your normal shift. This one and this one. Which you call right shift with 0. You can use this too. It will do the same thing. Let me tell you one more thing. Let's say I have a byte number. And I take the number h. And that would be 1 again. And I write h equal to h left shift 1. So see, here my error comes. And what error comes? Type mismatch cannot convert from int to byte. This is what we talked about. Whenever you left shift H from 1, the compiler will promote H in int. And then it will try to left shift int to 1. And then it will try to put that number back in H. Now when you put integer back in byte, you know there is an error.

  530. Speaker 2

    To remove this, we type cast. So we will write byte here. Sorry.

  531. Speaker 1

    And put it in a different bracket as you can see the error is gone now it will be type casted you know how to represent 1 in 8 bits 1 2 3 4 5 6.

  532. Speaker 2

    7 8 something like this comment it Something like this.

  533. Speaker 1

    Now when I left shift H from 1, then you know that first this number, type will be promoted in integer. Means this number will be this big. This number will be something like this. Let's comment this too. After that left shift 1 operation will be performed. Means here 1 will come.

  534. Speaker 2

    Right? And the rest will be 0.

  535. Speaker 1

    And then this, whatever the answer is, I am casting it in bytes. That means, the previous 8 bits will remain. The rest will be truncated. Something like this. And this will be your answer. And what does answer mean? It will be 2 again. So, it will be 2 only. But actually, how it became 2, we will have to know that.

  536. Speaker 2

    Right?

  537. Speaker 1

    We understood internally that first that type is promoted in int and then 8 bits are truncated in the last. So, if I print H like G, So, let's print this too quickly. Here we have H. So, let's see. We should also get 2 here. 2. Absolutely correct. So, this is the whole shift operations. You can play with it a little more. You will understand. I hope you have crystal clear everything.

  538. Speaker 2

    This is very important.

  539. Speaker 1

    Some people just read it from top to bottom. But it is very important to know it in depth. Now, let's move towards our next operators. So, let's move towards our next operator.

  540. Speaker 2

    Which we call... LOGICAL OPERATORS Okay, now what are these logical operators?

  541. Speaker 1

    See, this is very simple. If you understood bitwise, then you would have understood this. There are only two operators in this. One is AND AND and the other one is OR OR. By the way, it doesn't read AND AND, it reads AND only and it also reads OR only. Okay, but we read AND in bitwise and its AND looked like this, the single ampersand one.

  542. Speaker 2

    Right?

  543. Speaker 1

    And its OR also looked like this, the simple pipe operator. Why did everything become double in this?

  544. Speaker 2

    Right? Listen, the knot in that looks like this. Unary knot.

  545. Speaker 1

    In this, your knot will look like this. You have studied relational operator, so you don't need to study it. But in general, we are talking about two things. End operator and your or operator.

  546. Speaker 2

    Okay, what are they? How are they different?

  547. Speaker 1

    The simple thing is that your logical operators don't work on bits. Like bitwise operators used to work on bits. Okay, so what do they work on? They work on expressions. Okay, like suppose I wrote an expression and wrote another expression. To evaluate both of them, I used these two operators. Okay, what does it mean?

  548. Speaker 2

    Let's see.

  549. Speaker 1

    Look, let's say I have written int a equal to 5 somewhere here. And I have written one more expression b equal to 10. And let's say I take one more int c equal to 15. Now suppose I write an expression int d is equal to a less than b and a less than c. If I write a statement like this, then what do you understand from this statement? You must be understanding this statement that I did something wrong here, here int will not come, here boolean will come. Yes, you must be understanding this statement that the value we are going to get here is going to be boolean.

  550. Speaker 2

    Why?

  551. Speaker 1

    Because what am I asking? I am asking the compiler again, is a less than b? And here I am asking is a less than c question mark. I am asking from compiler. And then I am also telling compiler that I have put an and operator here. Means is it like this or like this.

  552. Speaker 2

    Now see.

  553. Speaker 1

    And operator, we had understood in bitwise also, even if we had read single and in it, and it would have worked on bits, and it works on expressions instead of bits. But from inside, their logical, conceptual thing remains the same. And that is, if both the conditions are true, then only the end result will be true. And if one of the two conditions is false, then the answer will be false. And if both are false, then obviously it will be false.

  554. Speaker 2

    Right?

  555. Speaker 1

    We saw this on the basis of zeros and ones in Bitwise. Here we can see it on the basis of true and false. So what I mean to say is that if this expression comes true and this expression also comes true, then only true will be stored in this D. If one of these expressions is false or both are false, then it will be false in D.

  556. Speaker 2

    Simple.

  557. Speaker 1

    So the concept I used for zeros and ones, in the same concept, I am using it for expressions.

  558. Speaker 2

    Okay, very good.

  559. Speaker 1

    Now, if I write the same concept in Boolean, and I use the same concept, let's say a less than b or a less than c. So, what does this mean? This statement means that I am telling the compiler that either this.

  560. Speaker 2

    Condition is true or this.

  561. Speaker 1

    Condition is true. If one of these two is true, then E will be true. We saw in the truth table that there are two, A and B. A can be 0, B can be 0, 0, 1, 1, 0, and 1, 1. So, we just converted this truth table to true and false instead of 0 and 1. So, if you make it here, instead of 0 and 0, you write false, false, false, true, true, false, And false, false.

  562. Speaker 2

    Okay? So the result of a and b will come here as well. Right?

  563. Speaker 1

    And the result of a and b will come here as well.

  564. Speaker 2

    A double or b. Simple.

  565. Speaker 1

    Here, you were working on bits, so you expressed it with zeros and ones. And here, you are working with expressions. And an expression either tells you.

  566. Speaker 2

    False or true. That's why you are using double operators. Okay?

  567. Speaker 1

    So basically, it means that here will be false. Here also false. Here also false. Sorry, I wrote something wrong here. It should have been true true. Not false false because then it.

  568. Speaker 2

    Would never be true. Okay, true true.

  569. Speaker 1

    Okay, so here also it will be false and here also it will be true. And in A and B, here also it will be false and here also it will be true. Right, so it was in the same condition. 0, 0, 0, 0, 0 and 1 and here also 0 and here also 1. Right, so it is exactly the same. The concept which we used in bitwise, we are using it in logical. Okay, I hope you understood that how are OR operator and AND operator looking here.

  570. Speaker 2

    Now this is interesting. You must have understood this. We had already done this before.

  571. Speaker 1

    There is one more thing more interesting than this. And we call that thing short circuit. Short circuit. Now see what short circuit says. Short circuit says that if I know one thing about AND. That both, suppose I have written A and B here. Let's talk about true and false. Suppose I have written.

  572. Speaker 2

    A and B.

  573. Speaker 1

    Let's say false false, false true, true false and true true. So, I know one thing about A and B. If one of the two is false, then my output becomes false.

  574. Speaker 2

    I know this.

  575. Speaker 1

    So, the concept of short circuit is that if I am going to end A with B, if I am going to do this, and I get to know that A, the statement, its result is already false.

  576. Speaker 2

    So, do I need to check B? No, right?

  577. Speaker 1

    Because if I get A's result false, then I know that the answer to this whole expression is going to be false.

  578. Speaker 2

    Because A is false. And if A is also false, then A and B are also false. Right? So, I don't need to check B. This is called short-circuiting.

  579. Speaker 1

    And this is used a lot in coding, programming, Java, or any other language where short-circuit is used.

  580. Speaker 2

    Why is it used?

  581. Speaker 1

    Because look, you can write multiple such conditions together with it. That you know, if the first one fails, then the second one should never be checked. When does this work? When we do null checks, we will discuss about nulls later. But short circuit works a lot, especially in loops and conditions. When you intentionally want that if the first condition fails, then the second should never be executed. In that condition, you use the concept of short circuit. So you should know that it exists. And one more thing, the opposite of this. Whenever you write A and B, you know that if A is true, then it will be true. So in this case, it will be false. In the rest of the three cases, it will be true. So this means, if I have written A and B, and my A result is true, then do I need to check B again? Because I know that B, whether it is false or true, it doesn't matter. The whole combined statement is true. So, I don't even check B. So, again, this is also called short circuiting.

  582. Speaker 2

    Okay?

  583. Speaker 1

    So, short circuit is used a lot smartly. And you will understand as we use it. But you should know that they exist.

  584. Speaker 2

    Okay?

  585. Speaker 1

    Okay, if you understood this, then it is a very interesting thing. Many people have a misconception, which I will remove today.

  586. Speaker 2

    Okay?

  587. Speaker 1

    That is, these bitwise operators, these ones, and single OR and unary NOT. Yeah, we can leave unary or NOT. We will talk about this simply. We can use these operators only for bits. 99% you are right. But in Java, smartly, you can use these bits operators with expressions too. Like we declared a5, b10, c15. And we wrote a less than b. And and a less than c. Instead of this and, I can write one and. And its result can also be assigned to a variable d. This is also a correct statement. But what will happen from inside? We know that this operator AND works on bits, but there are no bits here. Here we are talking about expressions. Nothing will happen. What will happen is that the one who does double AND, the one who gives logical output, that if both of them are true, then only the answer will be true. If one of them is false, then the answer will be false. If both are false, then obviously it will be false. But what will it do? It doesn't do short circuit. Does not do Short circuiting. This is a very interesting interview question. Many people think that short circuiting is always there in Java.

  588. Speaker 2

    But it is not. If you want, you can avoid short circuiting. How? You just use your bitwise operator between expressions.

  589. Speaker 1

    You can use bitwise AND instead of logical AND and you can do this, this is an interesting technique if you intentionally don't want a short circuit there are many specific use cases in java 99% of the time when you write code you never use this AND with expressions mostly you use double AND and same double OR which we call logical OR This will be a very limited condition. This is generally a smart interview question. It doesn't have much use case. But still, it is asked. And the people who know this, they stand out a lot. Because they understand the operators very well.

  590. Speaker 2

    Right?

  591. Speaker 1

    So, today I have explained this to you. You should enjoy it. Try using this. Those who have read CC + + before and know what short circuiting is.

  592. Speaker 2

    For them. Right? Let's go.

  593. Speaker 1

    This is your bitwise operator. Let's use it in the code. So, let's see our logical operators. Let's say I wrote a as 5. Let's take the same example. I wrote b as 10. And I wrote c as 15. Now, let's say I write a boolean variable d. And I write a less than v. And b less than c. And what should I do with that boolean variable? System.out.println And I will print d. So what should happen?

  594. Speaker 2

    According to me, it should be true.

  595. Speaker 1

    Because 5 is smaller than 10, a less than b. And 10 is smaller than 15, b less than b. Both conditions are true. And end says, both will be true, then only I will be true.

  596. Speaker 2

    So true. Absolutely correct. Right?

  597. Speaker 1

    If one of them was false, then the answer would have been false. So we can see, if instead of 5, it would have been 25, then your answer should have been False.

  598. Speaker 2

    Very good. Okay. Let's make it 5 again. You can also try the.

  599. Speaker 1

    Same for others. If we write more here, then the answer will be true. Because obviously, both the conditions are true.

  600. Speaker 2

    Okay.

  601. Speaker 1

    Okay, we had studied short circuit. And I had said that short circuit means that if there is OR and the first condition is true, which has happened in this case, then it will not evaluate the second condition. In this case, it means that it will not be compared to B or C. It will directly give the true answer. But if I say the opposite, if I put AND here and fail the first condition, let's say I said 25. Now because the first condition has failed because 25 is not smaller than 10 So it will not evaluate the second condition again So it will not compare with B and C This is called short circuit But if I put single OR here Let's say if I would have done double AND So the answer would have been FALSE Because A is not less than B If a is less than b.

  602. Speaker 2

    Then it is false.

  603. Speaker 1

    But in this case, I can say that b is not compared to c. Because the answer is false in a and b. But if I had put a bitwise end here, then also the answer would have been false. But I can also say that this time both are compared. Obviously, the answer of this statement is false. The answer of this statement is true. But false and true. If you do false and true, then you always get false. So the final answer is false. But you will ask, what is the meaning of this?

  604. Speaker 2

    What is the benefit of this?

  605. Speaker 1

    What is the difference if the previous statement is evaluated or not? It is used when you use it to increase a statement in conditionals. We will see that later. And it plays a very smart role. I told you that 99% of the time, only double and is used in programming. No single and is used. There is a 1% chance. But this is a very good question to be asked in an interview. So we should know, right? Rest, I hope you have understood all these short circuiting logical operators. Let's quickly move to our last operator and finish it. Now see, you have read our last operator many times. That is the assignment operator.

  606. Speaker 2

    Why did we keep it for last? Because once all the operators are finished, it plays a role. AssignmentOperator. What does it say?

  607. Speaker 1

    Let's say I write int a equal to 5. So this 5 is my AssignmentOperator. Assignment operator What does it mean? What I want to do here I am saying This value of my right side 5 Put it in A That's it This is what assignment operator says Remember that's why we talked about this equal to And what's the difference between this equal to This equal to is assignment And this lower equal to is relational Relational Because it's comparing So if I give an example of this Let's say I have an int a equal to 5 and a b equal to 10. If I write a equal to b, then this is a Boolean statement. I told you what a equal to b means. What are you asking the compiler? Is a equal to b? You are asking the compiler this. The compiler will answer this in true or false. In this case, it will be false. But if you write a equal to b, So this means that you are telling the compiler that whatever is in B, put it in A.

  608. Speaker 2

    Simple. So what happened here is that whatever is in B, put it in A. What was in B? 10.

  609. Speaker 1

    So 10 will also come in A. And this time when you do system.out.println If you print A, then your output should be 10.

  610. Speaker 2

    You can do this.

  611. Speaker 1

    So, this is the difference between assignment operator and relational operator. So, let's go back to relational operator. So, we took an example. Let's say, int a equal to 5. This means that whatever is on the right side, i.e. 5, if you put it in a variable, it will be a container a, we put 5 in it. Obviously, I put it in binary, to represent it, I showed it in decimal. Okay, you can also make a chain of assignment operator.

  612. Speaker 2

    How?

  613. Speaker 1

    I can write code like this, int a equal to b equal to c equal to 10. This is also a valid java code. Because here, see how it starts. First of all, it will look at the rightmost thing, which is a literal 10. It will put it in C first. Then it will come to B in C. The one in B will come to A. And in all of them, A, B and C, the value of 10 will come.

  614. Speaker 2

    Okay. So you can write like this too. What have you done in this? You wrote int.

  615. Speaker 1

    Only once, otherwise you would have written int A equal to 10 three times. int b is equal to 10, and you write int c is equal to 10. So you write this statement three times. It's better if you short it and write int once. And three variables are going to come. a is equal to b is equal to c is equal to 10. Write it three times and your statement will run. You can run it and see. So this was the assignment operator. You know that we have always used it. You just had to tell me its name.

  616. Speaker 2

    I told you that.

  617. Speaker 1

    Now, one last thing that we have to discuss, now you will say, there are so many operators, but when you use them, they will be used seamlessly, you won't even know. You won't feel like, I need to remember them. No, you just need to understand that these are operators. As you keep using them in code, they will easily sit in your mind. Now, the last thing left is a special operator. That is a special operator, which we call ternary operator.

  618. Speaker 2

    Ternary operator. Now, what is ternary operator?

  619. Speaker 1

    Ternary operator will be fun to read when we read conditionals. So, I will explain it well. When we read if-else, it will be more fun. But, let me tell you. Till now, all the operators we were reading, either it was unary operator, like unary not, because it is on the same operand. Or we had binary operators, like plus, minus, multiply, divide.

  620. Speaker 2

    Because it needs two operands, you will add two numbers.

  621. Speaker 1

    This is the only operator, ternary, which takes place on three operands. So, operand 1, operand 2, and operand 3. It takes place on three operands. Now, how does it take place? What does it do? We won't read about it now, because it won't be fun, we won't understand. We can understand, but it won't be fun when we read about if-else, about conditionals. So, I am removing this from here. Okay, let's just look at one more thing. Now you'll say, bro, how many things will you show? Let's do one more thing. But this is definitely the last.

  622. Speaker 2

    And that is operator precedence. Okay? So what is operator precedence? Look, till now we've read a lot of operators.

  623. Speaker 1

    We've read a lot of logical operators, arithmetic operators, bitwise operators. But think about it. When in one expression, let's say I wrote an expression. And it is written like this. int c equal to And I will use a lot of operators here.

  624. Speaker 2

    Like a into b plus d minus f Divided by G. Something like this.

  625. Speaker 1

    Then I will move it from left shift to 2.

  626. Speaker 2

    Something like this.

  627. Speaker 1

    So I have used a lot of operators together. So what will be its final output? Just like we have a board mass in maths. Which tells us the precedence. That division will be first, then multiplication, addition, subtraction. In the same way, we follow precedence in computer science. Obviously, if we follow the mathematics board maths rule, then the priority of division will always be higher, then multiplication will come, and then addition and subtraction will come.

  628. Speaker 2

    So, these are all precedence rules. So, let me show you that rule quickly.

  629. Speaker 1

    So, see, this is the table, this is the precedence table. In this, the highest precedence is at the top, and as it comes down, the precedence becomes low. And in the same row, their precedence is the same. In the same row, let's say there is divide and multiply. I said their precedence is same. If I evaluate them, how will they be evaluated? They will be evaluated left to right. Where is the equal to operator? It is the lowest precedence. This is evaluated from right to left.

  630. Speaker 2

    We had seen this.

  631. Speaker 1

    We had given an example above. First 10 to C, then to B, then to A. This is how the evaluation is done. Now look, don't get scared by looking at this precedence table. No one remembers it. It doesn't happen in anyone's mind. You don't even know. You just know that the lowest is equal to. Because after everything is done, you assign something to the left side variable. That's why its precedence is lowest.

  632. Speaker 2

    Apart from that, Now you will see that postfix is the highest, then prefix comes, then your arithmetic operator comes, multiply, divide, and modulus, plus, minus, then shifts comes, and etc. comes.

  633. Speaker 1

    So, you don't need to remember all this and you don't need to keep it in your mind. I will tell you why. This is theoretical concept. When you look at it in theory, then obviously precedence should be there. But you think practically, when you write code, then you never write such a complicated statement. If you want to write a complicated statement in Maths, then you will use brackets. Interestingly, you can use brackets in Java as well. The precedence of brackets is highest. Highest precedence. So, you can play with brackets and evaluate any expression according to you. So, let's say, if it's written somewhere, A equal to, let's say, B plus C into D. So, obviously, according to the rule of board mass, first C into D should be there. Then, whatever result will come, it should be plus B. So, B plus C into D. But, let's say, I don't want this. I want that first, B plus C and then multiply by D. So what will I do? I will do nothing. I will simply put a bracket in B and C. So what does this mean? This means first evaluate this and then multiply the answer by D.

  634. Speaker 2

    Simple. So I changed the precedence according to me using brackets. And this is very useful.

  635. Speaker 1

    You always put brackets in your big expressions. So like this one was our expression. I can put brackets on this too. Suppose I want a into b first. Then suppose plus d minus f.

  636. Speaker 2

    Right? Then no expression can carry this. First f plus g. Then d plus.

  637. Speaker 1

    Then b plus. Then a in the last. Something like this. I can set rules according to myself using brackets. And that is very useful.

  638. Speaker 2

    Okay?

  639. Speaker 1

    So, it doesn't make much difference from this precedence table. A little bit should be in mind that yes, equal to is the last. Theoretical, if you want to give someone knowledge or tell someone in an interview, then there should be a little idea that post-it prefix are more, then arithmetic comes, then your shifts come, etc. But, whenever you are literally working on an application, then if you are working on a complex expression, then you always control things with brackets, not with their precedence. Because it looks so bad that even if you remember the precedence, then you.

  640. Speaker 2

    Sit and You won't see that this will happen first, then this will happen. No. You will put simple brackets and reduce it. Okay.

  641. Speaker 1

    So here we have finished all the operators. We have not studied ternary yet. Which I said we will study when we will do conditionals. Apart from that we have seen all types of operators. We have seen its code. Now I want you to practice it yourself. I know the video has become very long. But those who have stayed till the end. Their operator's concept must have been cleared. If you have any question in your mind. Then please comment on it. And interact with me. So that's it in this video. See you in the next video. Till then thank you so much.