Java Shift Operator ( << >> >>>)
Write a demo to test Shift operators:
/**
* Test Shift Operators:
* signedLeftShift (<<)
* signedRightShift (>>)
* unsignedRightShift (>>>)
*/
public class TestShiftOperators {
public static void main(String[] args) {
TestShiftOperators t = new TestShiftOperators();
t.testShift();
}
public void testShift(){
testSignedLeftShiftWrapper();
testSignedRightShiftWrapper();
testUnSignedRightShiftWrapper();
}
public void testSignedLeftShiftWrapper(){
System.out.println("***TestSignedLeftShift*************************");
// testSignedLeftShift(17, 1);
testSignedLeftShift(16, 1);
testSignedLeftShift(16, 2);
testSignedLeftShift(16, 3);
testSignedLeftShift(16, 30);
testSignedLeftShift(-16, 1);
testSignedLeftShift(-16, 2);
testSignedLeftShift(-16, 3);
testSignedLeftShift(-16, 30);
System.out.println();
}
public void testSignedLeftShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a<<b);
System.out.println(getBinaryString(ai) +"\t<< "+b+"\t= " + getBinaryString2(result));
}
public void testSignedRightShiftWrapper(){
System.out.println("***TestSignedRightShift*************************");
testSignedRightShift(16, 1);
testSignedRightShift(16, 2);
testSignedRightShift(16, 3);
testSignedRightShift(16, 30);
testSignedRightShift(-16, 1);
testSignedRightShift(-16, 2);
testSignedRightShift(-16, 3);
testSignedRightShift(-16, 30);
System.out.println();
}
public void testSignedRightShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a>>b);
System.out.println(getBinaryString(ai) +"\t>> "+b+"\t= " + getBinaryString2(result));
}
public void testUnSignedRightShiftWrapper(){
System.out.println("***TestUnSignedRightShift*************************");
testUnSignedRightShift(16, 1);
testUnSignedRightShift(16, 2);
testUnSignedRightShift(16, 3);
testUnSignedRightShift(16, 30);
testUnSignedRightShift(-16, 1);
testUnSignedRightShift(-16, 2);
testUnSignedRightShift(-16, 3);
testUnSignedRightShift(-16, 30);
System.out.println();
}
public void testUnSignedRightShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a>>>b);
System.out.println(getBinaryString(ai) +"\t>>> "+b+"\t= " + getBinaryString2(result));
}
public String getBinaryString(Integer i){
StringBuilder result = new StringBuilder(i.toBinaryString(i));
while(result.length() < 32){
result.insert(0, 0);
}
return i + "\t[" + result.toString() + "]";
}
public String getBinaryString2(Integer i){
StringBuilder result = new StringBuilder(i.toBinaryString(i));
while(result.length() < 32){
result.insert(0, 0);
}
if(i>10000000)
return i + "\t\t[" + result.toString() + "]";
return i + "\t\t\t[" + result.toString() + "]";
}
}
* Test Shift Operators:
* signedLeftShift (<<)
* signedRightShift (>>)
* unsignedRightShift (>>>)
*/
public class TestShiftOperators {
public static void main(String[] args) {
TestShiftOperators t = new TestShiftOperators();
t.testShift();
}
public void testShift(){
testSignedLeftShiftWrapper();
testSignedRightShiftWrapper();
testUnSignedRightShiftWrapper();
}
public void testSignedLeftShiftWrapper(){
System.out.println("***TestSignedLeftShift*************************");
// testSignedLeftShift(17, 1);
testSignedLeftShift(16, 1);
testSignedLeftShift(16, 2);
testSignedLeftShift(16, 3);
testSignedLeftShift(16, 30);
testSignedLeftShift(-16, 1);
testSignedLeftShift(-16, 2);
testSignedLeftShift(-16, 3);
testSignedLeftShift(-16, 30);
System.out.println();
}
public void testSignedLeftShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a<<b);
System.out.println(getBinaryString(ai) +"\t<< "+b+"\t= " + getBinaryString2(result));
}
public void testSignedRightShiftWrapper(){
System.out.println("***TestSignedRightShift*************************");
testSignedRightShift(16, 1);
testSignedRightShift(16, 2);
testSignedRightShift(16, 3);
testSignedRightShift(16, 30);
testSignedRightShift(-16, 1);
testSignedRightShift(-16, 2);
testSignedRightShift(-16, 3);
testSignedRightShift(-16, 30);
System.out.println();
}
public void testSignedRightShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a>>b);
System.out.println(getBinaryString(ai) +"\t>> "+b+"\t= " + getBinaryString2(result));
}
public void testUnSignedRightShiftWrapper(){
System.out.println("***TestUnSignedRightShift*************************");
testUnSignedRightShift(16, 1);
testUnSignedRightShift(16, 2);
testUnSignedRightShift(16, 3);
testUnSignedRightShift(16, 30);
testUnSignedRightShift(-16, 1);
testUnSignedRightShift(-16, 2);
testUnSignedRightShift(-16, 3);
testUnSignedRightShift(-16, 30);
System.out.println();
}
public void testUnSignedRightShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a>>>b);
System.out.println(getBinaryString(ai) +"\t>>> "+b+"\t= " + getBinaryString2(result));
}
public String getBinaryString(Integer i){
StringBuilder result = new StringBuilder(i.toBinaryString(i));
while(result.length() < 32){
result.insert(0, 0);
}
return i + "\t[" + result.toString() + "]";
}
public String getBinaryString2(Integer i){
StringBuilder result = new StringBuilder(i.toBinaryString(i));
while(result.length() < 32){
result.insert(0, 0);
}
if(i>10000000)
return i + "\t\t[" + result.toString() + "]";
return i + "\t\t\t[" + result.toString() + "]";
}
}
Output:
***TestSignedLeftShift*************************
16 [00000000000000000000000000010000] << 1 = 32 [00000000000000000000000000100000]
16 [00000000000000000000000000010000] << 2 = 64 [00000000000000000000000001000000]
16 [00000000000000000000000000010000] << 3 = 128 [00000000000000000000000010000000]
16 [00000000000000000000000000010000] << 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] << 1 = -32 [11111111111111111111111111100000]
-16 [11111111111111111111111111110000] << 2 = -64 [11111111111111111111111111000000]
-16 [11111111111111111111111111110000] << 3 = -128 [11111111111111111111111110000000]
-16 [11111111111111111111111111110000] << 30 = 0 [00000000000000000000000000000000]
***TestSignedRightShift*************************
16 [00000000000000000000000000010000] >> 1 = 8 [00000000000000000000000000001000]
16 [00000000000000000000000000010000] >> 2 = 4 [00000000000000000000000000000100]
16 [00000000000000000000000000010000] >> 3 = 2 [00000000000000000000000000000010]
16 [00000000000000000000000000010000] >> 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] >> 1 = -8 [11111111111111111111111111111000]
-16 [11111111111111111111111111110000] >> 2 = -4 [11111111111111111111111111111100]
-16 [11111111111111111111111111110000] >> 3 = -2 [11111111111111111111111111111110]
-16 [11111111111111111111111111110000] >> 30 = -1 [11111111111111111111111111111111]
***TestUnSignedRightShift*************************
16 [00000000000000000000000000010000] >>> 1 = 8 [00000000000000000000000000001000]
16 [00000000000000000000000000010000] >>> 2 = 4 [00000000000000000000000000000100]
16 [00000000000000000000000000010000] >>> 3 = 2 [00000000000000000000000000000010]
16 [00000000000000000000000000010000] >>> 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] >>> 1 = 2147483640 [01111111111111111111111111111000]
-16 [11111111111111111111111111110000] >>> 2 = 1073741820 [00111111111111111111111111111100]
-16 [11111111111111111111111111110000] >>> 3 = 536870910 [00011111111111111111111111111110]
-16 [11111111111111111111111111110000] >>> 30 = 3 [00000000000000000000000000000011]
16 [00000000000000000000000000010000] << 1 = 32 [00000000000000000000000000100000]
16 [00000000000000000000000000010000] << 2 = 64 [00000000000000000000000001000000]
16 [00000000000000000000000000010000] << 3 = 128 [00000000000000000000000010000000]
16 [00000000000000000000000000010000] << 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] << 1 = -32 [11111111111111111111111111100000]
-16 [11111111111111111111111111110000] << 2 = -64 [11111111111111111111111111000000]
-16 [11111111111111111111111111110000] << 3 = -128 [11111111111111111111111110000000]
-16 [11111111111111111111111111110000] << 30 = 0 [00000000000000000000000000000000]
***TestSignedRightShift*************************
16 [00000000000000000000000000010000] >> 1 = 8 [00000000000000000000000000001000]
16 [00000000000000000000000000010000] >> 2 = 4 [00000000000000000000000000000100]
16 [00000000000000000000000000010000] >> 3 = 2 [00000000000000000000000000000010]
16 [00000000000000000000000000010000] >> 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] >> 1 = -8 [11111111111111111111111111111000]
-16 [11111111111111111111111111110000] >> 2 = -4 [11111111111111111111111111111100]
-16 [11111111111111111111111111110000] >> 3 = -2 [11111111111111111111111111111110]
-16 [11111111111111111111111111110000] >> 30 = -1 [11111111111111111111111111111111]
***TestUnSignedRightShift*************************
16 [00000000000000000000000000010000] >>> 1 = 8 [00000000000000000000000000001000]
16 [00000000000000000000000000010000] >>> 2 = 4 [00000000000000000000000000000100]
16 [00000000000000000000000000010000] >>> 3 = 2 [00000000000000000000000000000010]
16 [00000000000000000000000000010000] >>> 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] >>> 1 = 2147483640 [01111111111111111111111111111000]
-16 [11111111111111111111111111110000] >>> 2 = 1073741820 [00111111111111111111111111111100]
-16 [11111111111111111111111111110000] >>> 3 = 536870910 [00011111111111111111111111111110]
-16 [11111111111111111111111111110000] >>> 30 = 3 [00000000000000000000000000000011]