>> 什么意思?用Java做?

发布于 2024-09-27 09:13:06 字数 210 浏览 8 评论 0原文

好吧,我尝试查找 >> 或 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

芸娘子的小脾气 2024-10-04 09:13:06

计算机是二进制设备。因此,数字由 1 和 0 的序列表示。

位移位只是将 1 和 0 的序列向左或向右移动。

因此,>> 运算符所做的就是将位向右移动一位。

考虑数字 101:

// Assuming signed 8-bit integers
01100101 // How 101 is represented in binary
00110010 // After right shifting one bit, this represents 50

在这种情况下,最低有效位被截断。显然,细节决定成败,但这就是真正的全部。

<< 运算符执行相反的操作:

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

在本例中,最高有效位被截断,因为我只使用了 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

因此,您可能会得到不同的数字,具体取决于有多少位以及与您正在处理的这些位相关的数据类型。

附录:如果您想知道二进制如何工作,请考虑十进制数字系统如何工作。考虑数字 5287。它可以这样写:

5287

但您也可以这样写:

5287 = (5 * 1000) + (2 * 100) + (8 * 10) + (7 * 1)

然后您可以这样写:

5287 = (5 * 10^3) + (2 * 10^2) + (8 * 10^1) + (7 * 10^0)

上面的等式解释了为什么十进制数字系统有时称为以 10 为底的系统。十进制数字系统使用 10 位数字 (0-9)。请注意指数如何与数字位置相对应。

二进制数字系统或以 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)

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:

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

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

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.

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.

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)
抚你发端 2024-10-04 09:13:06

我可以假设与我交谈的孩子了解一点二进制吗? :)

所有数字都可以用某种二进制表示,如下所示:

   Base 10 : Base 2
   1 : 0001
   2 : 0010
   3 : 0011
   4 : 0100
   5 : 0101
   6 : 0110
   7 : 0111
   8 : 1000

...
等等。

移位运算符基本上将所有位(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:

   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

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)

唱一曲作罢 2024-10-04 09:13:06

>> SHIFT RIGHT 运算符

示例:

class X
      { 
       public static void main(String args[])
       {
         System.out.println("20>>2 = "+20>>2);
       }
      }        

输出:20>>2 = 5

解释:

20< 的二进制值/code> 是: 00000000000000000000000000010100

将所有位 2 位置向右移动 000000000000000000000000000000101

它将给出 5 ( 1*2^2 + 0*2^1 + 1*2^0 )

>> the SHIFT RIGHT operator

Example:

class X
      { 
       public static void main(String args[])
       {
         System.out.println("20>>2 = "+20>>2);
       }
      }        

Output : 20>>2 = 5

Explanation:

Binary value of 20 is: 00000000000000000000000000010100

shift all bits 2 positions to right 00000000000000000000000000000101

It will give 5 ( 1*2^2 + 0*2^1 + 1*2^0 )

我不在是我 2024-10-04 09:13:06

我曾经写过一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文