473,396 Members | 1,921 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,396 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 5778
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
20
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int...
4
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
17
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using ,...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
8
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
6
by: newbie | last post by:
I have a script given to me by a co-worker to convert into VB; I can get the same results but I cannot fully understand the logic here... can someone please help me write this in a more...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.