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-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: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: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: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!