This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Bit Operator

Hi ,

A "bit" of a fundamental query.

Suppose I declare two bit variables
bit MyBit1,MyBit2;

I need to do something if both bits are 1.

Are these two methods equivalent:

if(MyBit1 & MyBit2) // Logical &
{ do something....
}

if(MyBit1 && MyBit2) // Conditional &&
{ do something....
}

Thomas

  • "Thanks" to you choosing to make a secret out of what platform you're talking about, and by implication, what "bit" actually is, it's impossible to answer that.

    Anyway, it doesn't matter. Write what you mean, and let the compiler handle the rest. In the case at hand, that would be the '&&'

  • Please observe the instructions for posting source code:

    www.danlhenry.com/.../keil_code.png

    if( MyBit1 && MyBit2 ) // Conditional &&
    {
       do something....
    }
    

    The correct term there is bitwise

  • "The correct term there is bitwise"

    I think you copied the wrong code block there, but that was easy to do since the original code used "conditional" about the logical test and and "logical" about the bitwise test. ;)

    & is bitwise

    && is logical

    When requiring multiple bool variables to be true (any implementation of bool, including the single-bit variables of the 8051) then it is a logical decision so && should be used.

    When performing tests of individual bits of an integer, then the bitwise & operator should be used.

    & extracts one or more bits from an integer.

    && requires multiple logical expressions to be true.

    The word "conditional" is related to the if statement itself, where the if statement evaluates the result of an expression to decide on the conditional execution.