PROPOSAL: Binary Literals
Derek Foster
vapor1 at teleport.com
Wed Mar 25 20:49:28 PDT 2009
In response to your questions about underscores... At least as they are used in Ruby, the programmer is allowed to put underscores wherever he/she wants to in a number as long as they are not at the front of it. Presumably, an underscore proposal for Java would allow the same flexibility. The underscores are intended to increase readability, rather than to provide automated error-checking.
Thus, if you live in a country that puts commas every three digits in decimal numbers for clarity, you can do this:
int tenBillion = 10_000_000_000;
If you want to put underscores every eight digits, four digits, or three digits in your binary numbers, you can. You can even use them to separate bitfields of varying sizes within the same number:
int bitmask1 = 0b000_00_111;
int bitmask2 = 0b000_11_000;
int bitmask3 = 0b111_00_000;
int number = 0b101_01_010;
or in hex numbers for that matter:
int bitmask1 = 0x00_000_FFF;
int bitmask2 = 0x00_FFF_000;
int bitmask3 = 0xFF_000_000;
int number = 0x13_254_232;
Derek
-----Original Message-----
>From: Reinier Zwitserloot <reinier at zwitserloot.com>
>Sent: Mar 25, 2009 5:40 PM
>To: brucechapman at paradise.net.nz
>Cc: coin-dev at openjdk.java.net
>Subject: Re: PROPOSAL: Binary Literals
>
>On Mar 25, 2009, at 22:02, brucechapman at paradise.net.nz wrote:
>
>> code extracts allows you to see most easily that bits 3,4 and 5 are
>> being extracted?
>>
>> (b & 0b00011100) >> 2
>>
>> (b & 0x1C) >> 2
>>
>
>Maybe it's my assembler days, but, seriously:
>
>The hexadecimal one.
>
>In your example above there's no real difference, they are equally
>readable to me. But here are some other examples:
>
>0b10000000 vs. 0x80
>0b100000000 vs 0x100
>0b10000101 vs 0x85
>
>No contest, hex wins hands down, with a big margin the first two
>margins, and still a winner in the third case, because 5 zeroes
>already become hard to count at a casual glance. The amount of digits
>involved in writing out a mask in binary just overwhelms the senses;
>your average human brain has a hard time processing more than 7 things
>at once, which means even a simply byte is already too large.
>Underscores would indeed help some, but what if I want to underscore
>around a nibble? Is that allowed? But clearly an underscore after 9
>digits ought to give me a warning or error, or the underscores are
>only going to make mistakes permanent. Should they mandatory? If not,
>trying to decade 0x100 written as a binary literal is still going to
>be very painful.
>
>To summarize: A bitmask might marginally be more readable in rare
>cases, especially to the casual programmer who doesn't have much
>experience with bit-level trickery, which is small comfort, because
>bitmasking comes up very rarely in java code in the first place. In
>most other situations, binary literals are much worse compared to hex
>literals.
>
>Sign me up for bruce's 0h notation to get around the need to cast-to-
>byte for 0x80 and up. That would serve all my bitmasking needs.
>
> -- Reinier Zwitserloot
>
>
More information about the coin-dev
mailing list