0

Possible Duplicate:
Double Greater Than Sign (>>) in Java?

What does the following achieve ?

int num = (10-2) >> 1;

Found this very helpful -

What are bitwise shift (bit-shift) operators and how do they work?

Community
  • 1
  • 1
blue-sky
  • 51,962
  • 152
  • 427
  • 752

6 Answers6

10

Shift right. It moves all the bits of the number right by the number specified

10-2 = 8 = 1000 in binary

8 >> 1 = 1000 >> 1 = 100 in binary = 4

thecoop
  • 45,220
  • 19
  • 132
  • 189
3

>> is just an operator to perform bitwise right shift

jmj
  • 237,923
  • 42
  • 401
  • 438
1

its bitwise right shift in java. it shifts all binary digits,1 place towards right and pastes a zero at the leftmost end. (10-2)=8 which is 1000 in binary, now right shifting each digit and pasting 0 at the leftmost position, it gives 0100 which equals 4.

desprado
  • 136
  • 1
  • 2
  • 13
  • 1
    This answer is not correct: >> does preserve the leftmost digit (which is the sign indicator) whereas >>> does insert a `0` at the leftmost position. You may prove this by `0xFFFFFFFF >> 1` and `0xFFFFFFFF >>> 1`. – Harmlezz Jun 16 '15 at 09:14
  • @Harmlezz But `0xFFFFFFFF >> 1 ` gives -1 which is also `0xFFFFFFFF`. This seems weird! – insumity Dec 18 '15 at 21:09
  • @foobar `0xFFFFFFFF` means all bits are set to `1`. The `>>` operator shifts all bits one position to the right and preserves the left-most bit, which is `1`. Hence the value `0xFFFFFFFF` won't change. Its not weird but consequent :-) – Harmlezz Jan 04 '16 at 14:59
0

Right shift the indicated number of bits.

Jay
  • 26,876
  • 10
  • 61
  • 112
  • 2
    OP could probably use a little bit of explanation. Chances are if OP doesn't know what the right shift operator is then OP doesn't know what the operation does either. – Brian Driscoll Mar 29 '11 at 16:42
0

http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

First result on google for ">> operator java"

Peeter
  • 9,282
  • 5
  • 36
  • 53
0

See the documentation here on Bitwise shift right

Roger
  • 15,793
  • 4
  • 51
  • 73