-1

Possible Duplicate:
>> in javascript

Here:

var num=10;
console.log(num/2);
num=4;
console.log(num/2);

It gives me 5 and 2.

And this one:

var num=10;
console.log(num>>1);
num=4;
console.log(num>>1);

It also gives me 5 and 2.
So x/2 is the same as x>>1? But why?

Community
  • 1
  • 1
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • 12
    Are you familiar with binary representations of integers? – Oliver Charlesworth Dec 11 '11 at 03:01
  • 3
    I think you just need to learn what the `>>` operator does... – deceze Dec 11 '11 at 03:03
  • google for "bit shift operation" – wim Dec 11 '11 at 03:05
  • Then should I use `>>1` for optimizing my code? – Derek 朕會功夫 Dec 11 '11 at 22:02
  • 1
    @Derek: Not unless you can prove (via benchmarking) that 1) the piece of code containing the division is the bottleneck in your application and 2) that changing the division to a shift actually improves performance. Most compilers will do this for you, so in general, changing divisions to shifts merely results in less-readable code with equivalent performance. – Cameron Dec 11 '11 at 22:38

4 Answers4

13

For the same reason that dropping the last digit off a normal (decimal) number is the same as dividing it by 10 (ignoring, of course, any non-integer remainder).

In computers, integers are internally represented in binary (base 2). So each digit represents a power of 2 instead of a power of 10 that we're used to with the decimal system.

>> 1 just means to shift all the bits right by one, which is another way of saying "drop the last digit". Since the digits are in binary, that's equivalent to dividing by the base, which is 2.

Similarly, if you need to divide by any power of 2, you can do so using the right shift operator: To divide by 4, shift by 2; to divide by 8, shift by 3; and so on.

Note that internally, it's often more efficient to do a shift operation instead of a division operation, but any compiler worth its salt will do this optimization for you (so that you don't have to write obfuscated code to get the performance benefit -- generally, you would only use the shift operator when your intention is to manipulate bits directly, and use the division operator when your intention is to do math).

Cameron
  • 96,106
  • 25
  • 196
  • 225
1

x>>1 is a bit shift, which operates on the number's binary representation. The effect is that x>>n is the same as x/(2^n) (except that bit shift is usually faster than division, as it is lower level).

Allen Z.
  • 1,560
  • 1
  • 11
  • 19
0

When you >> something, you basically shift all its bits to the right. When that happens, you shift the 2-place value into the 1-place value, and the 4-place value into the 2-place value, and so on. This effectively divides a number in half. Take the number 14, for example: 1110. When you shift the bits, you get 111, or 7.

Take a look at this: http://en.wikipedia.org/wiki/Division_by_two#Binary

eboix
  • 5,113
  • 1
  • 27
  • 38
0

Everything you need to know about >> can be found here and here.

mwcz
  • 8,949
  • 10
  • 42
  • 63