What do “>>” and “<<” mean in Javascript?

后端 未结 10 778
清酒与你
清酒与你 2020-11-29 13:39

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) {
    v         


        
相关标签:
10条回答
  • 2020-11-29 13:57

    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.
    
    0 讨论(0)
  • 2020-11-29 14:03

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

    0 讨论(0)
  • 2020-11-29 14:04

    It is an arithmetic shift

    0 讨论(0)
  • 2020-11-29 14:06

    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
    
    0 讨论(0)
提交回复
热议问题