`

java中的位移运算测试

    博客分类:
  • java
阅读更多

 

参考链接:https://blog.csdn.net/sxhlovehmm/article/details/44244195

                  https://blog.csdn.net/qq_35114086/article/details/70173329

 

1、左移运算 <<

丢弃最高位(符号位同样丢弃),0补最低位

 

正数左位移:

/**
	 * 左位移测试。 丢弃最高位(符号位同样丢弃),0补最低位 如果移动的位数超过了该类型的最大位数,那么编译器会对移动的位数取模。
	 * 如对int型移动33位,实际上只移动了33%32=1位。
	 */
	@Test
	public void test3() {
		int temp = 33;
		String as = "01000000101001010101001101100101";
		// 使用Integer.parseInt方法,将as转整型时,符号位不能为1,如果要表示负数,在as左测最高位添加“-”,而符号位右侧的数值位则表示负数的绝对值原码。
		int an = Integer.parseInt(as, 2);
		System.out.println("原值:" + an + "-->" + Integer.toBinaryString(an));
		System.out.println("<<" + temp);
		int bn = an << temp;
		System.out.println("运算后:" + bn + "-->" + Integer.toBinaryString(bn));
	}

	/**
	 * temp=1,打印结果:
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101 
	 * <<1
	 * 运算后:-2125814070-->1000 0001 0100 1010 1010 0110 1100 1010
	 * 
	 * temp=2,打印结果:
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101  
	 * <<2
	 * 运算后:43339156-->10 1001 0101 0100 1101 1001 0100
	 * 
	 * temp=31,打印结果:
	 * 
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101 
	 * <<31
	 * 运算后:-2147483648-->1000 0000 0000 0000 0000 0000 0000 0000
	 * 
	 * temp=32,打印结果:
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * <<32
	 * 运算后:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * 
	 * temp=33,打印结果:
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * <<33
	 * 运算后:-2125814070-->1000 0001 0100 1010 1010 0110 1100 1010
	 * 
	 */

 特殊值的二进制表示:

/**
	 * 特殊值的二进制表示
	 */
	@Test
	public void test4() {
		System.out.println(Integer.toBinaryString(-2147483648));
		System.out.println(Math.abs(-2147483648));
		System.out.println(Integer.toBinaryString(Math.abs(-2147483648)));
		System.out.println(Integer.toBinaryString(-2147483647));
		System.out.println(Integer.toBinaryString(2147483647));
		System.out.println(Integer.toBinaryString(0));
		System.out.println(Integer.toBinaryString(-1));
	}

	/**
	 * 打印结果:
	 * 10000000000000000000000000000000
	 * -2147483648
	 * 10000000000000000000000000000000
	 * 10000000000000000000000000000001
	 * 1111111111111111111111111111111
	 * 0
	 * 11111111111111111111111111111111
	 */

 

负数左位移:

 

/**
	 * 负数左移
	 */
	@Test
	public void test6() {
		int temp = 33;
		String as = "-1000000101001010101001101100101";
		// 使用Integer.parseInt方法,将as转整型时,符号位不能为1,如果要表示负数,在as左测最高位添加“-”,数值位表示负数的绝对值。
		int an = Integer.parseInt(as, 2);
		System.out.println("原值:" + an + "-->" + Integer.toBinaryString(an));
		System.out.println("<<" + temp);
		int bn = an << temp;
		System.out.println("运算后:" + bn + "-->" + Integer.toBinaryString(bn));
	}

	/**
	 * temp=1,打印结果:
	 * 
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011 
	 * <<1
	 * 运算后:2125814070-->111 1110 1011 0101 0101 1001 0011 0110
	 * 
	 * 
	 * temp=2,打印结果:
	 * 
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * <<2
	 * 运算后:-43339156-->1111 1101 0110 1010 1011 0010 0110 1100
	 * 
	 * 
	 * temp=31,打印结果:
	 * 
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * <<31
	 * 运算后:-2147483648-->1000 0000 0000 0000 0000 0000 0000 0000
	 * 
	 * 
	 * temp=32,打印结果:
	 * 
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * <<32
	 * 运算后:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * 
	 * 
	 * temp=33,打印结果:
	 * 
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * <<33
	 * 运算后:2125814070-->111 1110 1011 0101 0101 1001 0011 0110
	 * 
	 */

 

2、右移运算 >>

 

符号位不变,左边补上符号位 

 

 正数右位移:

/**
	 * 正数右位移
	 */
	@Test
	public void right1() {
		int temp = 33;
		String as = "01000000101001010101001101100101";
		int an = Integer.parseInt(as, 2);
		System.out.println("原值:" + an + "-->" + Integer.toBinaryString(an));
		System.out.println(">>" + temp);
		int bn = an >> temp;
		System.out.println("运算后:" + bn + "-->" + Integer.toBinaryString(bn));
	}
	
	/**
	 * temp=1,打印结果:
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * >>1
	 * 运算后:542288306-->10 0000 0101 0010 1010 1001 1011 0010
	 * 
	 * temp=2,打印结果:
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * >>2
	 * 运算后:271144153-->1 0000 0010 1001 0101 0100 1101 1001
	 * 
	 * temp=31,打印结果:
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * >>31
	 * 运算后:0-->0
	 * 
	 * temp=32,打印结果:
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * >>32
	 * 运算后:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * 
	 * temp=33,打印结果:
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * >>33
	 * 运算后:542288306-->10 0000 0101 0010 1010 1001 1011 0010 
	 * 
	 */

 

负数右位移:

/**
	 * 负数右位移
	 */
	@Test
	public void right2() {
		int temp = 33;
		String as = "-01000000101001010101001101100101";
		int an = Integer.parseInt(as, 2);
		System.out.println("原值:" + an + "-->" + Integer.toBinaryString(an));
		System.out.println(">>" + temp);
		int bn = an >> temp;
		System.out.println("运算后:" + bn + "-->" + Integer.toBinaryString(bn));
	}
	

	/**
	 * temp=1,打印结果:
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * >>1
	 * 运算后:-542288307-->1101 1111 1010 1101 0101 0110 0100 1101
	 * 
	 * temp=2,打印结果:
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * >>2
	 * 运算后:-271144154-->1110 1111 1101 0110 1010 1011 0010 0110
	 * 
	 * temp=31,打印结果:
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * >>31
	 * 运算后:-1-->1111 1111 1111 1111 1111 1111 1111 1111
	 * 
	 * temp=32,打印结果:
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * >>32
	 * 运算后:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * 
	 * temp=33,打印结果:
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * >>33
	 * 运算后:-542288307-->1101 1111 1010 1101 0101 0110 0100 1101
	 */
	

 

 3、无符号右移

忽略了符号位扩展,0补最高位 

 

正数右移:

 

/**
	 * 无符号右移:正数
	 */
	@Test
	public void right3() {
		int temp = 33;
		String as = "1000000101001010101001101100101";//1100 0000 1010 0101 0101 0011 0110 0101
		int an = Integer.parseInt(as, 2);
		System.out.println("原值:" + an + "-->" + Integer.toBinaryString(an));
		System.out.println(">>>" + temp);
		int bn = an >>> temp;
		System.out.println("运算后:" + bn + "-->" + Integer.toBinaryString(bn));
	}
	
	/**
	 * temp=1,打印结果: 
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * >>>1
	 * 运算后:542288306-->10 0000 0101 0010 1010 1001 1011 0010
	 * 
	 * temp=2,打印结果: 
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * >>>2
	 * 运算后:271144153-->1 0000 0010 1001 0101 0100 1101 1001
	 * 
	 * temp=31,打印结果: 
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * >>>31
	 * 运算后:0-->0
	 * 
	 * temp=32,打印结果: 
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * >>>32
	 * 运算后:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * 
	 * temp=32,打印结果: 
	 * 原值:1084576613-->100 0000 1010 0101 0101 0011 0110 0101
	 * >>>33
	 * 运算后:542288306-->10 0000 0101 0010 1010 1001 1011 0010
	 */

 

 

负数右移:

/**
	 * 无符号右移:负数
	 */
	@Test
	public void right3() {
		int temp = 32;
		String as = "-01000000101001010101001101100101";
		int an = Integer.parseInt(as, 2);
		System.out.println("原值:" + an + "-->" + Integer.toBinaryString(an));
		System.out.println(">>" + temp);
		int bn = an >>> temp;
		System.out.println("运算后:" + bn + "-->" + Integer.toBinaryString(bn));
	}
	
	/**
	 * temp=1,打印结果: 
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * >>>1
	 * 运算后:1605195341-->101 1111 1010 1101 0101 0110 0100 1101
	 * 
	 * temp=2,打印结果: 
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * >>>2
	 * 运算后:802597670-->10 1111 1101 0110 1010 1011 0010 0110
	 * 
	 * temp=31,打印结果: 
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * >>>31
	 * 运算后:1-->1
	 * 
	 * temp=32,打印结果: 
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * >>>32
	 * 运算后:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * 
	 * temp=33,打印结果: 
	 * 原值:-1084576613-->1011 1111 0101 1010 1010 1100 1001 1011
	 * >>>33
	 * 运算后:1605195341-->101 1111 1010 1101 0101 0110 0100 1101
	 * 
	 */

 

 最后一张总结图



 

 

 

  • 大小: 42.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics