The bitwise operator x >> 1 and x >> 0

Possible duplicates:
What do these operators do?
→ → in javascript

Can someone explain the bitwise operator >> 1?

Example:

65 >> 1 = 32

as well as when >> 0

what in this example is achieved:

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

+5
source share
7 answers
var size = (Math.random() * 100 >> 0) + 20;

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

  • Math.random () returns a number from 0 to 0.99999999 ...
  • This number, multiplied by 100, gives you another number from 0 to 99.999999 ...
  • 0 . ; 0 . , 0 99. , Math.floor() >> 0.
  • 20 , - 20 119.
+5

' → ' 1 . 2, :

   65 >> 1 = 32

, 32 . :

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

: ' → ' 32- 2, . :

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

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

'< , .

, Math.random(), , 0 , , .

+2

.
( ).

65 → 1 :

1000001 → 1 = 100000 = 32

2 .

+1

>> X X.

65, 01000001 . , () 0, " ". 00100000, 32.

>> 0, 0 .


'< < X ', , .


2 ^ X ( ) 2 ^ X ( ), , , .

0

, 32 rsplak. >> - , >> 1 . , 1, , 0.

0

The bitwise operator shifts the expression by several 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 code 0011 0000 binary shift 3 on the right - 0000 0110, which is 6 decimal.

For your second example, I cannot help you - I cannot understand why I would shift the expression to 0 positions, but maybe you will learn about debugging?

0
source

All Articles