user34537
user34537

Reputation:

Uses of & and && operator

Same question goes for | and ||.

What are uses for the & and && operator? The only use i can think of are

Bitwise Ands for int base types (but not float/decimals) using &
logical short circuit for bools/functions that return bool. Using the && operator usually.

I cant think of any other cases i have used it.
Does anyone know other uses?

-edit- To clarify, i am asking about any language. I seen DateTime use '-' to return a timespan, strings use '+' to create new strings, etc. I dont remember any custom datatype using && and &. So i am asking what might they (reasonably) be use for? I dont know of an example.

Upvotes: 0

Views: 395

Answers (6)

Steve Jessop
Steve Jessop

Reputation: 279345

If you're asking about all languages then I don't think it's reasonable to talk about "the & operator". The token & could have all sorts of meanings in different languages, operator and otherwise.

For example in C alone there are two distinct & operators (unary address-of and binary bitwise-and). Unary & in C and related languages is the only example I can immediately think of, of a use I've encountered that meets your criteria.

However, C++ adds operator overloading so that they can mean anything you like for user-defined classes, and in addition the & character has meaning in type declarations. In C++0x the && token has meaning in type declarations too.

A language along the lines of APL or J could "reasonably" use an & operator to mean pretty much anything, since there is no expectation that code in those languages bears any resemblance at all to C-like languages. Not sure if either of those two does in fact use either & or &&.

What meanings it's "reasonable" for a binary & operator overload to have in C++ is a matter of taste - normally it would be something that's analogous to bitwise & in some way, because the values represented by your class can be considered as a sequence of bits in some way. Doesn't have to be, though, as long as it's something that makes sense in the domain. Normally it's fairly "unreasonable" to use an & overload just because & happens to be unused. But if your class represents something fairly abstruse in mathematics and you need a third binary operator after + and *, I suppose you'd start looking around. If what you want is something with even lower precedence than +, binary & is a candidate. I can't for the moment think of any structures in abstract algebra that want such a thing, but that doesn't mean there aren't any.

Overloading operator&& in C++ is moderately antisocial, since the un-overloaded version of the operator short-circuits and overloaded versions don't. C++ programmers are used to writing expressions like if (p && *p != 0), so by overloading operator&& you're in effect messing with a control structure.

Overloading unary operator& in C++ is extremely antisocial. It stops people taking pointers to your objects. IIRC there are some awkward cases where common implementations of standard templates require of their template parameters that unary operator& results in a pointer (or at least a very pointer-like thing). This is not documented in the requirements for the argument, but is either almost or completely unavoidable when the library-writer comes to implement the template. So the overload would place restrictions on the use of the class that can't be deduced from the standard, and there'd better be a very good reason for that.

[Edit: what I didn't know when I wrote this, but do know now, is that template-writers could work around the need to use unary operator& with template parameters where the standard doesn't specify what & does for that type (i.e. all of them). You can do what boost::addressof does, which is:

reinterpret_cast<Foo*>(&reinterpret_cast<char&>(foo))

The standard doesn't require much of reinterpet_cast, but since we're talking about standard templates they know exactly what it does in the implementation, and anyway it's legal to reinterpret an object as chars. I think this is guaranteed to work - but if not the implementation can ensure that it does work if necessary to write fully conforming standard templates.

But, if your implementation doesn't go to these lengths to avoid calling an overloaded operator&, the original problem remains.]

Upvotes: 1

Oded
Oded

Reputation: 499212

In most C-based languages the meanings of these operators are:

  • && - boolean AND. Used in boolean expressions such as if statements.
  • || - boolean OR. Used in boolean expressions such as if statements.
  • & - bitwise AND. Used to AND the bits of both operands.
  • | - bitwise OR. Used to OR the bits of both operands.

However, these are not guaranteed to be such. Since every language defines its own operators, these string can be defined as anything in a different language.

From your edit, you seem to be using C#. The above description is right for C#, with | and & also being conditional operators (depending on context).

As for what you are saying about DateTime and the + operator - this is not related to the other operators you mentioned and their meaning.

Upvotes: 2

Guffa
Guffa

Reputation: 700670

As your previoes question about these operators has been about C#, I assume that this one is too.

Generally you want to use the short-circuit version of the conditional operators to avoid unneccesary operations. If the value of the first operand is enough to determine the result, the second operand needn't be evaluated.

When a condition relies on the previos condition being true, only the short-circuit operators work, for example doing a null check and property comparison:

if (myObj != null && myObj.State == "active")

Using the & operator in that case would not keep the second operand from being evaluated, and it would cause a null reference exception.

The non-shortcircuit operators are useful when you want both operands to always be evaluated, for example when they have a side effect:

if (DoSomeWork() & DoOtherWork())

Using the && operator would prevent the second method to be called if the first returned false.

The & and | are also binary operators, but as the || and && operators aren't, there is no ambiguity when you use them as binary operators.

Upvotes: 1

user623879
user623879

Reputation: 4142

C uses & as a unary operator on any data types to get the address of the data

for example:

int i = 5;
cout<<&i;//print the address of i

Some languages allow you to override such operators to make them do anything you want!

Upvotes: 0

Phil Helix
Phil Helix

Reputation: 3733

If it's Javascript then please look at this answer: Using &&'s short-circuiting as an if statement?

There is a short discussion on C# uses there too.

Java has a few more operators, such as |= : What does "|=" mean in Java?

Upvotes: 0

Gats
Gats

Reputation: 3462

Very general question and I'm assuming you're talking in Java, C#, or another similar syntax. In VB it's the equivalent of + on strings, but that's another story I assume.

As far as I know, your statement is correct if you're talking in terms of C#.

Upvotes: 0

Related Questions