Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualBrother Bob

Posted 04 February 2014 - 11:27 AM

Although I share the concerns that everyone else has about the readability and maintainability of this code (I'd stick with something clear over something short)... it's also worth pointing out that pretty much every alternate form that people have posted does not have the same effect as the original code. If you do anything with id after the code that you posted, then the final value of id varies, depending on whether or not it had the Pickable flag set. (Highlighted is only cleared if id had any bit other than pickable). In fact, I'm beginning to seriously doubt that this code does what you think it does. What were you actually trying to do with this logic?

I actually thought so too that the solutions are different, until I realized that the if-statements clears a bit but checks the entire value and not just that cleared bit. The only way to break on the first of the three if statements is if no bits at all are set in the original value. You are right that highlighted is only cleared if something other than pickable is set, but if nothing else is set then highlighted cannot be set either so there's nothing to clear in the first place (nor is there anything that can be equal to 200).

 

Whether you clear them all at the same time, as MARS and iMalc suggested in their solutions, or one at a time as MARS did in his original code actually makes no difference. All codes effectively clear both bits and checks the remaining value for 200. The net result is the same in all cases, and that includes whether the last if-statement (=200) is take or not, and what the final value of id is.

 

In Krohm's variant though it makes a difference since it doesn't modify anything at all. And, of course, like most other here I prefer a non-modifying variant.


#1Brother Bob

Posted 04 February 2014 - 11:26 AM

Although I share the concerns that everyone else has about the readability and maintainability of this code (I'd stick with something clear over something short)... it's also worth pointing out that pretty much every alternate form that people have posted does not have the same effect as the original code. If you do anything with id after the code that you posted, then the final value of id varies, depending on whether or not it had the Pickable flag set. (Highlighted is only cleared if id had any bit other than pickable). In fact, I'm beginning to seriously doubt that this code does what you think it does. What were you actually trying to do with this logic?

I actually thought so too that the solutions are different, until I realized that the if-statements clears a bit but checks the entire value and not just that cleared bit. The only way to break on the first of the three if statements is if no bits at all are set in the original value. You are right that highlighted is only cleared if something other than pickable is set, but if nothing else is set then highlighted cannot be set either so there's nothing to clear in the first place (nor is there anything that can be equal to 200).

 

Whether you clear them all at the same time, as MARS and iMalc suggested in their solutions, or one at a time as MARS did in his original code actually makes no difference. All codes effectively clear both and checks the remaining ones for the value 200. The net result is the same in all cases, and that includes whether the last if-statement (=200) is take or nor, and what the final value of id is.

 

In Krohm's variant though it makes a difference since it doesn't modify anything at all. And, of course, like most other here I prefer a non-modifying variant.


PARTNERS