Skip to main content
Tweeted twitter.com/#!/StackProgrammer/status/553049289912766464
reduced distraction possible by non-bool parameter
Source Link
Wolf
  • 640
  • 2
  • 6
  • 26

I stumbled about a Cppcheck warning (an inconclusive one), that I mistakenly used & instead of &&:

/// @param code identifies the command. Only the lower 16 bits of are being processed int encodeCmdState(bool finished, shortint paramcode, bool errerrorneous) { return (finished & 1) | ((paramcode & 0xffff)<<1) | ((err & 1)<< 17); } 

I'm not sure if this code (style) reaches back into times where bool wasn't available in C. My first idea would be to simply strip the & 1 normalization from the bool parts. But I want to be sure, so...

Is shifting bool values "portable" and "safe"? Or, more generally:

Does the implicit bool-to-int conversation in arithmetic expressions always normalize values?

I stumbled about a Cppcheck warning (an inconclusive one), that I mistakenly used & instead of &&:

int encodeCmdState(bool finished, short param, bool err) { return (finished & 1) | ((param & 0xffff)<<1) | ((err & 1)<< 17); } 

I'm not sure if this code (style) reaches back into times where bool wasn't available in C. My first idea would be to simply strip the & 1 normalization from the bool parts. But I want to be sure, so...

Is shifting bool values "portable" and "safe"? Or, more generally:

Does the implicit bool-to-int conversation in arithmetic expressions always normalize values?

I stumbled about a Cppcheck warning (an inconclusive one), that I mistakenly used & instead of &&:

/// @param code identifies the command. Only the lower 16 bits of are being processed int encodeCmdState(bool finished, int code, bool errorneous) { return (finished & 1) | ((code & 0xffff)<<1) | ((err & 1)<< 17); } 

I'm not sure if this code (style) reaches back into times where bool wasn't available in C. My first idea would be to simply strip the & 1 normalization from the bool parts. But I want to be sure, so...

Is shifting bool values "portable" and "safe"? Or, more generally:

Does the implicit bool-to-int conversation in arithmetic expressions always normalize values?

Source Link
Wolf
  • 640
  • 2
  • 6
  • 26

Is it always safe to shift bool values?

I stumbled about a Cppcheck warning (an inconclusive one), that I mistakenly used & instead of &&:

int encodeCmdState(bool finished, short param, bool err) { return (finished & 1) | ((param & 0xffff)<<1) | ((err & 1)<< 17); } 

I'm not sure if this code (style) reaches back into times where bool wasn't available in C. My first idea would be to simply strip the & 1 normalization from the bool parts. But I want to be sure, so...

Is shifting bool values "portable" and "safe"? Or, more generally:

Does the implicit bool-to-int conversation in arithmetic expressions always normalize values?