移位在 Java 中是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3312853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:59:21  来源:igfitidea点击:

How does bitshifting work in Java?

javabitwise-operators

提问by bentham

I have this statement:

我有这样的声明:

Assume the bit value of byte xis 00101011. what is the result of x>>2?

假设字节的位值为x00101011。结果是x>>2什么?

How can I program it and can someone explain me what is doing?

我该如何编程,有人可以解释我在做什么吗?

采纳答案by finnw

Firstly, you can notshift a bytein java, you can only shift an intor a long. So the bytewill undergo promotion first, e.g.

首先,你不能byte在java中移动 a ,你只能移动 anint或 a long。所以byte将首先进行促销,例如

00101011-> 00000000000000000000000000101011

00101011-> 00000000000000000000000000101011

or

或者

11010100-> 11111111111111111111111111010100

11010100-> 11111111111111111111111111010100

Now, x >> Nmeans (if you view it as a string of binary digits):

现在,x >> N意味着(如果您将其视为一串二进制数字):

  • The rightmost N bits are discarded
  • The leftmost bit is replicated as many times as necessary to pad the result to the original size (32 or 64 bits), e.g.
  • 最右边的 N 位被丢弃
  • 最左边的位根据需要复制多次以将结果填充到原始大小(32 或 64 位),例如

00000000000000000000000000101011 >> 2-> 00000000000000000000000000001010

00000000000000000000000000101011 >> 2-> 00000000000000000000000000001010

11111111111111111111111111010100 >> 2-> 11111111111111111111111111110101

11111111111111111111111111010100 >> 2-> 11111111111111111111111111110101

回答by jjnguy

When you shift right 2 bits you drop the 2 least significant bits. So:

当您右移 2 位时,您会丢弃 2 个最低有效位。所以:

x = 00101011

x >> 2

// now (notice the 2 new 0's on the left of the byte)
x = 00001010

This is essentially the same thing as dividing an int by 2, 2 times.

这与将 int 除以 2、2 次本质上是一样的。

In Java

在 Java 中

byte b = (byte) 16;
b = b >> 2;
// prints 4
System.out.println(b);

回答by Mark Byers

You can't write binary literals like 00101011in Java so you can write it in hexadecimal instead:

你不能像00101011在 Java 中那样编写二进制文字,所以你可以用十六进制编写它:

byte x = 0x2b;

To calculate the result of x >> 2you can then just write exactly that and print the result.

要计算x >> 2你的结果,你可以准确地写出来并打印结果。

System.out.println(x >> 2);

回答by Mike Daniels

>>is the Arithmetic Right Shift operator. All of the bits in the first operand are shifted the number of places indicated by the second operand. The leftmost bits in the result are set to the same value as the leftmost bit in the original number. (This is so that negative numbers remain negative.)

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

Here's your specific case:

这是您的具体情况:

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

回答by Jacob Tomaw

byte x = 51; //00101011
byte y = (byte) (x >> 2); //00001010 aka Base(10) 10

回答by stacker

public class Shift {
 public static void main(String[] args) {
  Byte b = Byte.parseByte("00101011",2);
  System.out.println(b);
  byte val = b.byteValue();
  Byte shifted = new Byte((byte) (val >> 2));
  System.out.println(shifted);

  // often overloked  are the methods of Integer

  int i = Integer.parseInt("00101011",2);
  System.out.println( Integer.toBinaryString(i));
  i >>= 2;
  System.out.println( Integer.toBinaryString(i));
 }
}

Output:

输出:

43
10
101011
1010

回答by Joni

You can use e.g. this API if you would like to see bitString presentation of your numbers. Uncommons Math

如果您想查看您的数字的 bitString 表示,您可以使用例如这个 API。不寻常的数学

Example (in jruby)

示例(在 jruby 中)

bitString = org.uncommons.maths.binary.BitString.new(java.math.BigInteger.new("12").toString(2))
bitString.setBit(1, true)
bitString.toNumber => 14

edit: Changed api link and add a little example

编辑:更改了 api 链接并添加了一个小示例

回答by Chandra Sekhar

Shift Operators

移位运算符

The binary 32 bits for 00101011is

用于二进制32个比特00101011就是

00000000 00000000 00000000 00101011, and the result is:

00000000 00000000 00000000 00101011,结果是:

  00000000 00000000 00000000 00101011   >> 2(times)
 \                                 \
  00000000 00000000 00000000 00001010

Shifts the bits of 43 to right by distance 2; fills with highest(sign) bit on the left side.

将 43 位右移距离 2;在左侧填充最高(符号)位。

Result is 00001010 with decimal value 10.

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

00001010
    8+2 = 10

回答by Percy Vega

These examples cover the three types of shifts applied to both a positive and a negative number:

这些示例涵盖了应用于正数和负数的三种类型的移位:

// 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

回答by Gihan Fernando

00101011 = 43 in decimal

00101011 = 43 十进制

class test {    
    public static void main(String[] args){
       int a= 43;       
       String b= Integer.toBinaryString(a >> 2);        
       System.out.println(b);
    }   
}

Output:

输出:

101011 becomes 1010

101011 变成 1010