Java Shift Operator (signedLeftShift, signedRightShift, unsignedRightShift)

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() + "]";
}
}

 

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]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值