什么>>在java做? [英] What does >> do in java?

查看:133
本文介绍了什么>>在java做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我尝试查找了什么>>,或者转移意味着什么,但是这个网站解释了它: 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

所以有人可以解释一下他们正和一个孩子说话?

So can someone explain it like they're talking to a kid?

推荐答案

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

Computers are binary devices. Because of this, numbers are represented by a sequence of 1s and 0s.

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

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.

考虑数字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

在这种情况下,最重要的一位是截断,因为我只使用8位。但是,如果数字有更多的位:

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.

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

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 = (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)。注意指数如何与数字位置对应。

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.

二进制数系统或base-2系统完全相同,但以二号为基数指数,只使用两位数:0和1.

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)

这篇关于什么&gt;&gt;在java做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆