James Kanze
11/14/2008 11:03:00 PM
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