在Javascript中,">>"和"<<"代表什么意思?

11

我有一段想要理解的Javascript代码

// 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);
}

">>"是什么意思?它是一种特殊的布尔类型吗(例如'<'或'>')

10个回答

12
这些是带符号右移左移运算符。
本质上,这些运算符用于在位级别上操作值。通常与位运算AND(&)和OR(|)运算符一起使用,并与掩码值结合使用,例如问题片段中发现的0x7F等立即值。问题片段使用这些运算符来“解析”32位浮点值的三个组成部分(符号、指数和分数)。
例如,在问题片段中:1 - (2*(b1 >> 7))会产生整数值1或-1,具体取决于b1变量中第7位(从右边数第8位)是零还是一。可以解释为如下习惯用法。
  • 在开始时,b1被表示为位,如下:0000000000000000abcdefgh
    请注意左侧的所有位都是零,这来自于上面几行中的b1 = data.charCodeAt(offset) & 0xFF赋值,它基本上将b1中除右侧8位(0xFF掩码)之外的所有位都清零了。
    a,b,c ... h代表未知的布尔值,可以是0或1。
    我们有兴趣测试a的值。
  • b1 >> 7将该值向右移动7位,将b1变为00000000000000000000000a,读作整数将具有值1或0
  • 然后将此1或0 整数值乘以2
    因此,分别为2或0。
  • 然后从1中减去该值,得到-1或1。

尽管上述习惯用法对说明位运算符的工作方式很有用,但可以使用一些更直接地测试第7位并更明确地分配符号变量的方法来替换它。此外,这种方法不需要在b1中屏蔽最左侧的位:

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

7
这些是位运算符。请查看此链接:位运算符

3

2

1

将 a 向左移动 b 位(用零填充)

a << b

将变量a右移b位(复制符号位)。
a >> b

1

来自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.

1
请注意,这些将数字转换为Int32 -- 而几乎所有其他javascript中的内容都是基于64位双精度浮点数运作的。 - Sean McMillan

1

0

左移和右移运算符。如果你有一个数字,它将把它的位向左或向右移动。


0

这是位移运算符。请参阅此处以获取更多详细信息。


0

它们是位移运算符。计算机中的数字以二进制表示。向左移位相当于乘以2,向右移位相当于除以2。

例如,数字8在二进制中表示为1000。将其左移3位<<将得到1000000,即64。将其右移2位将得到10,即2。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接