PROPOSAL: Binary Literals

Reinier Zwitserloot reinier at zwitserloot.com
Wed Mar 25 14:40:24 PDT 2009


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