15

Consider:

echo 50 >> 4;

Output:

3

Why does it output 3?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user198729
  • 61,774
  • 108
  • 250
  • 348

8 Answers8

33

50 in binary is 11 0010, shift right by 4 yields 11 which is equal to 3.

See PHP documentation and Wikipedia.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nevets1219
  • 7,692
  • 4
  • 32
  • 47
  • 7
    Bitwise operator. Specifically, arithmetic shift right bitwise operator. – webbiedave May 07 '10 at 17:20
  • Since you are dealing with PHP, you may look at additional information about it here (http://php.net/manual/en/language.operators.bitwise.php). The wiki page is also relevant (http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts) – nevets1219 May 07 '10 at 17:22
  • 1
    somewhat related? [c++ doc on bitwise operators](http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/) – Félix Adriyel Gagnon-Grenier Nov 18 '16 at 05:41
22

As documented on php.org, the >> operator is a bitwise shift operator which shifts bits to the right:

$a >> $b - Shift the bits of $a $b steps to the right (each step means "divide by two")

50 in binary is 110010, and the >> operator shifts those bits over 4 places in your example code. Although this happens in a single operation, you could think of it in multiple steps like this:

  • Step 1 - 00011001
  • Step 2 - 00001100
  • Step 3 - 00000110
  • Step 4 - 00000011

Since binary 11 is equal to 3 in decimal, the code outputs 3.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • You may want to right-pad those with 0s to make it more clear (while technically the same, it looks like the bits are not shifting, but rather dropping from the right side). – Synetech May 07 '10 at 17:24
  • Thanks for the feedback - just I updated the answer to clarify both points. – Justin Ethier May 07 '10 at 17:28
  • 1
    +1 , Best explanation for someone who has never explored bitwise operations. – Tim Post May 07 '10 at 17:39
3

>> is the binary right-shift operator.

Your statement shifts the bits in the numeric value 50 four places to the right. Because all integers are represented in two's complement, this equals 3. An easy way to remember this is that one shift to the right is the same as dividing by 2, and one shift to the left is the same as multiplying by 2.

Geoff
  • 7,935
  • 3
  • 35
  • 43
driis
  • 161,458
  • 45
  • 265
  • 341
3

The >> operator is called a binary right shift operator.

Shifting bits to the right 4 times is the same as dividing by two, four times in a row. The result, in this case would be 3.125. Since 50 is an int, bit shifting will return the floor of this, which is 3.

Put another way, 50 is 0b110010 in binary. Shifted 4 times we have 0b11, which is 3 in decimal.

Geoff
  • 7,935
  • 3
  • 35
  • 43
3

Arithmetic shift right.

1

It shifts the bits down four places.

50 in binary is 110010.

Shifted down four places is 11, which is 3.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt Greer
  • 60,826
  • 17
  • 123
  • 123
1

It's called a right shift. 'The bits of the left operand are shifted right by the number of positions of the right operand. The bit positions vacated on the left are filled with the sign bit, and bits shifted out on the right are discarded.'

Information can be found on it here: http://php.comsci.us/etymology/operator/rightshift.php

bradenkeith
  • 2,397
  • 1
  • 17
  • 26
1

For your convenience, one of the fastest ways to calculate the outputted value from a bitwise shift is to multiply or divide by 2.

For example echo 50 >> 4;

Given that this is a bitwise right, it literally means that the value will be decrease, then we can get the output by divide 50 for 2 and 4 times.

echo 50 >> 4; // 50/(2*2*2*2) ~ 3.

Given that (from) 48 -> (to) 63/16(2*2*2*2), the result will be more than 2 and less than 4. Then

echo 48 >> 4; // 48/(2*2*2*2) ~ 3.

echo 63 >> 4; // 63/(2*2*2*2) ~ 3.

However, when bitwise left, the result will be totally different as it multiplies by 2 with n times:

If echo 50 << 4; // 50*(2*2*2*2) ~ 800

If echo 51 << 4; // 51*(2*2*2*2) ~ 816

Live example: https://3v4l.org/1hbJe

Vuong
  • 31
  • 1