5

Possible Duplicates:
What do these operators do?
>> in javascript

Can somebody please explain the bitwise operator >> 1?

example:

65 >> 1 = 32

and also when >> 0

what does it achieve in this example:

var size = (Math.random() * 100 >> 0) + 20;

Community
  • 1
  • 1
davivid
  • 5,910
  • 11
  • 42
  • 71
  • 4
    65 = `1000001`, shifting bits will give you `0100000`, which represents `32`. –  May 19 '11 at 09:05
  • 2
    possible duplicate of [What do these operators do?](http://stackoverflow.com/questions/4535328/what-do-these-operators-do), [>> in javascript](http://stackoverflow.com/questions/4437169/in-javascript) and [Meaning of >>, <<, | and & in JavaScript](http://stackoverflow.com/questions/4541455/meaning-of-and-in-javascript). http://stackoverflow.com/search?q=javascript+bitwise – Andy E May 19 '11 at 09:06

7 Answers7

8
var size = (Math.random() * 100 >> 0) + 20;

>> 0 in the above example is used to eliminate the fractional portion, as follows:

  1. Math.random() returns a number between 0 and 0.99999999...
  2. This number multiplied by 100 gives you another number between 0 and 99.999999...
  3. This number is right shifted 0 times. The number is implicitly cast to an integer for the shift operation; right shifting 0 times does not have any effect on the value of the resulting integer. You thus end up with an integer between 0 and 99. Note that you could have used the Math.floor() function instead of >> 0.
  4. Add 20 to the integer, the result is an integer between 20 and 119.
Salman A
  • 262,204
  • 82
  • 430
  • 521
3

Bitwise operator >> means shift right.
It moves the binary value to the right (and removes the right-most bit).

65 >> 1 in binary is:

1000001 >> 1 = 100000 = 32

It effectively divides the number into 2 and drops the remainder.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
2

The operator '>>' shifts the contents of a variable right by 1 bit. This results, effectively, in integer division of that value by 2 as you show in your example:

   65 >> 1 = 32

Let's say that a variable is always 32 bits long. The example then says:

   65 decimal >> 1 = 32  or, in hex, 0x000041 >> 1 = 0x00000020

More generally: the operator '>>' divides its operand, as a 32-bit integer, by the power of 2 whose value is the shift length. Thus:

  129 decimal >> 1 = 64  or  0x000081 >> 1 = 0x000040
  129 decimal >> 2 = 32  or  0x000081 >> 2 = 0x000020
  129 decimal >> 5 =  2  or  0x000081 >> 5 = 0x000002

and

  129 decimal >> 8 =  0  or: 0x000081 >> 8 = 0x000000

The operator '<<' multiplies its operand, as you'd expect.

I don't know how Math.random( ) operates, but I'm willing to bet that the shift of its floating-point returned value right by 0 turns that number into an integer, because shifting left and right has arithmetic meaning only when the operand is an integer.

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
0

The bitwise shift operator shifts each bit of the input x bits to the right (>>) or to the left (<<).

65 is 1000001, thus 65 >> 1 = 0100000, which is 32.

EDIT

Here are some useful links:

http://en.wikipedia.org/wiki/Bitwise_operation

http://javascript.about.com/library/blbitop.htm

http://www.java2s.com/Tutorial/JavaScript/0040__Operators/ShiftLeft.htm

Christian
  • 4,345
  • 5
  • 42
  • 71
0

>> X takes the binary number and moves all the digits right by X places.

In your example, you use 65, which is 01000001 in binary. If you shift that right by one, the first space (on the left) gets filled in with a 0, and the last digit 'falls off the end'. Giving 00100000, which is the binary representation for 32.

>> 0, therefore shifts the number 0 spaces to the right, and does nothing.


'<< X', does the same, but shifts the number to the left.


These can be compared to multiplying by 2^X (Left-shift) or divinding by 2^X (right-shift), but it should be noted that a binary shift is much faster than a division operation.

MatBailie
  • 83,401
  • 18
  • 103
  • 137
  • 1
    *">> 0, therefore shifts the number 0 spaces to the right, and does nothing."* - not true. In JavaScript it converts the number to a 32 bit integer. On a number that is already a 32 bit integer it appears to do nothing, but on a larger number or a floating point number it will reduce/floor accordingly. – Andy E May 19 '11 at 09:11
0

You can understand why the output is 32 from rsplak's post. >> is the Right Bit Shift operator and using it as >> 1 will cause every bit to be shifted one place to the right. This means, if the rightmost bit was 1, it would get expelled and the left most bit will contain 0.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
0

The bitwise operator shifts an expression by a number of digits. So in your example you have 65 which ist binary 0100 0001 shiftet 1 position to the right so you got 0010 0000 which is 32 decimal.

Another example: 48 >> 3 = 6

48 decimal is 0011 0000 binary shifted 3 to the right is 0000 0110 which is 6 decimal.

For your second example I can not help you - I can not image why I would shift an expression by 0 positions but maybe you can find out debugging it?

chaosr
  • 494
  • 2
  • 6
  • 17