[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Equivalence in use of bitwise | operator and + operator

Ioannis Vranos

11/14/2008 3:24:00 PM

Well when we have an expression like this:

int obj= first | second;


Why is this style preferred than the equivalent:


int obj= first+ second; ?

9 Answers

Pavel Lepin

11/14/2008 3:39:00 PM

0


Ioannis Vranos <ivranos@nospam.no.spam.freemail.gr> wrote in
<gfk56c$2uno$1@ulysses.noc.ntua.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 <Sfedncgr46kOPmDanZ2dnUVZ8vCdnZ2d@bt.com>

maverik

11/14/2008 3:46:00 PM

0

On Nov 14, 6:23 pm, Ioannis Vranos
<ivra...@nospam.no.spam.freemail.gr> 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).

Pete Becker

11/14/2008 4:19:00 PM

0

On 2008-11-14 10:23:55 -0500, Ioannis Vranos
<ivranos@nospam.no.spam.freemail.gr> 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.

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

Ioannis Vranos

11/14/2008 6:23:00 PM

0

Pete Becker wrote:
> On 2008-11-14 10:23:55 -0500, Ioannis Vranos
> <ivranos@nospam.no.spam.freemail.gr> 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.

.rhavin grobert

11/14/2008 6:32:00 PM

0

On 14 Nov., 19:22, Ioannis Vranos <ivra...@nospam.no.spam.freemail.gr>
wrote:
> Pete Becker wrote:
> > On 2008-11-14 10:23:55 -0500, Ioannis Vranos
> > <ivra...@nospam.no.spam.freemail.gr> 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".

Andrey Tarasevich

11/14/2008 8:02:00 PM

0

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

peter koch

11/14/2008 9:44:00 PM

0

On 14 Nov., 19:22, Ioannis Vranos <ivra...@nospam.no.spam.freemail.gr>
wrote:
> Pete Becker wrote:
> > On 2008-11-14 10:23:55 -0500, Ioannis Vranos
> > <ivra...@nospam.no.spam.freemail.gr> 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

James Kanze

11/14/2008 11:01:00 PM

0

On Nov 14, 4:23 pm, Ioannis Vranos
<ivra...@nospam.no.spam.freemail.gr> 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:james.kanze@gmail.com
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

11/14/2008 11:03:00 PM

0

On Nov 14, 7:22 pm, Ioannis Vranos
<ivra...@nospam.no.spam.freemail.gr> wrote:
> Pete Becker wrote:
> > On 2008-11-14 10:23:55 -0500, Ioannis Vranos
> > <ivra...@nospam.no.spam.freemail.gr> 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:james.kanze@gmail.com
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