什么做“>>”和“<<<<<在Javascript中意味着什么? [英] What do ">>" and "<<" mean in Javascript?

查看:142
本文介绍了什么做“>>”和“<<<<<在Javascript中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段我正在尝试理解的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 '>')

推荐答案

这些是右移(带标志) 向左移动 运营商。

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

基本上,这些运营商用于操纵BIT级别的值

它们通常与& 一起使用(按位AND )和 | (按位OR)运算符以及与掩码相关联的值,例如 0x7F 和类似的立即值找到问题的片段。

有问题的片段使用这些运算符来解析32位浮点值的三个分量(符号,指数和分数) 。

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).

例如,在问题的片段中:

1 - (2 *(b1& gt;> 7))产生整数值1或-1,这取决于b1变量中的位7(右起第8位)是零还是1


这个成语可以解释如下。

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.


  • 一开始,b1,表示为位是 0000000000000000abcdefgh

    请注意左边的所有位都是零,这来自

    b1 = data.charCodeAt(offset)& 0xFF 分配上面的几行,除了右边的8位(0xFF掩码)之外,它基本上将所有位中的所有位置零。

    a,b,c ... thru h表示未知的布尔值0或1.

    我们有兴趣测试a的值。

  • b1>> 7 将此值向右移动7位,将
    b1保留为 00000000000000000000000a ,作为整数读取将具有值1或0

  • 此1或0 整数值然后乘以2

    然后分别为2或0。

  • 然后从1减去此值,留下-1或1.

  • 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.

虽然有用的说明位运算符的工作方式,上面的习语可以用更直接测试位7的东西代替,并更明确地分配符号变量。此外,这种方法不需要对b1中最左边的位进行初始屏蔽:

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;

这篇关于什么做“&gt;&gt;”和“&lt;&lt;&lt;&lt;&lt;在Javascript中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆