java的移位运算详解(举例说明)

it2022-05-08  7

1)java中无符号右移:>>>,下面是一个int型的负数,然后每次移动一位。

 

int k = -0x123;System.out.println(Integer.toBinaryString(k));int n = k>>>1;System.out.println(Integer.toBinaryString(n));n = n>>>1;System.out.println(Integer.toBinaryString(n));n = n>>>1; System.out.println(Integer.toBinaryString(n));。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

 

111111111111111111111110110111011111111111111111111111101101110111111111111111111111110110111111111111111111111111110110111111111111111111111111101101111111111111111111111110110111111111111111111111110111111111111111111111111101111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111000

2)下面是一个整型int的正数的无符号移位:因为正数的最前面一位是符号位为0,因此一个32位的正数一般都是从第一个非零开始表示,前面的零都省略表示出来。

 

int k = 0x123;System.out.println(Integer.toBinaryString(k));int n = k>>>1;System.out.println(Integer.toBinaryString(n));n = n>>>1;System.out.println(Integer.toBinaryString(n));n = n>>>1; System.out.println(Integer.toBinaryString(n));。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

 

100100011

10010001

1001000

100100

10010

1001

100

10

1

0

0

0

 

0

3)有符号右移“>>”,负数右移如下:每次移位都是从最右边移除一位,然后最前面添加一位“1”来填充,因此总的位数不变,依旧是32位。

int k = -0x123;System.out.println(Integer.toBinaryString(k));int n = k>>1;System.out.println(Integer.toBinaryString(n));n = n>>1;System.out.println(Integer.toBinaryString(n));n = n>>1; System.out.println(Integer.toBinaryString(n));。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

111111111111111111111110110111011111111111111111111111110110111011111111111111111111111110110111111111111111111111111111110110111111111111111111111111111110110111111111111111111111111111110110111111111111111111111111111110111111111111111111111111111111110111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

4)有符号右移“>>”,正数右移如下:每次移位都是从最右边移除一位,然后最前面添加一位“0”来填充,因此总的位数不变,依旧是32位,但是只显示首位为非零的开始。

int k = 0x123;System.out.println(Integer.toBinaryString(k));int n = k>>1;System.out.println(Integer.toBinaryString(n));n = n>>1;System.out.println(Integer.toBinaryString(n));n = n>>1; System.out.println(Integer.toBinaryString(n));n = n>>1; System.out.println(Integer.toBinaryString(n));n = n>>1; System.out.println(Integer.toBinaryString(n));。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

100100011100100011001000100100100101001100101000

 5)左移,只有有符号数左移。正数左移。

int k = 0x123;System.out.println(Integer.toBinaryString(k));int n = k<<1;System.out.println(Integer.toBinaryString(n));n = n<<1;System.out.println(Integer.toBinaryString(n));n = n<<1; System.out.println(Integer.toBinaryString(n));n = n<<1; System.out.println(Integer.toBinaryString(n));。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

1001000111001000110100100011001001000110001001000110000100100011000001001000110000001001000110000000100100011000000001001000110000000001001000110000000000100100011000000000001001000110000000000001001000110000000000000100100011000000000000001001000110000000000000001001000110000000000000000100100011000000000000000001001000110000000000000000001001000110000000000000000000100100011000000000000000000001001000110000000000000000000001001000110000000000000000000000100100011000000000000000000000001000110000000000000000000000001000110000000000000000000000000100011000000000000000000000000001100000000000000000000000000011000000000000000000000000000011000000000000000000000000000001100000000000000000000000000000010000000000000000000000000000000000

6)左移,只有有符号数左移。负数左移。

int k = -0x123;System.out.println(Integer.toBinaryString(k));int n = k<<1;System.out.println(Integer.toBinaryString(n));n = n<<1;System.out.println(Integer.toBinaryString(n));n = n<<1; System.out.println(Integer.toBinaryString(n));n = n<<1; System.out.println(Integer.toBinaryString(n));。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

1111111111111111111111101101110111111111111111111111110110111010111111111111111111111011011101001111111111111111111101101110100011111111111111111110110111010000111111111111111111011011101000001111111111111111101101110100000011111111111111110110111010000000111111111111111011011101000000001111111111111101101110100000000011111111111110110111010000000000111111111111011011101000000000001111111111101101110100000000000011111111110110111010000000000000111111111011011101000000000000001111111101101110100000000000000011111110110111010000000000000000111111011011101000000000000000001111101101110100000000000000000011110110111010000000000000000000111011011101000000000000000000001101101110100000000000000000000010110111010000000000000000000000110111010000000000000000000000011011101000000000000000000000000101110100000000000000000000000001110100000000000000000000000000111010000000000000000000000000001101000000000000000000000000000010100000000000000000000000000000100000000000000000000000000000010000000000000000000000000000000000

 

 

 

 

转载于:https://www.cnblogs.com/igoodful/p/8722158.html


最新回复(0)