7

Why does the following code output 128?

<?php 
    print 4 << 5; 
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yogeshK
  • 195
  • 4
  • 19

2 Answers2

15

Because it's a bitwise operator. I think it means 4 multiplied to 2^5 because that operator means

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

so it's five steps. It's 4 * 2 * 2 * 2 * 2 * 2 (But I'm guessing here; everything happens at bit level).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
15

Refer to Bitwise Operators:

We have to find 4 << 5. It means shift the bits of 4 5 times to the left:

4 is 00000000000000000000000000000100
4 << 5 after shifting is 00000000000000000000000010000000
00000000000000000000000010000000 is 2^7 = 2*2*2*2*2*2*2 = 128
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101