开发者

Java << Operator [duplicate]

开发者 https://www.devze.com 2023-04-01 18:51 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: What is the following sign: <<?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What is the following sign: <<?

Can someone please explain the << in the开发者_开发知识库 follow sample code?

final static public int MY_VAR = 1<<3;

Thank you!


Sure, it's a left shift - you're shifting the number "1" left by 3 bits, so the result will be 8.

See section 15.19 of the Java Language Specification for more details.


It's the bitwise left-shift operator. It shifts bits to the left, like so:

00000001 << 3 == 00001000

In other words, 1 << 3 == 8, since you shift the 1 bit over by 3 places.


It is a bitshift operator. See http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html for docs.


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.

[Source]


<< the left shift operator.

Basically it moves every bit in the left-hand value to the left by an amount indicated by the right-hand value.

So 0b1 (decimal 1) becomes 0b1000 (decimal 8) in this example.

It's explained in this tutorial and illustrated in this Wikipedia article.


It's 2 raised to the power of 3. It's a bitwise operator SHIFT_LEFT: 0001 => 0100

0

精彩评论

暂无评论...
验证码 换一张
取 消