|
Quick Lists
|
|
Bug ID:
|
5025288
|
|
Votes
|
2
|
|
Synopsis
|
Language support for literal numbers in binary and other bases
|
|
Category
|
java:specification
|
|
Reported Against
|
1.4.2
, merlin-beta
|
|
Release Fixed
|
|
|
State
|
6-Fix Understood,
request for enhancement
|
|
Priority:
|
4-Low
|
|
Related Bugs
|
4495744
|
|
Submit Date
|
01-APR-2004
|
|
Description
|
A DESCRIPTION OF THE REQUEST :
It's alway struck me as strange that there is no way of writing binary numbers in Java (or C++). Many languages have a syntax for binary literals.
Can I suggest a minor language change allowing for numbers in any base, using a simple syntax such as 2_10111. In practise this would mostly be used for binary, but would be occasionally useful for other bases up to 36.
JUSTIFICATION :
Argument in favour:
It is still a common programming technique to use sets of flags, indeed this is a major use of the forthcoming enum feature.
Flags in hex and decimal are difficult to read, and particularly when a number contains more than one flag. Who could read 0xD9 at a glance? The clearest current representation would be
(1<<7) + (1<<6) + (1<<4) +(1<<3) + (1<<0)
which is clumsy and error-prone, unlike (say) 2_11011001.
Another benefit: writing a set of flags as (say) 2_11011001 clearly expresses that the number is to be interpreted as flags, rather than a true integer.
Another minor benefit: Java's leading-zero octal notation is very unclear and it's easy to switch to octal by accidental as a result of a typo. It could be deprecated (maybe even producing a compiler warning!) in favour of something like 8_...
Argument against: there is resistance to adding to the language in case it increases complexity. Fair enough, but I think this addition actually reduces complexity.
CUSTOMER SUBMITTED WORKAROUND :
Use hex, octal or decimal to express other bases.
(Incident Review ID: 201283)
======================================================================
Posted Date : 2006-10-27 15:04:29.0
|
|
Work Around
|
N/A
|
|
Evaluation
|
This is syntactic sugar, but a nice feature for beginners and experts alike. Other bases and the proposed <base>_<literal> syntax seem overkill.
A binary literal would be unsigned, so that 0b1111111 means 127 and not -1 as in two's-complement. Ideally, the largest binary literal would be 0b11111111111111111111111111111111 (32 bits).
Posted Date : 2006-12-07 23:20:53.0
|
|
Comments
|
PLEASE NOTE: JDK6 is formerly known as Project Mustang
|
|
|
 |