473,411 Members | 2,009 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,411 software developers and data experts.

QUERY: op= rationale


I've been curious for awhile about why C supports some of its binary
operators in an op= format, but not others. For example, why are &=
and |= supported by C, but not &&= and ||=?

For awhile I was guessing that it was because the designers wanted to
avoid 3-character operators, but then I remembered <<= and >>=.

If anyone has any insight into the rationale for this, please let me
know. My sense has been that such operators would occasionally be
useful, and I've considered adding them to my own C-like languages,
but want to be sure I'm not overlooking something subtle.

Thanks very much,
-Brad
Nov 14 '05 #1
4 1296
Hi Brad,
there is no definite answer from my side, just stating the obvious -
sometimes that can be a help, too.

I've been curious for awhile about why C supports some of its binary
operators in an op= format, but not others. For example, why are &=
and |= supported by C, but not &&= and ||=?

If anyone has any insight into the rationale for this, please let me
know. My sense has been that such operators would occasionally be
useful, and I've considered adding them to my own C-like languages,
but want to be sure I'm not overlooking something subtle.


The "supported" operators are the binary operators for arithmetic
and bitwise operations. IMO, reasons are

- the behaviour and types of operands and result is clear or easy to
define
- these operations are used rather often, so a shortcut makes sense
- for early none-too-intelligent compilers, this was a help to generate
assembler code which was faster if there were corresponding
operations (e.g. on a Motorola 68xx I've been playing with,
adding two values and storing the result in a different value
took one cycle longer than adding one value to another)
- in the same vein: this is AFAIK not true for logical operations and
the like

Now, introducing &&=, ||=, ===, ->=, .=, ... in general may be
a nice idea if you need it a lot; even ?=: is an option.

For the logical AND and OR as well as for ==,>=,<=, I think that
the readability of your code might suffer, apart from the
higher probability of errors by typing not enough or too many
&,|,=.
The ->= is also prone to the latter problem, if you manage to
forget the >.
For <=,>= there are <== and >== but what is there for <,> ?

Maybe someone else can contribute a little bit more.
So, the only "real" downsides are that you have to teach it to the
compiler and that you'll be one of a very small group to use
the resulting language in full :-)
Cheers,
Michael

Nov 14 '05 #2
Bradford Chamberlain wrote:

I've been curious for awhile about why C supports some of its binary
operators in an op= format, but not others. For example, why are &=
and |= supported by C, but not &&= and ||=?

For awhile I was guessing that it was because the designers wanted to
avoid 3-character operators, but then I remembered <<= and >>=.

If anyone has any insight into the rationale for this, please let me
know. My sense has been that such operators would occasionally be
useful, and I've considered adding them to my own C-like languages,
but want to be sure I'm not overlooking something subtle.

Thanks very much,
-Brad


Think 'bout the long version of what you're suggesting ->

a &&= b;

would be

a = a && b;

Would you use such an expression in a _real_ program
(hint: `&&' and `||' aren't binary operators)?
-
Stephen
Nov 14 '05 #3
Bradford Chamberlain <br***@lotus.wc.cray.com> wrote in message news:<qk*************@lotus.wc.cray.com>...
I've been curious for awhile about why C supports some of its binary
operators in an op= format, but not others. For example, why are &=
and |= supported by C, but not &&= and ||=?

For awhile I was guessing that it was because the designers wanted to
avoid 3-character operators, but then I remembered <<= and >>=.

If anyone has any insight into the rationale for this, please let me
know. My sense has been that such operators would occasionally be
useful, and I've considered adding them to my own C-like languages,
but want to be sure I'm not overlooking something subtle.

I suspect it's mainly a lack of general utility - yes it might be
useful on occasion, but not very often. Second, how exactly do you
intend to define the short-circuit semantics for "||="? Neither
option is very attractive. Then there's the workaround, which is
trivial, so why bother.
Nov 14 '05 #4

On Tue, 20 Jul 2004, Stephen L. wrote:

Bradford Chamberlain wrote:

I've been curious for awhile about why C supports some of its binary
operators in an op= format, but not others. For example, why are &=
and |= supported by C, but not &&= and ||=?


Think 'bout the long version of what you're suggesting ->

a &&= b;

would be

a = a && b;

Would you use such an expression in a _real_ program
(hint: `&&' and `||' aren't binary operators)?


Double hint: they sure as heck ain't unary! The word you're
aiming for is "bitwise," and it's completely irrelevant.

#include <stdbool.h>

bool rabbitsFromAllThreeHats(void)
{
bool rc = true;
rc &&= rabbitFromHatA();
rc &&= rabbitFromHatB();
rc &&= rabbitFromHatC();
return rc;
}

-Arthur
Nov 14 '05 #5

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

Similar topics

24
by: Matt Feinstein | last post by:
Hi all-- I'm new to Python, and was somewhat taken aback to discover that the core language lacks some basic numerical types (e.g., single-precision float, short integers). I realize that there...
4
by: Beman Dawes | last post by:
The docs for os.path.exists(), isdir(), and the like, do not describe behavior when an I/O error occurs. Testing on Windows XP SP2 with Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32, on a...
4
by: sdowney717 | last post by:
Select LOCGeneralHave.LSCH, LOCSubClassHave.LSCH from LOCGeneralHave , LOCSubClassHave Where (LOCGeneralHave.LCNT <> '0' and LOCSubClassHave.LCNT = '0') This query seems to be ignoring the...
2
by: Howard Jess | last post by:
Given the html at the end of this message, I see how a DOM NodeList exhibits its "live" behavior; that is, adding elements to a document can change any NodeList variables, when there's *no* code...
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
14
by: Jim Andersen | last post by:
I have a problem with this standard employee-supervisor scenario For pictures: http://www.databasedev.co.uk/self-join_query.html I want to show all employees "belonging" to a specific...
15
by: Ben Hinkle | last post by:
I'm curious, what was the rationale for making a builtin type _Bool but then having #define true 1 #define false 0 in stdbool.h? That seems very odd that true and false don't have type _Bool. In...
1
by: Nick Bastin | last post by:
I've done some searching in this newsgroup and on the internet and haven't found an answer, so I thought I'd ask here... :-) I don't understand why full specialization of a member function...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.