Zeeno
Zeeno

Reputation: 2721

What do ">>" and "<<" mean in Javascript?

I have a piece of Javascript code I'm trying to understand

// read big-endian (network byte order) 32-bit float
readFloat32 = function(data, offset) {
    var b1 = data.charCodeAt(offset) & 0xFF,
        b2 = data.charCodeAt(offset+1) & 0xFF,
        b3 = data.charCodeAt(offset+2) & 0xFF,
        b4 = data.charCodeAt(offset+3) & 0xFF;
    var sign = 1 - (2*(b1 >> 7));       //<--- here it is and 2 lines below
    var exp = (((b1 << 1) & 0xff) | (b2 >> 7)) - 127;
    var sig = ((b2 & 0x7f) << 16) | (b3 << 8) | b4;
    if (sig == 0 && exp == -127)
      return 0.0;
    return sign * (1 + sig * Math.pow(2, -23)) * Math.pow(2, exp);
}

what does ">>" mean? Is it a special type of boolean (like '<' or '>')

Upvotes: 12

Views: 12523

Answers (10)

Jeest
Jeest

Reputation: 66

It is an arithmetic shift

Upvotes: 2

mjv
mjv

Reputation: 75115

These are the shift right (with sign) and shift left operators.

Essentially, these operators are used to manipulate values at BIT-level.
They are typically used along with the the & (bitwise AND) and | (bitwise OR) operators and in association with masks values such as the 0x7F and similar immediate values found the question's snippet.
The snippet in question uses these operators to "parse" the three components of a 32 bits float value (sign, exponent and fraction).

For example, in the question's snippet:
1 - (2*(b1 >> 7)) produces the integer value 1 or -1 depending if the bit 7 (the 8th bit from the right) in the b1 variable is zero or one respectively.
This idiom can be explained as follow.

  • at the start, b1, expressed as bits is 0000000000000000abcdefgh
    note how all the bits on the left are zeros, this comes from the
    b1 = data.charCodeAt(offset) & 0xFF assignement a few lines above, which essentially zero-ed all the bits in b1 except for the rightmot 8 bits (0xFF mask).
    a, b, c... thru h represent unknown boolean values either 0 or 1.
    We are interested in testing the value of a.
  • b1 >> 7 shifts this value to the right by 7 bits, leaving
    b1 as 00000000000000000000000a which, read as an integer will have value 1 or 0
  • this 1 or 0 integer value is then multiplied by 2
    it is then either 2 or 0, respectively.
  • this value is then substracted from 1, leaving either -1 or 1.

Although useful to illustrate the way the bit-operators work, the above idiom could be replaced by something which tests the bit 7 more directly and assigns the sign variable more explicitly. Furthermore this approach does not require the initial masking of the leftmost bits in b1:

var sign
if (b1 & 0x80)   // test bit 7  (0x80 is  [00000000]10000000)
  sign = -1;
else
  sign = 1;

Upvotes: 14

Michalis
Michalis

Reputation: 1538

From http://ecma262-5.com/ELS5_HTML.htm

11.7 Bitwise Shift Operators

ShiftExpression : AdditiveExpression ShiftExpression << AdditiveExpression ShiftExpression >> AdditiveExpression ShiftExpression >>> AdditiveExpression

11.7.1 The Left Shift Operator ( << ) Performs a bitwise left shift operation on the left operand by the amount specified by the right operand. The production ShiftExpression : ShiftExpression << AdditiveExpression is evaluated as follows:

Let lref be the result of evaluating ShiftExpression.
Let lval be GetValue(lref).
Let rref be the result of evaluating AdditiveExpression.
Let rval be GetValue(rref).
Let lnum be ToInt32(lval).
Let rnum be ToUint32(rval).
Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum & 0x1F.
Return the result of left shifting lnum by shiftCount bits. The result is a signed 32-bit integer.

11.7.2 The Signed Right Shift Operator ( >> ) Performs a sign-filling bitwise right shift operation on the left operand by the amount specified by the right operand.

The production ShiftExpression : ShiftExpression >> AdditiveExpression is evaluated as follows:

Let lref be the result of evaluating ShiftExpression.
Let lval be GetValue(lref).
Let rref be the result of evaluating AdditiveExpression.
Let rval be GetValue(rref).
Let lnum be ToInt32(lval).
Let rnum be ToUint32(rval).
Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum & 0x1F.
Return the result of performing a sign-extending right shift of lnum by shiftCount bits. The most significant bit is propagated. The result is a signed 32-bit integer.

Upvotes: 1

tskuzzy
tskuzzy

Reputation: 36446

They are bitshift operators. Numbers in the computer are represented in binary. Shifting left is equivalent to multiplying by 2 and shifting right is equivalent to dividing by 2.

For example, the number 8 is 1000 in binary. Shift left << by 3 would yield 1000000 which is 64. Shift right by 2 would yield 10 which is 2.

Upvotes: 0

Ricardo Marimon
Ricardo Marimon

Reputation: 10687

shift a by b bits to the left (padding with zeros)

a << b

shift a by b bits to the right (copying the sign bit)

a >> b

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

Right and Left and shift operators.

Upvotes: 1

the_drow
the_drow

Reputation: 19181

It's the bitshifting operator. See here for more details.

Upvotes: 0

Dennis
Dennis

Reputation: 32598

You can read about the operators here: https://developer.mozilla.org/en/JavaScript/Reference/operators/bitwise_operators

They are bit shifts and also occur in languages other than JS.

Example: 5 >> 1 = 2

binary: 0101 shifting one position = 0010

Upvotes: 4

sushil bharwani
sushil bharwani

Reputation: 30187

shift left and shift right operators. If you have a number it will shift its bits to left or right.

Upvotes: 0

Sascha Galley
Sascha Galley

Reputation: 16091

These are bit operators. Have a look at this link: Bitwise Operators

Upvotes: 7

Related Questions