Neither cppreference nor cplusplus, and nor Microsoft websites provided the verbose definition of the C++ keyword &= (a.k.a. and_eq), despite examples that seem to be a bit cryptic for a novice.
Yet the book C++ In a Nutshell: A Desktop Quick Reference by Lischner provided the following (p. 291, 2003):
The and_eq operator is an assignment operator that performs bitwise and.
Quoting the example from Microsoft:
#include <iostream>
#include <iso646.h>
int main( )
{
using namespace std;
int a = 3, b = 2, result;
result= a &= b;
cout << result << endl;
}
yields 2.
Accordingly, could you please tell me whether &= is a shortcut for concatenated assignments (if such a thing exists?), for example: result = a = b;?
No. a &= b is a shortcut for a = a & b.
Additionally, a = b returns the value that was assigned to a. So result = a &= b is short for:
a = a & b;
result = a;
&=, and all of the other op= built in operators are described in [expr.ass]\7 as
The behavior of an expression of the form E1 op= E2 is equivalent to E1 = E1 op E2except that E1 is evaluated only once. In += and -=, E1 shall either have arithmetic type or be a pointer to a possibly cv-qualified completely-defined object type. In all other cases, E1 shall have arithmetic type.
So, result= a &= b; is the same as
result = (a = (a & b));
or textually:
store the result of bitwise and between a and b into a and result.
It's not a keyword, it's an expression operator. Found here: https://en.cppreference.com/w/cpp/language/expressions
It is the bit-wise AND operator combined with the assignment operator. On primitive types, it performs a bit-wise AND on the two parameters and then assigns the result to the first parameter.
uint32_t a = 0b00110101;
uint32_t b = 0b01101011;
a &= b;
//Equivalent: a = a & b;
assert(a == 0b00100001);
foo &= bar is short for foo = foo & bar, for built-in types. But, it's an operator that can be reimplemented by user defined types, so it's semantics could be changed for such.
There’s one relevant difference between a = a & b; and a &= b;. The former creates a temporary equal to a & b and assigns that temporary value to a. The latter updates a in place.
For built-in types, that doesn’t matter: any compiler will just optimize either expression to the same machine-language instruction. If you define a class and overload the operators, though, writing a = a & b; might end up creating a temporary object, copying a bunch of data to it, and then copying all its data somewhere else. While there are ways to mitigate this problem, a &= b; is in theory more efficient.
This question already has an answer here:
Why doesn't C++ have the ~= and != operators? [closed]
(1 answer)
Closed 5 years ago.
int main()
{
unsigned int a = 0;
unsigned int b = 0;
a ^= b; // ok
a |= b; // ok
a &= b; // ok
a = ~b; // ok
a ~= b; // error : expected ';' after expression
}
^=, |=, and &= are all legal.
Why isn't ~= legal in C++?
Because ~ is an unary operator, not binary.
The short form op= applies to binary operators only, when the first operand is the destination.
~ is only ever a unary operator.
The contraction of a = a # b to a #= b for an arbitrary operator # only makes sense if it takes two arguments; i.e. if # is a binary operator.
Why isn't ~= legal in C++?
That's because C++ does not contain an ~= operator; ~= are just two separate tokens.
As for why C++ wasn't designed that way, you'd have to ask the designer, but I think it's safe to say that it's because C++ was based on C originally and has the same operators as C, with only a few new operators added for the new language features (i.e. :: and ->*).
So you should probably ask this question again for C.
Because the "operator assignments" (like +=, ^=) are based on the original operation having two operands.
For example, a + b has two operands of +, and a += b gives the same net effect as a = a + b.
Operator ~ is a unary operator i.e. it accepts one operand. So a = ~b makes sense, but a = a~b does not. Since a = a~b doesn't make sense, neither does a ~= b.
Can you tell me how these two are different?
int i = + +0;
int i = ++0; // compiler error
Thanks.
The first one has a whitespace between the first + and the second +, the second one hasn't.
This means the first parses as two instances of the unary prefix operator +, while the latter parses as the unary prefix operator ++ (which of course cannot be applied to a literal).
There are used two different operators in these declarations.
In the first declaration there is used twice the unary plus operator. For built-in types it simply returns the value of its promoted operand. So in fact it has no any meaning that is this declaration
int i = + +0;
is equivalent to
int i = 0;
You could even write
int i = + + + + + +0;
but the result would not be changed.:)
In the second declaration there is used the unary prefix increment operator ++. It may not be applied to rvalues. So the compiler will issue an error.
The declaration would be compiled if you would add one more dclaration. For example
int j = 0;
int i = ++j;
In this case lvalue j may be incremented. The result of the initializer expression will be equal to 1 that is i will be initialized by 1 and of course j itself will be also equal to 1..
Also take into account that you may write for example any even number of pluses before j in the example above. For example
int j = 0;
int i = ++++++++++++j;
But you may use only one pair of pluses after j. For example
int j = 0;
int i = j++++; // compilation error.
But it is another question.:)
Take into account that (C++ Standard)
1 Expressions with unary operators group right-to-left
and prefix increment operator ++ and the unary plus operator are both unary operators.
For my first program, I'm trying to make a calculator, but I'm getting an error:
Error: lvalue required as a left operand of assignment
Here is the code:
http://pastebin.com/mxdnPGeT
the error is where I say x + y = sum;
The assignment operator = works by assigning whatever is on the right of the operator to the object on the left. So you are attempting to assign the value of sum, which is uninitialized, to the value resulting from x + y, which you cannot assign to. Looks like you really want to do:
sum = x + y;
It's not like in maths where the = operator just means that the two operands are equal. In C++ (and many programming languages), = is assignment. It assigns from the right to the left. Some languages use different characters for the operator precisely to avoid this confusion (like := or <-).
x + y = sum;
This is not valid, x+y is not an lvalue: you can't assign to x+y because it doesn't have a storage address.
You should invert the order of the operands in the assignment expression:
sum = x + y;
You have the following:
x + y = sum;
What you needed is:
sum = x + y;
I don't understand the concept of postfix and prefix increment or decrement. Can anyone give a better explanation?
All four answers so far are incorrect, in that they assert a specific order of events.
Believing that "urban legend" has led many a novice (and professional) astray, to wit, the endless stream of questions about Undefined Behavior in expressions.
So.
For the built-in C++ prefix operator,
++x
increments x and produces (as the expression's result) x as an lvalue, while
x++
increments x and produces (as the expression's result) the original value of x.
In particular, for x++ there is no no time ordering implied for the increment and production of original value of x. The compiler is free to emit machine code that produces the original value of x, e.g. it might be present in some register, and that delays the increment until the end of the expression (next sequence point).
Folks who incorrectly believe the increment must come first, and they are many, often conclude from that certain expressions must have well defined effect, when they actually have Undefined Behavior.
int i, x;
i = 2;
x = ++i;
// now i = 3, x = 3
i = 2;
x = i++;
// now i = 3, x = 2
'Post' means after - that is, the increment is done after the variable is read. 'Pre' means before - so the variable value is incremented first, then used in the expression.
The difference between the postfix increment, x++, and the prefix increment, ++x, is precisely in how the two operators evaluate their operands. The postfix increment conceptually copies the operand in memory, increments the original operand and finally yields the value of the copy. I think this is best illustrated by implementing the operator in code:
int operator ++ (int& n) // postfix increment
{
int tmp = n;
n = n + 1;
return tmp;
}
The above code will not compile because you can't re-define operators for primitive types. The compiler also can't tell here we're defining a postfix operator rather than prefix, but let's pretend this is correct and valid C++. You can see that the postfix operator indeed acts on its operand, but it returns the old value prior to the increment, so the result of the expression x++ is the value prior to the increment. x, however, is incremented.
The prefix increment increments its operand as well, but it yields the value of the operand after the increment:
int& operator ++ (int& n)
{
n = n + 1;
return n;
}
This means that the expression ++x evaluates to the value of x after the increment.
It's easy to think that the expression ++x is therefore equivalent to the assignmnet (x=x+1). This is not precisely so, however, because an increment is an operation that can mean different things in different contexts. In the case of a simple primitive integer, indeed ++x is substitutable for (x=x+1). But in the case of a class-type, such as an iterator of a linked list, a prefix increment of the iterator most definitely does not mean "adding one to the object".
No one has answered the question:
Why is this concept confusing?
As an undergrad Computer Science major it took me awhile to understand this because of the way I read the code.
The following is not correct!
x = y++
X is equal to y post increment. Which would logically seem to mean X is equal to the value of Y after the increment operation is done. Post meaning after.
or
x = ++y
X is equal to y pre-increment. Which would logically seem to mean X is equal to the value of Y before the increment operation is done. Pre meaning before.
The way it works is actually the opposite. This concept is confusing because the language is misleading. In this case we cannot use the words to define the behavior.
x=++y is actually read as X is equal to the value of Y after the increment.
x=y++ is actually read as X is equal to the value of Y before the increment.
The words pre and post are backwards with respect to semantics of English. They only mean where the ++ is in relation Y. Nothing more.
Personally, if I had the choice I would switch the meanings of ++y and y++. This is just an example of a idiom that I had to learn.
If there is a method to this madness I'd like to know in simple terms.
Thanks for reading.
It's pretty simple. Both will increment the value of a variable. The following two lines are equal:
x++;
++x;
The difference is if you are using the value of a variable being incremented:
x = y++;
x = ++y;
Here, both lines increment the value of y by one. However, the first one assigns the value of y before the increment to x, and the second one assigns the value of y after the increment to x.
So there's only a difference when the increment is also being used as an expression. The post-increment increments after returning the value. The pre-increment increments before.
int i = 1;
int j = 1;
int k = i++; // post increment
int l = ++j; // pre increment
std::cout << k; // prints 1
std::cout << l; // prints 2
Post increment implies the value i is incremented after it has been assigned to k. However, pre increment implies the value j is incremented before it is assigned to l.
The same applies for decrement.
Post-increment:
int x, y, z;
x = 1;
y = x++; //this means: y is assigned the x value first, then increase the value of x by 1. Thus y is 1;
z = x; //the value of x in this line and the rest is 2 because it was increased by 1 in the above line. Thus z is 2.
Pre-increment:
int x, y, z;
x = 1;
y = ++x; //this means: increase the value of x by 1 first, then assign the value of x to y. The value of x in this line and the rest is 2. Thus y is 2.
z = x; //the value of x in this line is 2 as stated above. Thus z is 2.
Since we now have inline javascript snippets I might as well add an interactive example of pre and pos increment. It's not C++ but the concept stays the same.
let A = 1;
let B = 1;
console.log('A++ === 2', A++ === 2);
console.log('++B === 2', ++B === 2);
From the C99 standard (C++ should be the same, barring strange overloading)
6.5.2.4 Postfix increment and decrement operators
Constraints
1 The operand of the postfix increment
or decrement operator shall have
qualified or unqualified real or
pointer type and shall be a modifiable
lvalue.
Semantics
2 The result of the postfix ++
operator is the value of the operand.
After the result is obtained, the
value of the operand is incremented.
(That is, the value 1 of the
appropriate type is added to it.) See
the discussions of additive operators
and compound assignment for
information on constraints, types, and
conversions and the effects of
operations on pointers. The side
effect of updating the stored value of
the operand shall occur between the
previous and the next sequence point.
3 The postfix -- operator is analogous
to the postfix ++ operator, except
that the value of the operand is
decremented (that is, the value 1 of
the appropriate type is subtracted
from it).
6.5.3.1 Prefix increment and decrement operators
Constraints
1 The operand of the prefix increment
or decrement operator shall have
qualified or unqualified real or
pointer type and shall be a modifiable
lvalue.
Semantics
2 The value of the operand of the
prefix ++ operator is incremented. The
result is the new value of the operand
after incrementation. The expression
++E is equivalent to (E+=1). See the discussions of additive operators and
compound assignment for information on
constraints, types, side effects, and
conversions and the effects of
operations on pointers.
3 The prefix -- operator is analogous
to the prefix ++ operator, except that
the value of the operand is
decremented.
Post increment(a++)
If
int b = a++,then this means
int b = a;
a = a+1;
Here we add 1 to the value. The value is returned before the increment is made,
For eg a = 1; b = a++;
Then b=1 and a=2
Pre-increment (++a)
If int b = ++a; then this means
a=a+1;
int b=a ;
Pre-increment: This will add 1 to the main value. The value will be returned after the increment is made, For a = 1; b = ++a;
Then b=2 and a=2.
Already good answers here, but as usual there seems to be some general lack of clarity in simply remembering which way round these work. I suppose this arises because semantically resolving the nomenclature is not entirely straightforward. For example, you may be aware that "pre-" means "before". But does the pre-increment ++i return the value of i before the increment, or does it increment i before returning a value?
I find it much easier to visually follow the expression through from left to right:
++ i
-------------------------------------------------->
Increment i Then supply the value of i
i ++
-------------------------------------------------->
Supply the value of i Then increment i
Of course, as Alf points out in the accepted answer, this may not reflect when the 'real i' is updated, but it is a convenient way of thinking about what gets supplied to the expression.
#include<stdio.h>
void main(){
char arr[] ="abcd";
char *p=arr,*q=arr;
char k,temp;
temp = *p++; /* here first it assigns value present in address which
is hold by p and then p points to next address.*/
k = ++*q;/*here increments the value present in address which is
hold by q and assigns to k and also stores the incremented value in the same
address location. that why *q will get 'h'.*/
printf("k is %c\n",k); //output: k is h
printf("temp is %c\n",temp);//output: temp is g
printf("*p is %c\n",*p);//output: *p is e
printf("*q is %c",*q);//output: *q is h
}
Post and Pre Increment with Pointers
The pre increment is before increment value ++ e.g.:
(++v) or 1 + v
The post increment is after increment the value ++ e.g.:
(rmv++) or rmv + 1
Program:
int rmv = 10, vivek = 10;
cout << "rmv++ = " << rmv++ << endl; // the value is 10
cout << "++vivek = " << ++vivek; // the value is 11
You should also be aware that the behaviour of postincrement/decrement operators is different in C/C++ and Java.
Given
int a=1;
in C/C++ the expression
a++ + a++ + a++
evaluates to 3, while in Java it evaluates to 6. Guess why...
This example is even more confusing:
cout << a++ + a++ + a++ << "<->" << a++ + a++ ;
prints 9<->2 !! This is because the above expression is equivalent to:
operator<<(
operator<<(
operator<<( cout, a++ + a++ ),
"<->"
),
a++ + a++ + a++
)