Operator precedence.

A

Angel Tsankov

Can someone give a reason why bitwise operators (&,^,|) have lower
precedence than equality/inequality test oeprators (==, !=)?
 
V

Victor Bazarov

Angel said:
Can someone give a reason why bitwise operators (&,^,|) have lower
precedence than equality/inequality test oeprators (==, !=)?

You need to ask Brian Kernighan or Dennis Ritchie. It is the same way in
C++ as it is in C. If I were to speculate, I'd say that initially the
(now bitwise) operators were used for "logical" conditions as well, that's
how they got lower precedence than equality. Later && and || were added
to C (with even lower precedence than bitwise ones). Nobody cared (or was
courageous enough) to change the bitwise ones. Of course, there can exist
another explanation.

V
 
G

Greg

Victor said:
You need to ask Brian Kernighan or Dennis Ritchie. It is the same way in
C++ as it is in C. If I were to speculate, I'd say that initially the
(now bitwise) operators were used for "logical" conditions as well, that's
how they got lower precedence than equality. Later && and || were added
to C (with even lower precedence than bitwise ones). Nobody cared (or was
courageous enough) to change the bitwise ones. Of course, there can exist
another explanation.

V

Your explanation is correct. The && and || operators were added later
for their "short-circuiting" behavior. Dennis Ritchie admits in
retrospect that the precedence of the bitwise operators should have
been changed when the logical operators were added. But with several
hundred kilobytes of C source code in existence at that point and an
installed base of three computers, Dennis thought it would be too big
of a change in the C language...

Greg
 
K

Kaz Kylheku

Angel said:
Can someone give a reason why bitwise operators (&,^,|) have lower
precedence than equality/inequality test oeprators (==, !=)?

A better question is why:

(obj->*pmemb)(arg1, ...); // fucking stupid!

versus

obj->pfunc(arg1, ...);
 
G

Greg

Kaz said:
A better question is why:

(obj->*pmemb)(arg1, ...); // <expletive deleted> stupid!

versus

obj->pfunc(arg1, ...);

How would ambiguity be handled with such a syntax? Consider:

struct A
{
void DoSomething(int);
void DoNothing(int);
};

int main()
{
A a;
void (A::*DoSomething)(int); // a member function pointer

DoSomething = &A::DoNothing;

// Current syntax - calls DoNothing()

(&a->*DoSomething)(3);

// Proposed syntax
// Would the next statement call

// A::DoSomething()

// - or -

// A::DoNothing() ?

a->DoSomething(3); // ambiguous function call
}

Greg
 
R

red floyd

Greg said:
How would ambiguity be handled with such a syntax? Consider:


The real question is, why:

(obj->*pmemb)(arg1,...);

instead of

obj->*pmemb(arg1,...);

If you're adding a new syntactic form, why not give it the right precedence?
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top