Equivalence in use of bitwise | operator and + operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ioannis Vranos

    #1

    Equivalence in use of bitwise | operator and + operator

    Well when we have an expression like this:

    int obj= first | second;


    Why is this style preferred than the equivalent:


    int obj= first+ second; ?

  • Pavel Lepin

    #2
    Re: Equivalence in use of bitwise | operator and + operator


    Ioannis Vranos <ivranos@nospam .no.spam.freema il.grwrote in
    <gfk56c$2uno$1@ ulysses.noc.ntu a.gr>:
    Well when we have an expression like this:
    >
    int obj= first | second;
    >
    Why is this style preferred than the equivalent:
    >
    int obj= first+ second; ?
    #include <iostream>
    #include <ostream>
    int main()
    {
    int a = 1, b = 3;
    std::cout << (a + b) << std::endl
    << (a | b) << std::endl;
    }

    Does this answer your question?

    --
    If we want the average quality of computer programs to rise
    by 1000%, all we have to do is carefully to select 90% of
    the world's programmers, and shoot them. --Richard
    Heathfield in <Sfedncgr46kOPm DanZ2dnUVZ8vCdn Z2d@bt.com>

    Comment

    • maverik

      #3
      Re: Equivalence in use of bitwise | operator and + operator

      On Nov 14, 6:23 pm, Ioannis Vranos
      <ivra...@nospam .no.spam.freema il.grwrote:
      Well when we have an expression like this:
      >
      int obj= first | second;
      >
      Why is this style preferred than the equivalent:
      >
      int obj= first+ second; ?
      Hmm... o.O
      It's not equivalent operators.
      The | operator is bitwise OR operator, and + is an addition opeartor.
      Once more, it's not equivalent operators (see Pavel's post for
      example).

      Comment

      • Pete Becker

        #4
        Re: Equivalence in use of bitwise | operator and + operator

        On 2008-11-14 10:23:55 -0500, Ioannis Vranos
        <ivranos@nospam .no.spam.freema il.grsaid:
        Well when we have an expression like this:
        >
        int obj= first | second;
        >
        >
        Why is this style preferred than the equivalent:
        >
        >
        int obj= first+ second; ?
        They're equivalent if first and second have no common bits set.
        Otherwise, they're different. Once you start combining flag values with
        multiple bits set you need to use |, so it's clearer to use | all the
        time.

        --
        Pete
        Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
        Standard C++ Library Extensions: a Tutorial and Reference
        (www.petebecker.com/tr1book)

        Comment

        • Ioannis Vranos

          #5
          Re: Equivalence in use of bitwise | operator and + operator

          Pete Becker wrote:
          On 2008-11-14 10:23:55 -0500, Ioannis Vranos
          <ivranos@nospam .no.spam.freema il.grsaid:
          >
          >Well when we have an expression like this:
          >>
          >int obj= first | second;
          >>
          >>
          >Why is this style preferred than the equivalent:
          >>
          >>
          >int obj= first+ second; ?
          >
          They're equivalent if first and second have no common bits set.
          Otherwise, they're different. Once you start combining flag values with
          multiple bits set you need to use |, so it's clearer to use | all the time.

          Yes I was talking about the flags situations, where | is used to add two
          flags (with different 1 bits) . Using the + operator makes more sense
          for me.


          For example (from Qt):

          model->setSorting(Qdi r::DirsFirst | Qdir::IgnoreCas e | QDir::Name);



          I think

          model->setSorting(Qdi r::DirsFirst + Qdir::IgnoreCas e + QDir::Name);

          is more obvious.

          Comment

          • .rhavin grobert

            #6
            Re: Equivalence in use of bitwise | operator and + operator

            On 14 Nov., 19:22, Ioannis Vranos <ivra...@nospam .no.spam.freema il.gr>
            wrote:
            Pete Becker wrote:
            On 2008-11-14 10:23:55 -0500, Ioannis Vranos
            <ivra...@nospam .no.spam.freema il.grsaid:
            >
            Well when we have an expression like this:
            >
            int obj= first | second;
            >
            Why is this style preferred than the equivalent:
            >
            int obj= first+ second; ?
            >
            They're equivalent if first and second have no common bits set.
            Otherwise, they're different. Once you start combining flag values with
            multiple bits set you need to use |, so it's clearer to use | all the time.
            >
            Yes I was talking about the flags situations, where | is used to add two
            flags (with different 1 bits) . Using the + operator makes more sense
            for me.
            >
            For example (from Qt):
            >
            model->setSorting(Qdi r::DirsFirst | Qdir::IgnoreCas e | QDir::Name);
            >
            I think
            >
            model->setSorting(Qdi r::DirsFirst + Qdir::IgnoreCas e + QDir::Name);
            >
            is more obvious.
            No, it is not. the |-approach tells everyone who reads your code "ah,
            flags!" while the second way may be ranged-values or strings/chars or
            whatever on first view, because | is an "and" while + is an "increase
            by".

            Comment

            • Andrey Tarasevich

              #7
              Re: Equivalence in use of bitwise | operator and + operator

              Ioannis Vranos wrote:
              >
              Yes I was talking about the flags situations, where | is used to add two
              flags (with different 1 bits) . Using the + operator makes more sense
              for me.
              >
              For example (from Qt):
              model->setSorting(Qdi r::DirsFirst | Qdir::IgnoreCas e | QDir::Name);
              >
              I think
              model->setSorting(Qdi r::DirsFirst + Qdir::IgnoreCas e + QDir::Name);
              is more obvious.
              If you find it "more obvious" - great, use it. But you have to know what
              you are doing, and keep in mind the inherent dangers of this approach.
              I'd say that using '+' as an equivalent of '|' always requires an
              assertion that verifies that the flags are indeed independent

              STATIC_ASSERT(
              (Qdir::DirsFirs t & Qdir::IgnoreCas e) == 0 &&
              (Qdir::DirsFirs t & QDir::Name) == 0 &&
              (Qdir::IgnoreCa se & QDir::Name) == 0);

              or literally

              STATIC_ASSERT(Q dir::DirsFirst + Qdir::IgnoreCas e + QDir::Name ==
              Qdir::DirsFirst | Qdir::IgnoreCas e | QDir::Name);

              (or a run-time assertion at least).

              Of course, it might be sufficient to place such an assertion just once
              in some place in the code, but this, in my opinion, is too high a price
              to pay for your "obviousnes s" (which I personally don't really see).
              While trying to do it without any safeguards is asking for trouble.

              --
              Best regards,
              Andrey Tarasevich

              Comment

              • peter koch

                #8
                Re: Equivalence in use of bitwise | operator and + operator

                On 14 Nov., 19:22, Ioannis Vranos <ivra...@nospam .no.spam.freema il.gr>
                wrote:
                Pete Becker wrote:
                On 2008-11-14 10:23:55 -0500, Ioannis Vranos
                <ivra...@nospam .no.spam.freema il.grsaid:
                >
                Well when we have an expression like this:
                >
                int obj= first | second;
                >
                Why is this style preferred than the equivalent:
                >
                int obj= first+ second; ?
                >
                They're equivalent if first and second have no common bits set.
                Otherwise, they're different. Once you start combining flag values with
                multiple bits set you need to use |, so it's clearer to use | all the time.
                >
                Yes I was talking about the flags situations, where | is used to add two
                flags (with different 1 bits) . Using the + operator makes more sense
                for me.
                >
                For example (from Qt):
                >
                model->setSorting(Qdi r::DirsFirst | Qdir::IgnoreCas e | QDir::Name);
                >
                I think
                >
                model->setSorting(Qdi r::DirsFirst + Qdir::IgnoreCas e + QDir::Name);
                >
                is more obvious.
                No it is not. First of all, how do you know it works? That would
                require that the different elements did not have any bits in common -
                in this or a future version of the library. Secondly, as said
                elsewhere, the signalling vaule is wrong.

                /Peter

                Comment

                • James Kanze

                  #9
                  Re: Equivalence in use of bitwise | operator and + operator

                  On Nov 14, 4:23 pm, Ioannis Vranos
                  <ivra...@nospam .no.spam.freema il.grwrote:
                  Well when we have an expression like this:
                  int obj= first | second;
                  Why is this style preferred than the equivalent:
                  int obj= first+ second; ?
                  It's not preferred. The two do different things, and what is
                  preferred is the one that does what the program requires.

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  • James Kanze

                    #10
                    Re: Equivalence in use of bitwise | operator and + operator

                    On Nov 14, 7:22 pm, Ioannis Vranos
                    <ivra...@nospam .no.spam.freema il.grwrote:
                    Pete Becker wrote:
                    On 2008-11-14 10:23:55 -0500, Ioannis Vranos
                    <ivra...@nospam .no.spam.freema il.grsaid:
                    Well when we have an expression like this:
                    int obj= first | second;
                    Why is this style preferred than the equivalent:
                    int obj= first+ second; ?
                    They're equivalent if first and second have no common bits
                    set. Otherwise, they're different. Once you start combining
                    flag values with multiple bits set you need to use |, so
                    it's clearer to use | all the time.
                    Yes I was talking about the flags situations, where | is used
                    to add two flags (with different 1 bits) . Using the +
                    operator makes more sense for me.
                    If they're flags, and you're combining bits, it makes more sense
                    to use a bitwise operator.
                    For example (from Qt):
                    model->setSorting(Qdi r::DirsFirst | Qdir::IgnoreCas e | QDir::Name);
                    I think
                    model->setSorting(Qdi r::DirsFirst + Qdir::IgnoreCas e + QDir::Name);
                    is more obvious.
                    If the three terms are values, and you want the sum, then + is
                    more obvious. If the three terms are bit masks (i.e. they
                    represent members of a small set), and you want the union.

                    --
                    James Kanze (GABI Software) email:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                    Comment

                    Working...