Equivalence in use of bitwise | operator and + operator

Discussion in 'C++' started by Ioannis Vranos, Nov 14, 2008.

  1. Well when we have an expression like this:

    int obj= first | second;


    Why is this style preferred than the equivalent:


    int obj= first+ second; ?
    Ioannis Vranos, Nov 14, 2008
    #1
    1. Advertising

  2. Ioannis Vranos

    Pavel Lepin Guest

    Ioannis Vranos <> wrote in
    <gfk56c$2uno$>:
    > 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 <>
    Pavel Lepin, Nov 14, 2008
    #2
    1. Advertising

  3. Ioannis Vranos

    maverik Guest

    On Nov 14, 6:23 pm, Ioannis Vranos
    <> wrote:
    > 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).
    maverik, Nov 14, 2008
    #3
  4. Pete Becker wrote:
    > On 2008-11-14 10:23:55 -0500, Ioannis Vranos
    > <> said:
    >
    >> 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(Qdir::DirsFirst | Qdir::IgnoreCase | QDir::Name);



    I think

    model->setSorting(Qdir::DirsFirst + Qdir::IgnoreCase + QDir::Name);

    is more obvious.
    Ioannis Vranos, Nov 14, 2008
    #4
  5. On 14 Nov., 19:22, Ioannis Vranos <>
    wrote:
    > Pete Becker wrote:
    > > On 2008-11-14 10:23:55 -0500, Ioannis Vranos
    > > <> said:

    >
    > >> 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(Qdir::DirsFirst | Qdir::IgnoreCase | QDir::Name);
    >
    > I think
    >
    > model->setSorting(Qdir::DirsFirst + Qdir::IgnoreCase + 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".
    .rhavin grobert, Nov 14, 2008
    #5
  6. 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(Qdir::DirsFirst | Qdir::IgnoreCase | QDir::Name);
    >
    > I think
    > model->setSorting(Qdir::DirsFirst + Qdir::IgnoreCase + 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::DirsFirst & Qdir::IgnoreCase) == 0 &&
    (Qdir::DirsFirst & QDir::Name) == 0 &&
    (Qdir::IgnoreCase & QDir::Name) == 0);

    or literally

    STATIC_ASSERT(Qdir::DirsFirst + Qdir::IgnoreCase + QDir::Name ==
    Qdir::DirsFirst | Qdir::IgnoreCase | 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 "obviousness" (which I personally don't really see).
    While trying to do it without any safeguards is asking for trouble.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 14, 2008
    #6
  7. Ioannis Vranos

    peter koch Guest

    On 14 Nov., 19:22, Ioannis Vranos <>
    wrote:
    > Pete Becker wrote:
    > > On 2008-11-14 10:23:55 -0500, Ioannis Vranos
    > > <> said:

    >
    > >> 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(Qdir::DirsFirst | Qdir::IgnoreCase | QDir::Name);
    >
    > I think
    >
    > model->setSorting(Qdir::DirsFirst + Qdir::IgnoreCase + 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
    peter koch, Nov 14, 2008
    #7
  8. Ioannis Vranos

    James Kanze Guest

    On Nov 14, 4:23 pm, Ioannis Vranos
    <> wrote:
    > 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Nov 14, 2008
    #8
  9. Ioannis Vranos

    James Kanze Guest

    On Nov 14, 7:22 pm, Ioannis Vranos
    <> wrote:
    > Pete Becker wrote:
    > > On 2008-11-14 10:23:55 -0500, Ioannis Vranos
    > > <> said:


    > >> 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(Qdir::DirsFirst | Qdir::IgnoreCase | QDir::Name);


    > I think


    > model->setSorting(Qdir::DirsFirst + Qdir::IgnoreCase + 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Nov 14, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MS News \(MS ILM\)
    Replies:
    0
    Views:
    365
    MS News \(MS ILM\)
    Aug 28, 2003
  2. Manish_Ganvir
    Replies:
    13
    Views:
    1,553
    Keith Thompson
    Feb 14, 2005
  3. Replies:
    6
    Views:
    472
  4. Sam the Cat

    Python and Javascript equivalence

    Sam the Cat, Apr 23, 2007, in forum: Python
    Replies:
    5
    Views:
    311
    Sam the Cat
    Apr 23, 2007
  5. Roland Reichenberg
    Replies:
    7
    Views:
    151
    Josef Möllers
    Oct 14, 2003
Loading...

Share This Page