bithifting如何在Java中工作?

ben*_*ham 59 java bitwise-operators

我有这样的声明:

假设byte的位值x是00101011.结果是x>>2什么?

我该如何编程呢?有人能解释我在做什么吗?

fin*_*nnw 95

首先,你不能byte在java中移位,你只能移动一个int或一个long.因此,byte将首先进行促销,例如

00101011 - > 00000000000000000000000000101011

要么

11010100 - > 11111111111111111111111111010100

现在,x >> N表示(如果您将其视为二进制数字字符串):

  • 最右边的N位被丢弃
  • 根据需要复制最左边的位以将结果填充到原始大小(32或64位),例如

00000000000000000000000000101011 >> 2 - > 00000000000000000000000000001010

11111111111111111111111111010100 >> 2 - > 11111111111111111111111111110101


Cha*_*har 58

移位运算符

二进制32个比特00101011

00000000 00000000 00000000 00101011,结果是:

  00000000 00000000 00000000 00101011   >> 2(times)
 \\                                 \\
  00000000 00000000 00000000 00001010
Run Code Online (Sandbox Code Playgroud)

将位43向右移动距离2; 填充左侧最高(符号)位.

结果为00001010,十进制值为10.

00001010
    8+2 = 10
Run Code Online (Sandbox Code Playgroud)


jjn*_*guy 16

向右移2位时,丢弃2个最低有效位.所以:

x = 00101011

x >> 2

// now (notice the 2 new 0's on the left of the byte)
x = 00001010
Run Code Online (Sandbox Code Playgroud)

这与将int除以2,2次基本相同.

在Java中

byte b = (byte) 16;
b = b >> 2;
// prints 4
System.out.println(b);
Run Code Online (Sandbox Code Playgroud)

  • 每个右移除以2,因此两个右移除以4. (5认同)
  • 究竟.或者,在数学中:n >> m - > n /(2 ^ m) (4认同)
  • @delnan,是的,但看起来很吓人. (2认同)

Per*_*ega 10

这些例子涵盖了应用于正数和负数的三种类型的转变:

// Signed left shift on 626348975
00100101010101010101001110101111 is   626348975
01001010101010101010011101011110 is  1252697950 after << 1
10010101010101010100111010111100 is -1789571396 after << 2
00101010101010101001110101111000 is   715824504 after << 3

// Signed left shift on -552270512
11011111000101010000010101010000 is  -552270512
10111110001010100000101010100000 is -1104541024 after << 1
01111100010101000001010101000000 is  2085885248 after << 2
11111000101010000010101010000000 is  -123196800 after << 3


// Signed right shift on 626348975
00100101010101010101001110101111 is   626348975
00010010101010101010100111010111 is   313174487 after >> 1
00001001010101010101010011101011 is   156587243 after >> 2
00000100101010101010101001110101 is    78293621 after >> 3

// Signed right shift on -552270512
11011111000101010000010101010000 is  -552270512
11101111100010101000001010101000 is  -276135256 after >> 1
11110111110001010100000101010100 is  -138067628 after >> 2
11111011111000101010000010101010 is   -69033814 after >> 3


// Unsigned right shift on 626348975
00100101010101010101001110101111 is   626348975
00010010101010101010100111010111 is   313174487 after >>> 1
00001001010101010101010011101011 is   156587243 after >>> 2
00000100101010101010101001110101 is    78293621 after >>> 3

// Unsigned right shift on -552270512
11011111000101010000010101010000 is  -552270512
01101111100010101000001010101000 is  1871348392 after >>> 1
00110111110001010100000101010100 is   935674196 after >>> 2
00011011111000101010000010101010 is   467837098 after >>> 3
Run Code Online (Sandbox Code Playgroud)


Mik*_*els 5

>>是算术右移运算符.第一个操作数中的所有位都移位了第二个操作数指示的位数.结果中最左边的位设置为与原始数字中最左边的位相同的值.(这是负数仍然是负数.)

这是您的具体情况:

00101011
  001010 <-- Shifted twice to the right (rightmost bits dropped)
00001010 <-- Leftmost bits filled with 0s (to match leftmost bit in original number)
Run Code Online (Sandbox Code Playgroud)