>> 什么意思?用Java做?
好吧,我尝试查找 >>
或 Shift 的含义,但正如该网站所解释的那样,这超出了我的理解范围:http://www.janeg.ca/scjp/oper/shift.html
如果与孩子交谈,解释是什么?
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
What would the explanation be, if talking to a kid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
计算机是二进制设备。因此,数字由 1 和 0 的序列表示。
位移位只是将 1 和 0 的序列向左或向右移动。
因此,
>>
运算符所做的就是将位向右移动一位。考虑数字 101:
在这种情况下,最低有效位被截断。显然,细节决定成败,但这就是真正的全部。
<<
运算符执行相反的操作:在本例中,最高有效位被截断,因为我只使用了 8 位。但是,如果该数字具有更多位:
因此,您可能会得到不同的数字,具体取决于有多少位以及与您正在处理的这些位相关的数据类型。
附录:如果您想知道二进制如何工作,请考虑十进制数字系统如何工作。考虑数字 5287。它可以这样写:
但您也可以这样写:
然后您可以这样写:
上面的等式解释了为什么十进制数字系统有时称为以 10 为底的系统。十进制数字系统使用 10 位数字 (0-9)。请注意指数如何与数字位置相对应。
二进制数字系统或以 2 为底的系统是完全相同的,但以数字 2 作为指数的底数,并且仅使用两个数字:0 和 1。
Computers are binary devices. Because of this, numbers are represented by a sequence of 1s and 0s.
Bitshifting is simply moving those sequences of 1s and 0s left or right.
So all the
>>
operator does is shift the bits towards the right one bit.Consider the number 101:
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:In this case, the most significant bit was truncated since I used only 8-bits. If the number had more bits, however:
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:
But you can also write it out like this:
Which you can then write out like this:
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.
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.
我可以假设与我交谈的孩子了解一点二进制吗? :)
所有数字都可以用某种二进制表示,如下所示:
...
等等。
移位运算符基本上将所有位(1 或 0)移动到一个位置。因此,例如:
000111>> 1
将 000111 中的所有位右移一位以产生:
000011
000111 << 1
将所有这些位左移一位,产生:
001110
如果移位超过一位,那么它只会进一步移动这些位。
现在,根据您使用的语言和您使用的数字类型,它可能会比这更复杂一些。例如,如果您使用的语言中“最高有效位”(数字中最左边的位)表示该数字是否有符号,则该语言必须考虑到这一点。
从数学上讲,如果您采用一个整数(并忽略溢出的风险,这是由于计算机耗尽存储位的空间而导致的),左移 1 (<< 1) 相当于乘以 2,右移 1 相当于除以 2。(稍微思考一下二进制数学中的“位值”值多少钱,这就有意义了)
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:
...
and so on.
The shift operators basically move all of the bits (1s or 0s) across one position. So, for example:
000111 >> 1
shifts all the bits in 000111 right by one number to produce this:
000011
000111 << 1
shifts all those bits left by one, to produce this:
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)
>>
SHIFT RIGHT
运算符示例:
输出:
20>>2 = 5
解释:
20< 的二进制值/code> 是:
00000000000000000000000000010100
将所有位
2
位置向右移动000000000000000000000000000000101
它将给出
5
(1*2^2 + 0*2^1 + 1*2^0
)>>
theSHIFT RIGHT
operatorExample:
Output :
20>>2 = 5
Explanation:
Binary value of
20
is:00000000000000000000000000010100
shift all bits
2
positions to right00000000000000000000000000000101
It will give
5
(1*2^2 + 0*2^1 + 1*2^0
)我曾经写过一个 JApplet (bitorgel) 并将其放在我的网页上 ,可以在这里使用位运算符。您可以实时尝试,或下载源代码。 AFAIK 它们在 C、C++ 和 Java 中的工作方式相同 - 可能在 C# 中也是如此。
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.