Note to self:
3 + 8 << 2
is not the same as
3 + (8 << 2)
That's an hour you just spent tracking down a bug based on a mistake you've made several times before.
Yes, I know, brackets. Normally I'm assiduous about brackets. But for short lines like, say,
3 + 8*2
I actually think they detract from readability.
I'm sure there are sound reasons why shift has precedence over addition in C, but I find it counter-intuitive in the extreme.
Maybe a nice dose of online mockery will help me remember in the future :(
Note to non-geeks:
Don't worry, not much to see here. I made a school-boy error.
3 + 8 << 2
is not the same as
3 + (8 << 2)
That's an hour you just spent tracking down a bug based on a mistake you've made several times before.
Yes, I know, brackets. Normally I'm assiduous about brackets. But for short lines like, say,
3 + 8*2
I actually think they detract from readability.
I'm sure there are sound reasons why shift has precedence over addition in C, but I find it counter-intuitive in the extreme.
Maybe a nice dose of online mockery will help me remember in the future :(
Note to non-geeks:
Don't worry, not much to see here. I made a school-boy error.
no subject
Date: 2010-07-12 03:24 pm (UTC)no subject
Date: 2010-07-12 03:44 pm (UTC)no subject
Date: 2010-07-12 05:12 pm (UTC)I have a natural defence against making this error, though. I am so bad at remembering precedence that the version of the line without parentheses reads like an error to me purely for not having them!
no subject
Date: 2010-07-12 06:07 pm (UTC)If it isn't in BODMAS, be afraid.
Date: 2010-07-12 06:47 pm (UTC)I believe the logic is, twiddling bits is considered higher priority than assembling them into a bitmask. So build your mask, shift it into position, then OR the mask together.
no subject
Date: 2010-07-13 10:39 am (UTC)3 + 2*4 != 3+2<<2
Would you bracket
x = 3 + 2*4
?
no subject
Date: 2010-07-13 10:41 am (UTC)I concede that the someone-else's-code I'm porting generates so many warnings that I may have missed it, though. I really must do that trawl through and try to obliterate the warnings for fear I'm missing useful ones :(
Re: If it isn't in BODMAS, be afraid.
Date: 2010-07-13 10:41 am (UTC)no subject
Date: 2010-07-13 10:44 am (UTC)(I think this error was indeed introduced because I changed a *7 to a *8, and instinctively altered it to a <<3. Which I'm quite sure our compiler is smart enough to do for me, anyway.)
no subject
Date: 2010-07-13 10:46 am (UTC)Depends on the circumstances. I wouldn't in a case like:
position + 4*increment...but I would for something like:
(width1 * height1 * depth1) + (width2 * height2 * depth2)The approximate rule being something to do with the length of the resulting term rather than anything intrinsic to the mathematics. (Note also my use of spaces to imply the expected precedence, as you have done in some cases too.)
no subject
Date: 2010-07-13 10:47 am (UTC)no subject
Date: 2010-07-13 10:48 am (UTC)If I could remember not to treat it the same, I wouldn't be in this mess ;) As it is, my shift was a very short line, so just got spaces to clarify its intended operation.
no subject
Date: 2010-07-13 10:49 am (UTC)no subject
Date: 2010-07-13 10:57 am (UTC)Of course, this does mean I'm vulnerable to messing up the precedence of something like foo^2, but fortunately it both binds very tightly and is seldom used!
no subject
Date: 2010-07-13 11:02 am (UTC)Interesting. I'd never really thought about the choice of syntax indicating subtle meaning to future readers. So much more sophisticated than comments ;)
no subject
Date: 2010-07-13 12:47 pm (UTC)Then again, I'm mostly working with Access and VB ... so I can generate my own cockups.