Scanner class Methods in Java

Java-Bitwise Operator

Bitwise Operator

Bitwise operators are the operators that perform operation on operands that are bits. These operators are extensive bit manipulation operators for programmers who want to communicate directly with the hardware. These operators are used for testing,setting,or shifting individual bits in a value.In order to work with these operators,one should be aware of the binary numbers and two's complement format used to represent negative integers.

The bitwise operators can be used on values of type long,int,short,char or byte but cannot be used with boolean,float,double,array or object type.

Types of Bitwise Operators

There are seven bitwise operators in java as shown below in table,
types of Bitwise Operators
Lets discuss these operators ,

1. Bitwise AND (&): It is same as logical AND operator it combines two integer operands by performing logical AND operation on them. It only works on bits. We know, 0 is an Off bit and 1 is an ON bit. So,If and only if the both operands are 1 then it will return 1 as result.If any of the operand is 0 or both operands are 0, it will return 0 as result.
For example: 0 & 1, will result in 0 and 1&1 will result in 1.

2. Bitwise OR (|): It is same as logical OR operator it combines two integer operands by performing logical OR operation on them. It only works on bits.If and only if the both operands are 0 then it will return 0 as result.If any of the operand is 1 or both operands are 1, it will return 1 as result.
For example: 0 & 1, will result in 1 and 0&0 will result in 0.

3. Bitwise XOR (^): It is same as logical XOR operator it combines two integer operands by performing logical XOR operation on them. It results in 1 if one of the operand is  1 and outputs 0 if both the expressions are 0  and both are 1. 
Example: 1 ^ 1, will result in 0 and 0^0 will result in 0.but 0 ^1 or 1^0 is always 1.

The table below illustrate the result of excepted inputs of operands.
result of Bitwise AND ,OR,XOR Operators

4. Bitwise One's Complement (~): We have already learnt about the 1's complement in binary, If you have not check that post click here. It is used to store negative numbers in bits. It is an unary operator . It inverts each bit , 1 becomes 0 and 0 becomes 1 i.e,1->0 and 0->1.
It is used to:-
  • encrypt the contents of file which can be later decrypted.
  • To store negative numbers in computer that supports the 1's complement method to store negative numbers.
Example: if a=1001, and we perform ~(a), the the number will be stores as negative number an it's value will be 0110.

5. Bitwise Shift left(<<): This operator is used to shift the bits of a number to left . Basic working of  this operator is ,
  • it will first convert the value of the operand on the left side of operator to binary number,
  • then it will check the right operand , which tells the number of bits to be shifted like 3 , it will shift bits to left 3 times.
  • then it will print the result.
Syntax:
operand1<<operand2
Now, understand the exact working of this operator using an example.
Example: a<<3, where a=0000 0101 0000 0011

On preforming a<<3, as the right operand is 3 so the bits will be shifted by 3 bits and first three bits will be lost, if the right operand is 2 then shift will happen with 2 bits.
Here, first table is the value of a before shift left and second table is after shift.Lets understand it in below,
  1. The left most 3 bit which are 000 are shifted and lost from the 16 bit of number a.
  2. The bits 0010100000011 which comes after the lost bits are shifted to left , and arrows is pointing to the shift of bits.
  3. Now, as all bits are shifted by 3 bits to left the last 3 bits becomes empty.So ,we have to fill the last bits with 3 zeros as 000.
Before shift the value of  a was 0000010100000011, after shifting left by or after a<<3 , a becomes 0010100000011000.

6. Bitwise Shift Right(>>): This operator is used to shift the bits of a number to right . Basic working of  this operator is ,
  • it will first convert the value of the operand on the right side of operator to binary number,
  • then it will check the right operand , which tells the number of bits to be shifted like 3 , it will shift bits to right 3 times.
  • then it will print the result.
Syntax:
operand1>>operand2
Now, understand the exact working of this operator using an example.
Example: a>>3, where a=0000 0101 0000 0011

On preforming a<<3, as the right operand is 3 so the bits will be shifted by 3 bit and first three bits will be lost, if the right operand is 2 then shift will happen with 2 bits.

Here, above line is the value of a before shift right and second table is after shift.Lets understand it in below,
  1. The right most 3 bit which are 011 are shifted and lost from the 16 bit of number a.
  2. The bits 0000010100000 which before the lost bits are shifted to right , and arrows is pointing to the shift of bits.
  3. Now, as all bits are shifted by 3 bits to right ,and the first 3 bits becomes empty so we have to fill the first 3 bits with 3 zeros as 000.
Before shift the value of  a was 0000010100000011, after shifting left by or after a<<3 , a becomes 0000000010100000.

7. Bitwise Shift Right with Zero Fill(>>>): Operator is similar to bitwise right shift(>>) operator with the exception that it always shifts 0's in to higher order bits of the result regardless of the sign of the left-hand operand. Left-hand operand may be a negative number having sign but as 1.
Syntax:
operand1>>>operand2
Now, understand the exact working of this operator using an example.
Example: a>>3, where a=1000 0101 0000 0011,a negative number having 1 as sign bit.

On preforming a<<3, as the right operand is 3 so the bits will be shifted by 3 bit and first three bits will be lost, if the right operand is 2 then shift will happen with 2 bits.


Here, above line is the value of a before shift right and second table is after shift.Lets understand it in below,
  1. The right most 3 bit which are 011 are shifted and lost from the 16 bit of number a.
  2. The bits 1000010100000 which before the lost bits are shifted to right , and arrows is pointing to the shift of bits.
  3. Now, as all bits are shifted by 3 bits to right ,and the first 3 bits becomes empty so we have to fill the first 3 bits with 3 zeros as 000.
Before shift the value of  a was 1000010100000011, after shifting left by or after a<<3 , a becomes 0001000010100000.

Order of evaluation of Bitwise operators

Order of evaluation means the precedence of bitwise operator that which operator is evaluated first.
  1. Bitwise 1's complement operator is evaluate first,
  2. Then, the Bitwise Left Shift Operator,
  3. now, the Shift Right operator
  4. And then, Shift Right Zero Fill operator
  5. And at last ,the bitwise AND,OR and XOR operators.

Example:

This example will illustrate the use of all bitwise operators:

Bitwise Operators in Java with example

Here, if the number is of int type then  result will be in int ,if the type of operand is long the result will be in long.

Output:

Bitwise Operators in Java with example
Note: The binary representation of x is 00010100 and y  is 00000101. The negative numbers are stored in 2's complement form.z is negative number when we apply 1's complement it becomes positive number.

Try it Yourself! Happy Coding!

Comments