assignment expression

Apr 4, 2014 at 9:31pm
I have to do this for a practice assignment but i missed the class where it talked about it, can anyone help me?
Write an assignment expression to increase the value of the variable quantity by the value of the variable extra.

Apr 4, 2014 at 9:34pm
= is the assignment operator.
+ is the addition operator.
+= is a compound operator of equal and plus so that a += b is a = a + b

So you need something like:
1
2
3
quantity += extra; //prefered
//or
quantity = quantity + extra;
Apr 4, 2014 at 9:38pm
oh ok and then its asking to write a bit-wise expression to return twice the value of the integer variable quantity.
Apr 4, 2014 at 9:42pm
If you shift to the left you multiply by 2. If you shift to right you divide by 2. If you look at binary numbers you will tell easily why.

0001 = 1
0010 = 2
0100 = 4
1000 = 8
0000 = 0 //if it was more than 1 byte it would be 0001 0000

now lets look at it backwards:
1000 = 8
0100 = 4
0010 = 2
0001 = 1
0000 = 0
Apr 4, 2014 at 9:43pm
but how do I write that in a bit wise expression?
Apr 4, 2014 at 9:45pm
Same way you would addition but with the bitwise shift.

http://www.cplusplus.com/doc/tutorial/operators/
1
2
3
quantity <<= 1;
//or
quantity = quantity << 1;
Apr 4, 2014 at 9:58pm
thank you so much
Topic archived. No new replies allowed.