“<<” java中的运算符

发布于 2024-09-28 20:13:41 字数 398 浏览 3 评论 0 原文

以下语句来自java的Character类:

(1 << Character.PARAGRAPH_SEPARATOR)) >> type

PARAGRAPH_SEPARATOR是一个字节,type是一个整数。

这句话中的运算符,他们是做什么的?我如何以及在哪里可以使用这些运算符?

这是oracle java.lang.Character 文档。类中几乎所有方法都使用这些运算符。

Fallowing statement is from Character class of java:

(1 << Character.PARAGRAPH_SEPARATOR)) >> type

PARAGRAPH_SEPARATOR is a byte and type is an integer.

The operators in this sentence, what do they do? how and where I can use those operators?

Here is the oracles java.lang.Character doc. Nearly all the methods in the class uses those operators.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

千柳 2024-10-05 20:13:41

它们是位移运算符。 << 将位“左”移(向最高有效位),>> 反之亦然。左移或右移 n 位与分别乘或除 2n 几乎相同。

有关如何在此上下文中使用这些运算符的说明,请参阅 @axtavt 的评论。

They are bit-shift operators. << shifts the bits "left" (towards the most-significant bit), and vice-versa for >>. Shifting left or right by n bits is pretty much the same as multiplying or dividing, respectively, by 2n.

See @axtavt's comment for an explanation of how these operators are being used in this context.

枯寂 2024-10-05 20:13:41

这些是按位移位运算符。

如果您左移以下字节:

00000001

您将得到:

00000010

即模式已“移动”到左侧,并且右侧填充了零。因此,如果您对该结果应用右移位运算符 >>,您将再次获得原始字节。

您会注意到这些数字的十进制值为 1 和 2。如果您再次向左移动,您将得到:

00000100 = 4

因此您会看到向左移动会将数字乘以 2(假设它没有溢出),而右移除以二。这在大多数计算机中都非常有效地发生。这是您如何实际使用这些运算符的一个示例。

These are the bitwise shift operators.

If you left shift the following byte:

00000001

you would get:

00000010

I.e. the pattern has "shifted" to the left and zeros fill in on the right. So if you apply the right shift operator >> on that result, you'll get the original byte again.

You'll notice that the decimal values of these numbers are 1 and 2. If you shift left once again you'll get:

00000100 = 4

So you see that shifting to the left multiplies the number by two (given that it doesn't overflow), while right shifting divides by two. This happens very efficiently in most computers. So that's one example of how you might use these operators in a practical way.

池予 2024-10-05 20:13:41

<< 是左移运算符:它将计算机中存储的二进制数左移。例如,二进制的 9 是 1001。 9 << 2 在二进制 (36) 中得到 100100,因为它将左移并在末尾添加 0。 <代码>1 << n 与 Math.pow(2, n) 相同,只是它通常更快更好,并且返回 int,而不是 <代码>双

>> 是右移。它将其右移,并丢弃空位。 13 的二进制为 1101,因此 13 >>> 1 是二进制的 110,通常是 6。

<< is the left shift operator: It shifts the binary number stored in the computer left. For example, 9 in binary is 1001. 9 << 2 makes 100100 in binary (36), because it shifts it left and adds 0s at the end. 1 << n is the same thing as Math.pow(2, n) except it is way faster and better in general, as well as returning int, not double.

>> is right shift. It shifts it right, and discards empty bits. 13 is 1101 in binary, so 13 >> 1 is 110 in binary, or 6 normally.

禾厶谷欠 2024-10-05 20:13:41

它的工作原理是高度优化的多重比较。带符号左移运算符“<<”将位模式左移,带符号右移运算符“>>”向右移动一个位模式。位模式由左侧操作数和右侧操作数要移位的位置数给出。无符号右移运算符“>>”将零移到最左边的位置,而“>>”之后的最左边的位置取决于符号扩展。

欲了解更多详情,您可以访问以下链接。
http://docs.oracle.com/javase/tutorial/java/ nutsandbolts/op3.html

It works as a highly optimized multiple comparison. The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

for more detail,You can visit below link .
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

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