1

I try the code

System.out.println( 16 >>> 3);

and the output is 2 !

but how does this work? What is the syntax?

Rafael Murata
  • 85
  • 1
  • 7

8 Answers8

8

16 >> 3 is a bitshift to the right, and is equivalent to dividing by 8 (2 ** 3).

16 -> 00010000
         ^
          \
           \
2 ->  00000010
Marc B
  • 356,200
  • 43
  • 426
  • 500
3

This is called right-shift with zero-fill (also called - unsigned right shift).

To make it easy to understand, for every shift, your number is divided by 2. So:

16 >>> 3  ==  16 / 2^3 == 16 / 8 == 2

For more details see, JLS - Shift Operators.

For real visualization, take a look at the bit representation of the two numbers:

16        - 00010000
16 >>> 3  - 00000010 == 2
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

The >>> operator is performing an unsigned right-shift of bits, when applied to an integer it has the effect of dividing by two. In particular, if the integer is a power of two, each right-shift exactly halves the value. For example:

16 >>> 1 // returns 8
16 >>> 2 // returns 4
16 >>> 3 // returns 2

In binary form, 16 is represented as 10000. If we shift the value three times to the right it ends up as 00010, which is 2. Also, take a look at the Java tutorial.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

Another note, the 3 chevrons indicate an unsigned right bit shift. Although it does not make a difference in this example...

The extra > indicates the sign is not reserved, an example:

-128 >> 2 = -128 / 2^2 = -32

The signed bit is retained giving:

(1) 10000000 >> 2 = (1) 100000 
11111111 11111111 11111111 10000000 >> 2 = 1111111 11111111 11111111 11100000

If >>> is used then the signed bit is also shifted converting it to a positive number.

-128 >>> 2 = 536870896

11111111 11111111 11111111 10000000 >> 2 = 0011111 11111111 11111111 11100000
DairyLea
  • 802
  • 1
  • 8
  • 16
  • Id be interested to see examples of where it would make a difference, would negative numbers be multipiled by 2? – Richard Tingle Jul 10 '13 at 19:23
  • Try to Google it, there are already plenty of examples on the web, and even SO. But basically no, negative numbers are not multiplied by 2. The signed bit is not reserved, so they become some positive value. For example consider the signed (32bit) integer -128: 11111111 11111111 11111111 10000000. If we perform (-128 >>> 3) you get 536870896. Because the signed bit is shifted to give 00011111 11111111 11111111 11110000 – DairyLea Jul 11 '13 at 07:08
  • Fair enough, I was more implying that would make your answer awesome and I would upvote it but fair enough I'll move along – Richard Tingle Jul 11 '13 at 07:12
  • I guess it wouldn't really answer your question at the top though :p – DairyLea Jul 11 '13 at 07:49
  • My question? Im not Rafael – Richard Tingle Jul 11 '13 at 07:52
  • Oh yeah, sorry! Ill update my answer anyway after all these comments – DairyLea Jul 11 '13 at 07:57
0

You're using a bitwise shift consult this post

In binary, every 0 is an increasing magnitude of the power 2 for example:

10000 = 16

01000 = 8

00100 = 4

00010 = 2

00001 = 1

The expression 16 >>> 3 in java is saying to move the bit over 3 places, so we are left with 2.

Marco Corona
  • 812
  • 6
  • 12
0

16 in binary is 00010000. You are shifting the bits right three positions which results in 00000010 which is 2

mistahenry
  • 8,554
  • 3
  • 27
  • 38
0

This is a bit shift, which is dividing by 2^3 (due to shifting by 3 positions) and truncating(rounding down) as we are not doing fillaround.

When using >>> we move to the right and divide. When using <<< it goes opposite, so you'd multiply by the correct power of 2.

Bit shifts are a type of bitwise operation that operates on a primitive(almost always number's) bits directly.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

It's an unsigned right shift operator, and the above shifts 3 times.

i.e. it divides by 2 each time, giving you 2.

See here for more information on Java operators.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440