-3

I am looking at some code in javascript

var numCombos = 1<<numActive;

numActive = 8 returns numCombos = 256

what does << means?

kkh
  • 4,799
  • 13
  • 45
  • 73

2 Answers2

4

It's the bitwise left operator. In a << b, it shifts a in binary representation b (< 32) bits to the left, shifting in zeros from the right.

Some examples:

a = 1       // 00000001 in binary
b = a << 1  // equals to 2, 00000010 in binary
c = a << 2  // equals to 4, 00000100 in binary

document.write('a << 1 = ' + b + '<br />'
               + 'a << 2 = ' + c);

This operator is kind of standard, and a little search should lead you to a lot of already existing topics on StackOverflow like this one: << operator in C++? or even this one: What are bitwise shift (bit-shift) operators and how do they work?

Community
  • 1
  • 1
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
3

It means bitwise left shift. Same as it means in most other programming languages.

Some console test:

>a = 2
2
>a << 1 
4
>a << 3
16
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175