## The course is part of this learning path

In this course, we'll learn about Java data types and operators.

### Learning Objectives

- Variables
- Data Types
- Type Conversion & Type Casting
- Operators
- Operator Precedence
- Expressions, Statements and Block

### Intended Audience

- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

### Prerequisites

- No prior knowledge is required about the Java programming language.
- Basic computer knowledge

Hi there, in this video we'll talk about the Bitwise and Bit Shift operators. The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. The operators discussed in this video are less commonly used, therefore their coverage is brief. The intent is to simply make you aware that these operations exist. Let's look at these operators. Bitwise complement operator, the unary bitwise complement operator, tilde inverts a bit pattern. It can be applied to any of the integral types, making every zero a one and every one a zero.

For example, a bike contains four bits, applying this operator to the value whose bit pattern is 0000 would change its pattern to 1111. If you apply it to the 0000, this operator converts it to 1111. In short, this operator inverts each digit in the binary system. Signed left shift operator, the signed left shift operator double less than sign shifts a bit pattern to the left. The bit pattern is given the left hand operand and the number of positions to shift by the right hand operand. In other words, it adds the zero as much as equal to the value of the right hand operator given to it to the end of the number in the binary system.

For example, if the left hand operator is the 101 and the right hand operator is 2 the result will be 10100. So, it adds two zeroes at the end. Signed right shift operator. The signed right shift operator double greater than sign shifts a bit pattern to the right. The bit pattern is given by the left hand operand, and the number of positions to shift by the right hand operand. In other words, it deletes the digit as much as equal to the value of the left hand operator given to it from the end of the number in the binary system. For example, if the left hand operator is the 1011 and the right hand operator is 2, the result will be 10. So, it deletes the two digit from the end. Unsigned right shift operator.

The unsigned right shift operator triple greater than sign shifts a zero into the left most position, while the left most position after a double greater than depends on sign extension. Bitwise AND operator. The bitwise AND operator performs a bitwise AND operation. Bitwise inclusive OR operator. The bitwise inclusive operator performs a bitwise inclusive OR operation, it's represented by a pipe. Bitwise exclusive OR()

operator. The bitwise exclusive operator performs a bitwise exclusive OR() operation. Now let's move on to Eclipse and get some practice. In exercise project, right click on the operator package and select 'New Class.' Specify the class name as bitwise bit shift and select the checkbox for the main method. Okay, let's first start with a bitwise AND operator. First, I will define two integer variables int number1 = 10, and int number2 = 13. Now look at the binary equivalent of these variables. The literal 10 is 1010 in the binary system, and the literal 13 is 1101 in the binary system. Bitwise AND will compare each digit among itself, and if both digits are one it will calculate that digit as one, otherwise it will calculate it a zero.

So, this digit will be one since the first digits are both one. It'll be zero because the second digits are different. It's still zero because the third digits are different. And since the last digits are different, it will be zero again. Therefore if we compare these two numbers using the bitwise AND operator, the result will be 1000 in the binary system. This is equal to eight in the decimal system. Now, let's print them to the console. First, let's print the binary equivalent of the number1 to the console. S out "number1 in binary: + " Integer.toBinaryString method here. Here I write number1. Now let's copy this line and paste it twice. This will be number2. This will be number2 also. In this line let's print the binary equivalent of the result. Here I write number1 bitwise and number2. This will be number1 bitwise and number2. Finally, let's print the equivalent of the result in the decimal system, I copy and paste this line. Let this be in decimal. Now we can delete the toBinaryString method here. The compiler will print it as decimal anyway. Also, let's create a print method at the top of the print methods which indicates that these operations belong to the bitwise AND operator.

Yes, now let's run the application. Let's edit the print method we used as a title and test it again. As you can see, the binary equivalent of number1 is 1010. The binary equivalent of number2 is 1101. The binary equivalent of the results of the comparison operation is 1000. And the decimal equivalent of the result is eight. Okay, let's look at the bitwise inclusive OR. First, I will copy these lines and paste them here. Now the name of the first variable can be number3, and the name of the second variable can be number4, their value can remain the same. Bitwise OR will compare each digit among itself, and if both digits are zero it will calculate that digit as zero. Otherwise it will calculate it as one.

So, this digit will be one since the first digits are both one. This will be one again because the second digit is one, this is still one because the third digits are different, and since the last digits are different it will be one again. Therefore, if we compare these two numbers using the bitwise OR operator, the result will be 1111 in the binary system. This is equal to 15 in the decimal system. Now, let's print them to the console. I'll make some changes to these print methods. This will be OR. Also, I will write the number3 instead of the number1.

And I will write the number4 instead of the number2. And these signs will be bitwise OR, ie, single pipe. Yes, now let's run and see. The binary equivalent of the results of the comparison operation is 1111, and the decimal equivalent of the result is 15. Now let's look at the bitwise exclusive OR. First I will copy these lines and paste them here. This will be exclusive OR or XOR. Now the name of the first variable can be number5 and the name of the second variable can be number6. Their value can remain the same. Exclusive OR will compare each digit among itself and if both digits are different, it will calculate that digit as one. Otherwise, it will calculate it

a zero. So, this digit will be zero, since the first digits are both one. This will be one because the second digits are different. This is still one because the third digits are different and since the last digits are different it will be one again. Therefore if we compare these two numbers using the exclusive OR operator, the result will be 0111 in the binary system. This is equal to seven in the decimal system. Now let's print them to the console. I will make some changes to these print methods. This will be Exclusive OR or XOR. Also I will write the number5 instead of the number.

And I will write the number6 instead of the number6 and I'll change the sign of this operator. It is represented by this sign caret. Yes. Now let's run and see. The binary equivalent of the result of the comparison operation is 111. And the decimal equivalent of the result is seven. Now let's look at the signed left shift operator. First I will copy only this variable and paste it here and I'll copy these two print methods and paste them here. And lastly, I'll copy these two print methods and paste them here. Now the name of the variable can be number7 and its value can remain the same. The signed left shift operator shifts a bit pattern to the left. For example, let's shift this number2 digits to the left.

For this I write two to the right of the operator that means adding two zeros to the end of the number. So our new number is 101000. This corresponds to 40 in the decimal system. Now let's print this to the console. I'll make some changes to these print methods. This will be signed left shift. Also I'll write the number7 instead of number5. And I'll delete the second number everywhere and I'll change the sign of this operator. It's represented by this sign double less than signs and I write two just right of the operator. Yes. Now let's run and see. As you can see the binary equivalent of the results of the operation is 101000. And the decimal equivalent of the result is 40. Now let's look at the signed right shift operator. First, I will copy these lines and paste them here. Now the name of the variable can be number8 and its value can remain the same. The signed right shift operator shifts a bit pattern to the right. For example, let's shift this number2 digits to the right.

For this, I'll change this operator and write 2 to the right of the operator. What this means is to delete the last digits. So, our new number is 10. This corresponds to two in the decimal system. Now let's print this to the console. I'll make some changes to these print methods. This will be signed right shift. Also, I'll write the number8 instead of the number7 and I'll change the sign of this operator. It's represented by this sign double greater than signs. Yes. Now let's run and see. As you can see the binary equivalent of the result of the operation is 10 and the decimal equivalent of the result is two. Now let's look at the unary bitwise complement operator. First I'll copy these lines and paste them here.

Now the name of the variable can be number9 and its value can remain the same. The unary bitwise operator inverts a bit pattern. It can be applied to any of the integral types, making every zero a one and every one a zero. But when you use this operator, the compiler calculates the number you are processing as 32 bits while calculating. We'll see that shortly. It is represented by this sign, tilde. If I use this operator at the front end of the number, it will invert the digits as 0101. This corresponds to five in the decimal system. But since the compiler perceives it as 32 bits, it will also convert the leading 28 zeros of this number to one. So, it will print the results as -11, not five. In other words, it will print the difference between 16 and the results of the console. Because we've processed a four bit number and in this case we will look to the two's complement of it i.e 16. As a result we will subtract 16 from five and print the result as -11 to the console.

Now let's print this to the console. I'll make some changes to these print methods. This will be the unary bitwise complement. Also I'll write the number9 instead of the number8. And I'll change the sign of this operator. It is represented by this sign, tilde. Yes. Now let's run and see. We use the unary bitwise compliment in the first print method, let's fix it and test it again. As you can see, although we're using a four bit number it also adds the 28 leading zeros and converts all zeros to one and one to zero. And the decimal equivalent of the result is -11 not five. Let's take another example, I'll copy these codes and paste them here. The name of the variable can be number10 and the new value of this variable can be 17. Now look at the binary equivalent of these variables. The literal 17 is 00010001 in the binary system. If I use the compliment operator at the front end of the number, it will invert the digits as 11101110. This corresponds to 238 in the decimal system.

But since the compiler perceives it as 32 bits, it will also convert the leading 24 zeros of this number to one. So, it will print the results as -18, not 238. In other words, it will print the difference between 256 and the result to the console because we have processed an eight bit number and in this case, we'll look at the two's complement of it i.e 256. As a result, it will subtract 256 from 238 and print the result as -18 to the console. Now let's print this to the console. Also I will write the number10 instead of the number9. Yes. Now let's run and see. As you can see, although we are using an eight bit number, it also adds the 24 leading zeros and converts all zero digits to one and one to zero. And the decimal equivalent of the result is -18, not 238. Yes friends this is how the bit wise and bit shift operators are used. As I mentioned at the beginning of the lesson these operators are almost never used, but it's useful to know that there are such operators in Java. Let's take a short break here. See you in the next lesson.

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.