473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Equivalence in use of bitwise | operator and + operator

Well when we have an expression like this:

int obj= first | second;
Why is this style preferred than the equivalent:
int obj= first+ second; ?

Nov 14 '08 #1
9 2501

Ioannis Vranos <iv*****@nospam.no.spam.freemail.grwrote in
<gf***********@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 <Sf******************************@bt.com>

Nov 14 '08 #2
On Nov 14, 6:23*pm, Ioannis Vranos
<ivra...@nospam.no.spam.freemail.grwrote:
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).
Nov 14 '08 #3
On 2008-11-14 10:23:55 -0500, Ioannis Vranos
<iv*****@nospam.no.spam.freemail.grsaid:
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)

Nov 14 '08 #4
Pete Becker wrote:
On 2008-11-14 10:23:55 -0500, Ioannis Vranos
<iv*****@nospam.no.spam.freemail.grsaid:
>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.
Nov 14 '08 #5
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.grsaid:
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".
Nov 14 '08 #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
Nov 14 '08 #7
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.grsaid:
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
Nov 14 '08 #8
On Nov 14, 4:23*pm, Ioannis Vranos
<ivra...@nospam.no.spam.freemail.grwrote:
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:ja*********@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
Nov 14 '08 #9
On Nov 14, 7:22*pm, Ioannis Vranos
<ivra...@nospam.no.spam.freemail.grwrote:
Pete Becker wrote:
On 2008-11-14 10:23:55 -0500, Ioannis Vranos
<ivra...@nospam.no.spam.freemail.grsaid:
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:ja*********@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
Nov 14 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
5
by: Bill Dee | last post by:
I need help converting a tiny piece of code which uses the bitwise complement operator from C# to VB.NET. In C# I currently have this: long useThis = Myclass.ALLCONSTANTS; long doNotUse =...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
3
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should...
17
by: zirconx | last post by:
I'm trying to understand how the bitwise AND can be used. I've read about what it does but am having trouble applying it practically. I'm working on a system that somebody else wrote and they...
5
by: Junmin H. | last post by:
Hello, all. I never use them when I am programming. But i have been reading codes that using them. When should I use them? Why not use char/short/int/long? Is it very important to know them well?...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.