BITWISE / SHIFT OPERATOR
Two statements are checked for conditions using the bitwise operator. These three types of bitwise operations are bitwise AND (&), bitwise exclusive OR (), and bitwise inclusive OR (|).
Example: (A<B & A<C), (A>B | A++ < C)
The shift operator is used for shifting the individual bit left or right. This is essential use in Java: right shift (>>), Left shift (<<), unsigned right shift (>>>)
Example: (A<<B), (A>>B), (A>>>B)
| Operator | Description | Example |
| << | Left Shift | |
| >> | Signed Right Shift | |
| >>> | Unsigned Right Shift | |
| & | AND | |
| | | OR | |
| ^ | Bitwise XOR | |
| ~ | Bitwise Complement |
1. Left Shift Operator (<<)
The left shift operator (<<) shifts the bits of a number to the left by a specified number of positions. During this operation, zeros are inserted on the right side of the number.
2. Right Shift Operator (>>)
The right shift operator is used to move the bits of a number toward the right side (i.e., a binary number) by a specified number of positions. When the right shift operator is implemented, the bits of the number are shifted to the right, and the leftmost positions are filled with the sign bit (0 for positive numbers and 1 for negative numbers). Example
import java.util.*;
class A {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
byte x=sc.nextByte();
int i;
i= x >> 2;
System.out.println("Original value of x: " + x);
System.out.println("i value after shifting " + i );
} }
Code Example
import java.util.*;
class A {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
byte x=sc.nextByte();
int i;
i= x << 2;
System.out.println("Original value of x: " + x);
System.out.println("i value after shifting " + i );
}
}
/* Explaintion:-
x << 2 shifts the bits of a two positions to the left, effectively multiplying it by 4.
The result is stored in i as an int, preserving the full value.
*/
RANREV INFOTECH