C++ “OR” operator

后端 未结 9 1597
无人及你
无人及你 2021-01-19 03:37

can this be done somehow?

if((a || b) == 0) return 1;
return 0;

so its like...if a OR b equals zero, then...but it is not working for me.

9条回答
  •  清歌不尽
    2021-01-19 04:07

    If you have lot of that code, you may consider a helping method:

    bool distanceLE (Point p1, Point p2, double threshold) {
        return (p1.distanceFrom (p2) <= threshold)
    }
    
    bool Circle2::contains (Line2 l) {
        return distanceLE (p1, l.p1, r) && distanceLE (p1, l.p2, r);
    }
    

    If you sometimes have <, sometimes <=, >, >= and so on, maybe you should pass the operator too, in form of a function.

    In some cases your intentions by writing this:

    if ((a || b) == 0) return 1;
    return 0;
    

    could be expressed with an bitwise-or:

    if ((a | b) == 0) return 1;
    return 0;
    

    and simplified to

    return  ! (a | b);
    

    But read up on bitwise operations and test it carefully. I use them rarely and especially I didn't use C++ for some time.

    Note, that you inverted the meaning between your examples 1 and 2, returning true and false in the opposite way.

    And bitwise less-equal doesn't make any sense, of course. :)

提交回复
热议问题