Javascript神秘命令

时间:2014-11-19 14:13:05

标签: javascript command

我是JavaScript的新手,必须弄清楚程序的功能。在其中将两个整数与这个不等式进行比较:<< = 我不知道它是什么,我找不到任何告诉我的在线教程。 谢谢 Finlay Perry

2 个答案:

答案 0 :(得分:1)

它通过向左移动右边的值来更新左值。

var a = 1;
a <<= 2; // leftshift it by 2 bits,
         // in effect multiplying it by 4, making it 4

a  += 1; // a more common (familiar?) example of this kind of operator
         // add 1, making it 5

答案 1 :(得分:0)

这是带有赋值的javascript按位左移。 javascript按位运算符用法示例:

// helper function for displaing results
var output = function(operator, result) {
    console.log(operator + " " + result);
}

// variables
var a = 5;
var b = 13;

// a | b - OR
// if in any of the given numbers corresponding bit 
// is '1', then the result is '1'
output('or', a|b); // 13

// a & b - AND
// if in both of the given numbers corresponding bit 
// is '1', then the result is '1'
output('and', a&b); // 5 

// a ^ b - XOR
// if in one of the given numbers (not both) 
// corresponding bit is '1', then the result is '1'
output('xor', a^b); // 8

// ~a - NOT
// inverts all the bits
output('not', ~a); // -6

// a >> b - RIGHT SHIFT
// shift binary representation of 'a' for 'b' 
// bits to the right, discarding bits shifted off
output('rs', a>>b); // 0

// a << b - LEFT SHIFT
// shift binary representation of 'a' for 'b' 
// bits to the right, shifting in zeros from the right
output('ls', a<<b); // 40960

// a >>> b - ZERO FILLED RIGHT SHIFT
// shift binary representation of 'a' for 'b' 
// bits to the right, discarding bits shifted off, 
// and shifting in zeros from the left.
output('zfrs', a>>>b); // 0