>> 在 Java 中有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3921145/
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
What does >> do in Java?
提问by William
Okay, I tried looking up what >>
, or shift means, but it's way over my head as this site explains it: http://www.janeg.ca/scjp/oper/shift.html
好的,我尝试查找>>
, 或 shift 的含义,但正如本网站所解释的那样,它超出了我的脑海:http: //www.janeg.ca/scjp/oper/shift.html
What would the explanation be, if talking to a kid?
如果和孩子说话,解释会是什么?
回答by In silico
Computers are binary devices. Because of this, numbers are represented by a sequence of 1s and 0s.
计算机是二进制设备。因此,数字由 1 和 0 的序列表示。
Bitshifting is simply moving those sequences of 1s and 0s left or right.
Bitshifting 只是将这些 1 和 0 序列向左或向右移动。
So all the >>
operator does is shift the bits towards the right one bit.
因此,>>
运算符所做的就是将位向右移动一位。
Consider the number 101:
考虑数字 101:
// Assuming signed 8-bit integers
01100101 // How 101 is represented in binary
00110010 // After right shifting one bit, this represents 50
The least significant bit in this case was truncated. Obviously the devil's in the details, but that's all there is really to it.
在这种情况下,最低有效位被截断。显然,魔鬼在细节中,但这就是它真正的全部内容。
The <<
operator does the opposite operation:
在<<
操作者不相反的操作:
// Assuming signed 8-bit integers
01100101 // How 101 is represented in binary
11001010 // After left shifting one bit, this represents -54
// Assuming unsigned 8-bit integers
01100101 // How 101 is represented in binary
11001010 // After left shifting one bit, this represents 202
In this case, the most significant bit was truncated since I used only 8-bits. If the number had more bits, however:
在这种情况下,最高有效位被截断,因为我只使用了 8 位。但是,如果数字有更多位:
// Assuming signed 16-bit integers
00000000 01100101 // How 101 is represented in binary
00000000 11001010 // After left shifting one bit, this represents 202
00000001 10010100 // After left shifting one bit again, this represents 404
So you may get different numbers depending on how many bits and the data types associated with those bits you're dealing with.
因此,您可能会得到不同的数字,具体取决于您正在处理的位数和与这些位相关联的数据类型。
Addendum:If you're wondering how binary works, think about how the decimal number system works. Consider the number 5287. It can be written like this:
附录:如果您想知道二进制的工作原理,请考虑十进制数字系统的工作原理。考虑数字 5287。它可以写成这样:
5287
But you can also write it out like this:
但是你也可以这样写:
5287 = (5 * 1000) + (2 * 100) + (8 * 10) + (7 * 1)
Which you can then write out like this:
然后你可以这样写:
5287 = (5 * 10^3) + (2 * 10^2) + (8 * 10^1) + (7 * 10^0)
The above equation explains why the decimal number system is sometimes called the base-10 system. The decimal number system employs the use of 10 digits (0-9). Notice how the exponents correspond to digit position.
上面的等式解释了为什么十进制数系统有时被称为以 10 为基数的系统。十进制数字系统使用 10 位数字 (0-9)。注意指数如何对应数字位置。
The binary number system, or the base-2 system, is the exact same thing but with the number two as the base of the exponents, and employing only two digits: 0 and 1.
二进制数系统或基数 2 系统完全相同,但以数字 2 作为指数的基数,并且仅使用两个数字:0 和 1。
5287 = 00010100 10100111 (base 2)
= (0 * 2^15) + (0 * 2^14) + (0 * 2^13) + (1 * 2^12)
+ (0 * 2^11) + (1 * 2^10) + (0 * 2^9) + (0 * 2^8)
+ (1 * 2^7) + (0 * 2^6) + (1 * 2^5) + (0 * 2^4)
+ (0 * 2^3) + (1 * 2^2) + (1 * 2^1) + (1 * 2^0)
回答by Erica
Can I assume the kid I'm talking to knows a bit about binary? :)
我可以假设我正在与之交谈的孩子对二进制有所了解吗?:)
All numbers can be represented in some kind of binary, like so:
所有数字都可以用某种二进制表示,如下所示:
Base 10 : Base 2
1 : 0001
2 : 0010
3 : 0011
4 : 0100
5 : 0101
6 : 0110
7 : 0111
8 : 1000
... and so on.
... 等等。
The shift operators basically move all of the bits (1s or 0s) across one position. So, for example: 000111 >> 1
移位运算符基本上将所有位(1 或 0)移动到一个位置。所以,例如:000111 >> 1
shifts all the bits in 000111 right by one number to produce this:
将 000111 中的所有位右移一个数字以产生以下结果:
000011
000011
000111 << 1
000111<<1
shifts all those bits left by one, to produce this:
将所有这些位左移一,以产生以下结果:
001110
001110
If you shift by more than one, then it just moves the bits further.
如果您移位不止一个,那么它只会进一步移动这些位。
Now, depending on what language you're using and the kind of numbers you're working with, it can be a little bit more complicated than that. For example, if you are working in a language where the "most significant bit" (the one furthest to the left in a number) represents whether the number is signed or not, then the language will have to take that into account.
现在,根据您使用的语言和您使用的数字类型,它可能比这更复杂一点。例如,如果您使用一种语言,其中“最高有效位”(数字中最左边的一位)代表数字是否有符号,则该语言必须考虑到这一点。
Mathematically speaking, if you take an integer (and ignore the risk of overflows, which are caused by the computer running out of space to store bits,) shift left by 1 (<< 1) is the equivalent of multiplying by 2, and shift right by 1 is the equivalent of dividing by 2. (Think a bit about what a "place value" in binary maths is worth, and that'll make sense)
从数学上讲,如果你取一个整数(并忽略溢出的风险,这是由计算机用完空间来存储位引起的),左移 1 (<< 1) 相当于乘以 2,然后移位右除以 1 相当于除以 2。(想想二进制数学中的“位值”是多少,这是有道理的)
回答by Thorin Oakenshield
>>
the SHIFT RIGHT
operator
>>
该SHIFT RIGHT
运营商
Example:
例子:
class X
{
public static void main(String args[])
{
System.out.println("20>>2 = "+20>>2);
}
}
Output : 20>>2 = 5
输出 : 20>>2 = 5
Explanation:
解释:
Binary value of 20
is: 00000000000000000000000000010100
的二进制值为20
:00000000000000000000000000010100
shift all bits 2
positions to right 00000000000000000000000000000101
将所有位2
位置右移00000000000000000000000000000101
It will give 5
( 1*2^2 + 0*2^1 + 1*2^0
)
它会给5
( 1*2^2 + 0*2^1 + 1*2^0
)
回答by user unknown
I once wrote an JApplet (bitorgel) and put it on my web page, where one can play around with bit operators. You can try it live, or download the source. AFAIK they work the same in C, C++ and Java - probably in C# too.
我曾经写过一个 JApplet (bitorgel) 并将它放在我的网页上,在那里人们可以玩弄位运算符。您可以现场试用,或下载源代码。AFAIK 它们在 C、C++ 和 Java 中的工作方式相同 - 可能也在 C# 中。