Equivalence in use of bitwise | operator and + operator

I

Ioannis Vranos

Well when we have an expression like this:

int obj= first | second;


Why is this style preferred than the equivalent:


int obj= first+ second; ?
 
P

Pavel Lepin

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; ?

#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?
 
M

maverik

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).
 
I

Ioannis Vranos

Pete said:
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.
 
R

.rhavin grobert

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".
 
A

Andrey Tarasevich

Ioannis said:
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.
 
P

peter koch

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
 
J

James Kanze

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);
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top