Implementing logical right shifting in PHP

PHP’s bitwise operators << and >> implement Arithmetic Bit Shifting. This means:

  • Left shift (using << operator) fills in zero(s) on the right end of the bitfield, and bit(s) shifted off the left end are discarded;
  • Right shift (using >> operator) discards bit(s) shifted off the right end of the bitfield and copy(ies) of sign bit are shifted in on the left end.

Logical Bit Shifting is different in that on right shifts, zeros are shifted in on the left end of the bitfield, affecting the sign of negative integer.


Finding Even or Odd numbers with PHP

PHP does not provide any mathematical function like is_even to help you check if a given integer is an Even number or Odd. Writing your own such function is not difficult and below are listed a number of alternatives that provide different logical solutions to do this.