I was doing the 70-480 paper today and I came across a question which had >>
was just wondering what it meant.
When to use it, how to use it that sort of thing

- 189
- 1
- 3
- 12
-
Post how you saw it used. – tymeJV Mar 17 '14 at 20:49
-
It's a [bitwise operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) – CrayonViolent Mar 17 '14 at 20:50
-
If you're working on a certification exam intended to make it possible to demonstrate your familiarity with this topic, you may want to familiarize yourself with [the JavaScript language spec](http://www.ecma-international.org/ecma-262/5.1/) while you're at it. Questions like yours have authoritative answers there. – Pointy Mar 17 '14 at 20:51
4 Answers
>>
is Sign-propagating right shift.
Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off.
Example:
9 (base 10): 00000000000000000000000000001001 (base 2) --------------------------------
9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)

- 25,304
- 9
- 61
- 68
It is a bit shifting operator that much I can say. As for when to use it it is for when you need to modify a value at the bit level for instance lets say you need to change 1 to 4 you could go 1 << 2 which its bit representations is 0001 then you shift it over twice that makes it 0100 which is 4. As for when you would want to use it, it has to do with design and prefernce for storage. At times it can make storing information require less memory.

- 4,609
- 2
- 22
- 32
>>
is a right bitwise shift. It shifts all of the bits in a value to the right however many places you specify.
Let's look at an example:
Take the number 9. In binary, this is 1001
. If we shift 9 one place to the right, we end up with 100
(the trailing 1 was shifted off), which is 4.
So (9 >> 1)
is equal to 4.
If we shift two places, we end up with 10
, which is 2. So (9 >> 2)
is 2.
These operators are commonly used when creating flag values. Let's say that you're developing a video game. A character can run, jump, and shoot. We can define these as flags:
var IN_RUN = (1 << 0);
var IN_JUMP = (1 << 1);
var IN_SHOOT = (1 << 2);
Note that here we are using <<
instead of >>
. It's the same idea, but it shifts to the left instead.
This expression is equivalent to:
var IN_RUN = 1;
var IN_JUMP = 2;
var IN_SHOOT = 4;
It's easier to use shifts though, especially when you start getting to the higher values.
We can combine flags using the |
operator. If we want to specify that someone is running and shooting, we'd use IN_RUN|IN_SHOOT
. This basically adds the values together (technically it combines the values on a binary level, setting bits to 1
if they are 1
in either value. So IN_RUN|IN_SHOOT
is equal to 5.
You can then check if a value has a specific flag using the &
operator (notice there's only one). myValue & IN_RUN
will return IN_RUN
if that flag is in the value.
TL;DR: Binary operators are commonly used to store many boolean values in a single value on a binary level.

- 2,727
- 2
- 15
- 19
>>
is the right bitshift operator. You can use it like this:
var x = 50; // Binary 00110010
var y = x >> 3; // Decimal 6, 00000110
You can see that the binary value of x gets shifted over right 3 bits. you can also use <<
for left shifting.
I can't think of any practical uses for it off my head. but you could use it for bitfields, like so
var MY_BOOLEAN_SETTING = 1 << 0; // 1, 00000001
var MY_OTHER_BOOLEAN_SETTING = 1 << 1; // 2, 00000010
var MY_THIRD_BOOLEAN_SETTING = 1 << 2; // 4, 00000100
var bitfield = MY_BOOLEAN_SETTING | MY_THIRD_BOOLEAN_SETTING; // 0000 0101
if(bitfield & MY_OTHER_BOOLEAN_SETTING){ //checks if 0010 is in the bitfield (its not)
//some code
}
if(bitfield & MY_BOOLEAN_SETTING){ //checks if 0001 is in the bitfield (it is)
//some more code
}

- 148
- 1
- 5