Topic: Binary constants


Author: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
Date: Mon, 22 Mar 1993 11:23:55 GMT
Raw View
A proposal to allow Binary Literals.
------------------------------------

  John Max Skaller
  Internet: maxtal@extro.ucc.su.oz.au
  Compuserve: 100236,1703 (100236.1703@compuserve.com from internet)

  WG21

  21/3/92

  Abstract
  --------

  Binary integral and character literals of the form

    0b0100101        0b11011u       0b00100L
    '\y01001'        L'\y00101000'
    "ab\y10100cd"    L"ab\y11011cd"

  are allowed to supplement hexadecimal literals for low
  level programming.


  Goal
  ----

    C and C++ are often used for machine level programming.
    It would be a great convenience to developers and maintainers
    to be able to specify constants in binary.

    The specifications of machine level facilities
    such as the layout of IO ports
    is often given in reference manuals for the hardware as
    a binary layout.

    Converting this to hexadecimal is error prone, and it is not
    easy to verify the correctness of hexadecimal constants
    against binary specifications.

    Furthermore, while programming at a low level one often thinks
    in terms of bits and bit patterns.

    In addition, binary constants may be useful when dealing
    with packed information.

  Proposal.
  --------

    The format for a binary integral constant is proposed as

      0b01000101

    and for characters

      '\y0100101'

    This proposal automatically allows the binary escape in
    string literals according to 2.9.4.

      "ab\y1010010cdef"

  Compatability
  -------------

    No conforming C code will be broken by this proposal.

    No existing C++ will be broken by this proposal.


  Explanation of choice of lexicology
  -----------------------------------
    For characters:

    Note that the use of '\z' where 'z' is not one of the
    defined characters (ntvbrfa\?'"x0123456789) is
    currently undefined.

    The choice of y (as in binar*y*) was made because:

    y is close to x(hex) in the alphabet

    b,n,r are reserved for character constants for backspace,
      newline and return

    a and b are also a hex digits.

    That left 'i' and 'y', of which I think 'y' is better.

   For the integral constants, the choice of b seems most natural.
   The format 0y110101 was considered (and actually proposed),
   but I was easily persuaded to change my mind. Apparently
   Zortech supports this format.

  Explanation of no binary IO
  ---------------------------

    There is no proposal to provide a binary facility
    for the printf or related functions. Hex input and output
    is available, and it is easy to code a function that
    does binary I/O. Such a facility would break backwards
    compatibility with the standard C library functions.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@extro.ucc.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA