logo separator

[mkgmap-dev] Rounding bug in splitter?

From Marko Mäkelä marko.makela at iki.fi on Tue Aug 11 08:02:54 BST 2009

On Tue, Aug 11, 2009 at 06:52:35AM +0000, Chris Miller wrote:
> >> private int roundDown(int val) {
> >> return val >> SHIFT << SHIFT;
> >> }
> > I would write this as
> > 
> > return val & ~(1 << SHIFT);
> 
> That's not the same thing though. I assume you really mean:
> 
> return val & ~((1 << SHIFT) - 1);
> 
> And in fact that's what was there originally for positive numbers. Negative 
> numbers were handled differently, for reasons I don't understand (hence why 
> I'm asking on here).

Oh, sorry, you are right, I forgot the -1.

> I think I still prefer val >> SHIFT << SHIFT, it's clearer what's happening 
> (to me at least, I appreciate everything thinks about these things differently). 
> It's also fewer clock cycles unless you're on a 286 or lower ;) Though the 
> clock cycle argument isn't relevant here, the code is only called twice during 
> the entire split!

If that is the case, then it is better to aim for readability and
maintainability.  The bit-shifting expression should be easier to read
than the monster bit-masking expression.  And because you are shifting
the same amount right and left, it won't matter whether you are using
arithmetic or logical right shift (>>> or >> in Java).

Sorry for the noise.

	Marko



More information about the mkgmap-dev mailing list