Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Equivalence in use of bitwise | operator and + operator

Reply
Thread Tools

Equivalence in use of bitwise | operator and + operator

 
 
Ioannis Vranos
Guest
Posts: n/a
 
      11-14-2008
Well when we have an expression like this:

int obj= first | second;


Why is this style preferred than the equivalent:


int obj= first+ second; ?

 
Reply With Quote
 
 
 
 
Pavel Lepin
Guest
Posts: n/a
 
      11-14-2008

Ioannis Vranos <(E-Mail Removed)> wrote in
<gfk56c$2uno$(E-Mail Removed)>:
> 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 <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
maverik
Guest
Posts: n/a
 
      11-14-2008
On Nov 14, 6:23*pm, Ioannis Vranos
<(E-Mail Removed)> 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).
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      11-14-2008
Pete Becker wrote:
> On 2008-11-14 10:23:55 -0500, Ioannis Vranos
> <(E-Mail Removed)> 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:irsFirst | Qdir::IgnoreCase | QDir::Name);



I think

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

is more obvious.
 
Reply With Quote
 
.rhavin grobert
Guest
Posts: n/a
 
      11-14-2008
On 14 Nov., 19:22, Ioannis Vranos <(E-Mail Removed)>
wrote:
> Pete Becker wrote:
> > On 2008-11-14 10:23:55 -0500, Ioannis Vranos
> > <(E-Mail Removed)> 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:irsFirst | Qdir::IgnoreCase | QDir::Name);
>
> I think
>
> model->setSorting(Qdir:irsFirst + 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".
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-14-2008
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:irsFirst | Qdir::IgnoreCase | QDir::Name);
>
> I think
> model->setSorting(Qdir:irsFirst + 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:irsFirst & Qdir::IgnoreCase) == 0 &&
(Qdir:irsFirst & QDir::Name) == 0 &&
(Qdir::IgnoreCase & QDir::Name) == 0);

or literally

STATIC_ASSERT(Qdir:irsFirst + Qdir::IgnoreCase + QDir::Name ==
Qdir:irsFirst | 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
 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      11-14-2008
On 14 Nov., 19:22, Ioannis Vranos <(E-Mail Removed)>
wrote:
> Pete Becker wrote:
> > On 2008-11-14 10:23:55 -0500, Ioannis Vranos
> > <(E-Mail Removed)> 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:irsFirst | Qdir::IgnoreCase | QDir::Name);
>
> I think
>
> model->setSorting(Qdir:irsFirst + 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-14-2008
On Nov 14, 4:23*pm, Ioannis Vranos
<(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-14-2008
On Nov 14, 7:22*pm, Ioannis Vranos
<(E-Mail Removed)> wrote:
> Pete Becker wrote:
> > On 2008-11-14 10:23:55 -0500, Ioannis Vranos
> > <(E-Mail Removed)> 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:irsFirst | Qdir::IgnoreCase | QDir::Name);


> I think


> model->setSorting(Qdir:irsFirst + 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use of bitwise operator for signed number arunaling@yahoo.co.in C Programming 6 06-07-2006 12:30 PM
bitwise & operator and counting set bits sathyashrayan C Programming 7 04-14-2005 05:36 PM
How to calculate size of an int without using the sizeof operator but using bitwise operator Manish_Ganvir C Programming 13 02-14-2005 07:24 PM
Pointer and BitWise Operator. Che C Programming 4 03-07-2004 11:21 PM
cannot use bitwise AND-Operator on hex-scalars from ARGV[0] Roland Reichenberg Perl Misc 7 10-14-2003 01:05 PM



Advertisments