venta: (Default)
[personal profile] venta
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.

Date: 2010-07-12 03:24 pm (UTC)
From: [identity profile] beckyc.livejournal.com
If it makes you feel any better, today I spent half an hour wondering why a function always returned false. Then I noticed that on Friday, I had commented out a line of code. Duh!

Date: 2010-07-12 03:44 pm (UTC)
From: [identity profile] boyofbadgers.livejournal.com
If it's any consolation, I suspect most C, C++ or Java programmers have done the same thing at some point. I know I have, and am super assiduous about bracketing bitshifts as a result. Not that I end up using them that often, which I suspect is the source of the problem in the first place.

Date: 2010-07-12 05:12 pm (UTC)
From: [identity profile] bateleur.livejournal.com
No, you're right, it's counter-intuitive.

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!

Date: 2010-07-12 06:07 pm (UTC)
ext_8103: (Default)
From: [identity profile] ewx.livejournal.com
Your compiler doesn’t warn you about that one?

If it isn't in BODMAS, be afraid.

Date: 2010-07-12 06:47 pm (UTC)
From: [identity profile] fractalgeek.livejournal.com
Brackets are your friend.

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.
Edited Date: 2010-07-12 09:17 pm (UTC)

Date: 2010-07-13 12:47 pm (UTC)
From: [identity profile] d-floorlandmine.livejournal.com
Glad I've never had to fuss with bitshifts.

Then again, I'm mostly working with Access and VB ... so I can generate my own cockups.

Profile

venta: (Default)
venta

December 2025

S M T W T F S
 123456
78910111213
14151617181920
212223 24252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 27th, 2025 07:58 am
Powered by Dreamwidth Studios