471,086 Members | 970 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Operator precedence.

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

Oct 13 '05 #1
5 5303
Angel Tsankov wrote:
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
Oct 13 '05 #2

Victor Bazarov wrote:
Angel Tsankov wrote:
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


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

Oct 14 '05 #3
Angel Tsankov wrote:
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, ...);

Oct 14 '05 #4
Kaz Kylheku wrote:
Angel Tsankov wrote:
Can someone give a reason why bitwise operators (&,^,|) have lower
precedence than equality/inequality test oeprators (==, !=)?


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

Oct 15 '05 #5
Greg wrote:
Kaz Kylheku wrote:
Angel Tsankov wrote:
Can someone give a reason why bitwise operators (&,^,|) have lower
precedence than equality/inequality test oeprators (==, !=)?


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:

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?
Oct 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Master of C++ | last post: by
20 posts views Thread by Vivek N | last post: by
4 posts views Thread by mux | last post: by
21 posts views Thread by siliconwafer | last post: by
56 posts views Thread by spibou | last post: by
9 posts views Thread by marko | last post: by
5 posts views Thread by paulo | last post: by
8 posts views Thread by subramanian100in | last post: by
6 posts views Thread by newbie | last post: by

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.