PROPOSAL: Unsigned Integer Widening Operator

Bruce Chapman brucechapman at paradise.net.nz
Thu Mar 26 02:23:07 PDT 2009


Comments inline.


regards


Bruce



Reinier Zwitserloot wrote:
> Noel, Bruce:
>
> Perhaps Noel has a point, here. imagine:
>
> public class ByteUtils {
>      public int b2i(byte b) {
>          return b & 0xFF;
>      }
> }
>   

I wouldn't advise this, b2i is the JVM bytecode to convert a byte to an 
integer with sign extension, so to use it for a method that does zero 
extension would be a little confusing.  :)
> import static ByteUtils.b2i;
>
> results = (result << 8) | b2i(contents[i]);
>
> vs:
>
> results = (result << 8) | (+)contents[i];
>
>
> The second bit is hardly better, and it suffers from cast ambiguity if  
> you add more fluff to the expression. Once you also add parens to  
> localize the cast like so:
>
> results = (result << 8) | ((+)contents[i]);
>   

the (+) only applies to a byte or short, and the only way to have one of 
those is a variable or array access (with optional prefix or postfix 
increment or decrement) or explicitly downcasting, so in practice I 
don't think there would EVER be a situation where you would need (or 
even want) to wrap the (+) expression in parenthesis.
> The static import starts to win, in my book, and that's before  
> considering the impact of a language change.
>
>
>
> did you not read Bruce's introduction to his three proposals? It  
> provides some useful background information.
>
> If you've ever worked with bytes in java, you may remember that the  
> main issue is crippling wordiness. A librar
>   --Reinier Zwitserloot
> Like it? Tip it!
> http://tipit.to
>
>
>
> On Mar 25, 2009, at 11:30, Noel Grandin wrote:
>
>   
>> I would think that a utility class that performed widening operations,
>> and maybe some other useful stuff would be preferable to extending the
>> language/
>>
>> Perhaps a BinaryMath class to live alongside java.lang.Math?
>>
>> Regards, Noel.
>>
>> Bruce Chapman wrote:
>>     
>>> Title
>>> Unsigned Integer Widening Operator
>>>
>>> latest html version at http://docs.google.com/Doc?id=dcvp3mkv_2k39wt5gf&hl
>>>
>>> AUTHOR(S): Bruce Chapman
>>>
>>> OVERVIEW
>>>
>>>
>>>       
SNIP



More information about the coin-dev mailing list