1

Possible Duplicate:
Reference - What does this symbol mean in PHP?

What does the << mean in this line of PHP?

$count = (1 << $count_log2) - 1;
Community
  • 1
  • 1
eric
  • 1,433
  • 2
  • 18
  • 30

9 Answers9

4

this is the shift left operator.

so in your example you are shifting left the value 1 , $count_log2 times to the left. so the value is 2^count_log2.

1 in 8 bit binary is 00000001 so if $count_log2 = 4, we need to get 2^4 = 16.

shifting left means moving the 1 left 4 times (since $count_log2 = 4). lets do the steps.

  1. 00000010 (2 in decimal)
  2. 00000100 (4 in decimal)
  3. 00001000 (8 in decimal)
  4. 00010000 (16 in decimal)

so we got 2^4.

a common reason for using shift operation is that it takes less time for the processor to do a shift operation than using multiplication.

yossi
  • 12,410
  • 28
  • 80
  • 110
1

Left Bitshift, http://php.net/manual/en/language.operators.bitwise.php

plundra
  • 17,374
  • 3
  • 31
  • 26
1

It is a left bitshift operator. See the Bitwise Operators page of the PHP manual.

To quote the manual:

$a << $b - Shift left - Shift the bits of $a $b steps to the left (each step means "multiply by two")

In this specific case, $count = (1 << $count_log2) - 1 is the same as setting $count to pow(2, $count_log2) - 1

Reese Moore
  • 11,358
  • 3
  • 23
  • 32
1

<< and >> are the so-called bitshift operator.

x << n shifts the bits in the integer x n places to the left, effectively multiplying x with 2 to the power of n.

Similarly x >> n shifts to the left, dividing x by 2 to the power of n.

sepp2k
  • 353,842
  • 52
  • 662
  • 667
1

The << and >> are called Bitwise operators, they shift left and right respectively by a certain number of bits.

In your example: 1 << $count_log2 will shift the number 1 left by the value of $count_log2. This is easier to see in binary where the number 1 represented as an 8-bit number would be:

1 - 0000 0001

If you shift this number left by 3 (1 << 3) you would get 8:

8 - 0000 1000
Error 454
  • 7,181
  • 2
  • 31
  • 48
0

Shift left or shift right. See the manual on bitwise operators.

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
0

Its a bitwise operator for shifting bits to the left, this is not just php but many languages use this for if binary manipulation

http://php.net/manual/en/language.operators.bitwise.php

RobertPitt
  • 55,891
  • 21
  • 113
  • 158
0

<< is shift left operator in PHP

$a << $b means shift the bits of $a $b steps to the left (each step means "multiply by two")

shankhan
  • 5,963
  • 2
  • 17
  • 21
0

'>>' and '<<' are bitwise operators. '>>' shifts to the right, and '<<' shifts to the left.

Think of it like this, in binary 25 is 00011001. if you performed a shift left on 25, you'd have 00110010, which is 50.

If you performed a shift right on 50, you'd have 25.

rownage
  • 2,352
  • 3
  • 22
  • 31