Tuesday, September 24, 2013

Bitwise Shift operators in java with code

Java provides bit wise shift operation only on integral types (int, long, possibly short and byte or char).

There are 3 types of bit wise shift operators.

1) >> (Signed right shift operator)

It will shift the bit pattern of the first argument to the right by the number of positions mentioned in the second argument.  It fills in positions on the left with zeros. It keeps the sign of the argument unchanged.

           
Example
Meaning In Binary format
Result
2 >>1
10  should be shifted to right by 1 position (keep the sign unchanged)
1
-2 >> 1
10  should be shifted to right by 1 position (keep the sign unchanged) 
-1


Byte representation (8 bytes)
2 >> 1
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
1

-2 >> 1
-1
1
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
Red color to indicate sign


This operator gives the value which is equal to the integer division by 2^n where n is the number of positions.

2) << (Signed left shift operator)

It will shift the bit pattern of the first argument to the left by the number of positions mentioned in the second argument. It fills in positions on the right with zeros.
 It keeps the sign of the argument unchanged.

Example
Meaning In Binary format
Result
2 << 1
10  should be shifted to left by 1 position (keep the sign unchanged)
4
-2 << 1
10  should be shifted to left by 1 position (keep the sign unchanged) 
- 4


Byte representation (8 bytes)
2 << 1
4
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
0

-2 << 1
-4
1
0
0
0
0
0
1
0
1
0
0
0
0
1
0
0
Red color to indicate sign


This operator gives the value which is equal to the integer multiplication by 2^n where n is the number of positions.

3)  >>> (Unsigned right shift operator)
It shifts bit pattern of the first argument to the right by the number of positions mentioned in the second argument without caring what the original sign was. It fills in positions on the left with zeros.

Byte representation (8 bytes)
2 >>> 1
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
1

-2 >>> 1
65
1
0
0
0
0
0
1
0
0
1
0
0
0
0
0
1
Red color to indicate sign

This operator works differently based on the sign of the first argument. It does not keep the sign of the argument. (Which means it may change) .In case of Positive numbers it works just like a signed right shift operator. For negative numbers this operator produces a positive value.

Java code to test the shift operators is below. (Please note that the code below uses 32 bytes since integer is used)

public class BitOperatorsExample {
public static void main(String[] args) {
int i = 2;
int negi = -2;
int leftShiftPositive = i << 1;   // 20
int leftShiftNegative = negi << 1;  // -4
int rightShiftPositive = i >> 1;  // 2
int rightShiftNegative = negi >> 1; // -1
int UnsignedRightShiftPositive = i >>> 1; //1
int UnsignedRightShiftNegative =  negi >>> 1; //2147483647
System.out.println(Math.pow(2, 31));
System.out.println("leftShiftPositive :"+ leftShiftPositive);
System.out.println("leftShiftNegative :"+ leftShiftNegative);
System.out.println("rightShiftPositive :"+ rightShiftPositive);
System.out.println("rightShiftNegative :"+ rightShiftNegative);
System.out.println("UnsignedRightShiftPositive :"+ UnsignedRightShiftPositive);
System.out.println("UnsignedRightShiftNegative :"+ UnsignedRightShiftNegative);
}
}

No comments:

Post a Comment

Resolve "Target runtime Apache Tomcat v6.0 is not defined." error

Go to window ->preferences ->Server->Runtime environments . add server Apache Tomcat v6.0 Mention the directory where apache-toma...