Table of Contents

Request for Comments: Supporting Binary Notation for Integers

Introduction

The sole purpose of this proposal is to extend PHP's syntax to support binary notation for integers just as it already does for decimal, octal, and hexadecimal representations. By its addition, developers would be permitted to define integer values in base-2, which is a convenient notation many other languages are now supporting.

History

As most developers know, PHP already supports octal and hexadecimal as alternatives to standard decimal notation. As such, a developer can currently define an integer value of 2010 using 2010 (base 10), 0x7da (base 16), and 03732 (base 8).

An example of using hexadecimal notation is for bitmasks, where flag values are commonly defined using hexadecimal notation (0x01, 0x02, 0x04, 0x08, 0x10, etc.) While this works, it isn't visually representative of the bitmask which, in addition to being just plain cool, is one of the reasons several of the newer languages have incorporated binary notation support--support that allows developers to specify the value 2010 as 0b11111011010 or flags as 0b00001, 0b00010, 0b00100, 0b01000, 0b10000.

Proposal

Given that binary notation is syntactic sugar, the proposed implementation appears to be relatively minor. The areas requiring changes are the scanner and custom string-to-double functions.

Patch

Preliminary source and documentation patches can be found with the ticket http://bugs.php.net/bug.php?id=50648

References

Changelog

implemented with http://svn.php.net/viewvc?view=revision&revision=313756