C ++: assigning bitwise AND result to bool

I have the following loop in C ++ compiled with g ++ 4.1.2:

while(1) { int status = getStatus(); bool firstOk = status & 0x1; bool secondOk = status & 0x2; if(firstOk != m_firstOk) { logStatus(1, firstOk); m_firstOk = firstOk; } if(secondOk != m_secondOk) { logStatus(2, secondOk); m_secondOk = secondOk; } sleep(1); } 

Note logStatus () takes its parameters by value, so the parameter does not change. m_firstOk and m_secondOk are, of course, bool member attributes.

This has worked so far. I got a message that it did not detect when firstOk changed. I connected to the running process using gdb. This was in the sleep () line, and I was surprised to see the following:

 (gdb) p m_firstOk $1 = true (gdb) p m_secondOk $2 = true (gdb) p firstOk $3 = 244 

WTF? How can firstOk be 244 when it should be the result of bitwise AND with 0x1? I know that a boolean is actually stored as an integer, but how is it bitwise AND ignored? Since it is 244, it is judged to be true when it must be false, which is the cause of the problem.

Doesn't assign a bitwise AND logical safe result? Is this a gcc error? Or do I need to do something like the following?

 bool firstOk = (status & 0x1) ? true : false; 

Thanks in advance.

+4
source share
3 answers

The local variables firstOk and secondOk are not "live" when you reach the sleep() call, so even if they were allocated by the stack slots, it is quite possible (and quite possibly) that their values โ€‹โ€‹arenโ€™t stored anywhere else .

If you need to debug one of these variables, you need to either

  • Re-declare them as static , which will allocate their persistent storage from the stack
  • Move the declaration firstOk and secondOk to the outside area. (Note that this may not be enough if you do not move them to the scope.)
  • Copy the values โ€‹โ€‹of firstOk and secondOk into constant variables or variables located in the external area and check them.

In any case, after completing the debugging process, I would return any of the above debugging measures. :-)

As for your last question, the statement bool firstOk = status & 0x1 fine, as in the next statement, setting secondOk . Assigning int a bool results in null / nonzero values โ€‹โ€‹to false and true .

As for your actual error (somehow you missed the switch to firstOk ), I donโ€™t see where you could lose it in this code. This piece of code seems beautiful. Is it possible that your getStatus() function should be called more often than once per second? Can anything else be written in m_firstOk or m_secondOk ? Their declarations are not shown, therefore, apparently, they exist in the external sphere.

+2
source

The only way that is possible is:

1) the stack was somehow overwritten.

2) The value of the temporary value is not so important as soon as you get on the waiting line, so the compiler no longer tracks it, so the memory slot can be used for something else!

If it works first and then starts to work incorrectly, most likely this is not a compiler error for this section of code.

0
source

The correct appointment should be:

 bool firstOk = (status & 0x1) == 0x01; 

Or even:

 bool flag = (value & mask) == mask; 
-1
source

All Articles