PROPOSAL Narrow Hexadecimal and Binary Integer Literals.

Bruce Chapman brucechapman at paradise.net.nz
Wed Mar 25 01:57:24 PDT 2009


Title Auto Typing Hexadecimal and Binary Integer Literals.


latest html version at http://docs.google.com/Doc?id=dcvp3mkv_125ww5tct

AUTHOR(S): Bruce Chapman

OVERVIEW


FEATURE SUMMARY:
Introduce two new forms of integer literals, for hexadecimal and binary 
that are typed as the minimum width integer that can accomodate the value.


MAJOR ADVANTAGE:

Allows for binary literals and allows for byte size and short size hex 
literals with a concise syntax.


 

    byte[] stuff = { 0x00, 0x7F, (byte)0x80,  (byte)0xFF};

can be recoded as

 

    byte[] ufum7 = { 0h00, 0h7F, 0h80, 0hFF };


MAJOR DISADVANTAGE:

Introduces a second form of hexadecimal literal with overlapping 
functionality.


ALTERNATIVES:

Introduce a new new integer type literal suffix for byte and short, and 
a new syntax for binary integers.

EXAMPLES
SIMPLE EXAMPLE:


    byte[] utf8bom7 = { 0hEF, 0hBB, 0b10111111 };


ADVANCED EXAMPLE:


    byte[] buffer = ...;

    if(buffer[i] == 0hBB) {

       ...
    }


DETAILS
SPECIFICATION:

JSL Section 3.10.1 needs considerable rewrite


An integer literal may be expressed in decimal (base 10), hexadecimal 
(base 16)(two ways), or octal (base 8), or binary (base 2):

    IntegerLiteral:
     FixedIntegerLiteral
     NarrowIntegerLiteral

    FixedIntegerLiteral:
     DecimalIntegerLiteral
     HexIntegerLiteral
     OctalIntegerLiteral

    NarrowIntegerLiteral:
            NarrowHexIntegerLiteral
            NarrowBinaryIntegerLiteral

    DecimalIntegerLiteral:
     DecimalNumeral IntegerTypeSuffixopt

    HexIntegerLiteral:
     HexNumeral IntegerTypeSuffixopt

    OctalIntegerLiteral:
     OctalNumeral IntegerTypeSuffixopt

    IntegerTypeSuffix: one of
     l L

    NarrowHexIntegerLiteral:
            0 h HexDigits
            0 H HexDigits

    NarrowBinaryIntegerLiteral:
            0 b BinaryDigits
            0 B BinaryDigits

    BinaryDigits:
            BinaryDigit
            BinaryDigit BinaryDigits

    BinaryDigit: one of
            0 1

An fixed integer literal is of type long if it is suffixed with an ASCII 
letter L or l (ell); otherwise it is of type int (§4.2.1). The suffix L 
is preferred, because the letter l (ell) is often hard to distinguish 
from the digit 1 (one).


 

A narrow integer literal has the smallest integer type able to 
accomodate the number of HexDigits or BinaryDigits specified, including 
leading zeros. For 1 or 2 HexDigits and for 1 through 8 binary digits 
the type is byte. For 3 or 4 Hex digits or from 9 through 16 binary 
digits the type is short. For 5 through 8 hex digits and for 17 through 
32 binary digits the type is integer and for 9 through 16 hex digits and 
33 through 64 binary digits the type is long. A compile time error 
occurs if more than 16 hex digits or 64 binary digits are specified. 
Narrow integer literals may not use an Integer type suffix, their type 
is determined by the number of digits.


A decimal numeral is either the single ASCII character 0, representing 
the integer zero, or consists of an ASCII digit from 1 to 9, optionally 
followed by one or more ASCII digits from 0 to 9, representing a 
positive integer:

    DecimalNumeral:
     0
     NonZeroDigit Digitsopt

    Digits:
     Digit
     Digits Digit

    Digit:
     0
     NonZeroDigit

    NonZeroDigit: one of
     1 2 3 4 5 6 7 8 9

A hexadecimal numeral consists of the leading ASCII characters 0x or 0X 
followed by one or more ASCII hexadecimal digits and can represent a 
positive, zero, or negative integer. Hexadecimal digits with values 10 
through 15 are represented by the ASCII letters a through f or A through 
F, respectively; each letter used as a hexadecimal digit may be 
uppercase or lowercase.

    HexNumeral:
     0 x HexDigits
     0 X HexDigits

    HexDigits:
     HexDigit
     HexDigit HexDigits



COMPILATION:

Two new forms of Integer literal need to be processed in scanner, and 
the sizing algorithm applied.

TESTING:

Exercise and check types and values of various narrow integer literals.

LIBRARY SUPPORT:

No library support required.

REFLECTIVE APIS:

None

OTHER CHANGES:

None - but it might be nice if the javadoc tool remembered the number 
base of Integer constants for constant fields and displayed them in that 
same base in the Constant Field Values list.

MIGRATION:

COMPATIBILITY
BREAKING CHANGES:

None

EXISTING PROGRAMS:

None

REFERENCES
EXISTING BUGS:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5025288

http://forums.sun.com/thread.jspa?forumID=316&messageID=2239381&threadID=480508


URL FOR PROTOTYPE (optional):
None



More information about the coin-dev mailing list