PHP RFC: Normalize string bitwise shifts


This RFC proposes that the behaviour of bitwise shift operations on strings be altered to match the behaviour of bitwise shifts on integers.


Currently, bitwise shift operations on strings are performed by converting the string to an integer and returning an integer. This behaviour does not match that of the other bitwise operators when they are used with string operands, as these perform the operation on a byte-by-byte basis, and return a string.

This RFC proposes that the behaviour of the bitwise shift operators be altered, such that when the left operand is a string the shift operation is performed on the byte values of the string's characters, carrying bits between bytes as necessary. The will effectively treat the string as an arbitrarily large integer.

TODO: Add some example use cases

Backward Incompatible Changes

Existing code that relies on the current behaviour will no longer produce the originally expected result. This can be easily resolved by explicitly casting the left operand to an integer.

Proposed PHP Version(s)

As this is a backwards-incompatible change, this RFC targets PHP 6.

Open Issues

Should right shifts shift copies of the left-most bits on, or always shift cleared bits?

Unaffected PHP Functionality

Only bitwise shifts in user land where the left operand is a string are affected.

Proposed Voting Choices

Yes or no.

Patches and Tests

A WIP patch can be seen on Github.


Rejected Features

None so far.

rfc/string-bitwise-shifts.txt · Last modified: 2021/03/27 14:34 by ilutov