Request for Comments: Supporting Binary Notation for Integers


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.


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.


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.

  • Scanner Changes–First, I propose adding a binary number representation (“0b”[01]+) to the scanner. Second, add the supporting code for handling binary conversions to long/double just as is done for hexadecimal. Lastly, add support for using the binary notation in variable offsets just as is done for the other representations.
  • Custom string-to-double Functions–Add another strtod function, zend_bin_strtod, which converts a number from base-2 representation to an integer. The supported syntax being valid base-2 digits [01] optionally prefixed by 0[bB].


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



rfc/binnotation4ints.txt · Last modified: 2011/08/06 21:29 by stas