What does *= mean?

For example,
1
2
3
4
void multiplyByTwo(int m)
{
     m*=2;
}

How does that read exactly? I understand what it does by the function name, but I don't understand what *=2; actually means. If someone could give me a quick answer that would be great.
m*=2 is the same as m = m*2 ( new value of m = old value of m * 2)
Last edited on
Thanks for the reply oleg. So If I am understanding correctly.
this statement in line 3 reads
m is equal to m times two.
the old value of m is now destroyed forever
old value of m , is being overwritten(namely , the bits of this integer)
don't forget that you are passing the argument of the function by value , so it will make a copy

ex :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int g = 17;

void multiplyByTwo(int m)
{
 m*=2;
 cout << m ; //  34
}

int main()
{
 multiplyByTwo(g);
 cout << g; // 17
}
Last edited on
That is what I am trying to learn right now, the difference of passing by value and reference.
don't forget to mark as solved each question
Thank you, I forgot.
closed account (1CfG1hU5)
the long version of "m*=2;" is "m = m * 2;"

a multiplication counter.

int m = 1;

m*=2; // number is 2

if done again

m*=2; // number is 4

m*=2; // number is 8

and so on
Last edited on
closed account (1CfG1hU5)
example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
 {
   int m = 1;

   while (m <= 8)
     {
       cout << "the value of m is now " << m << "\n";
       m*=2;
     }

return 0;

  }

Last edited on
closed account (1CfG1hU5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void multiplyByTwo(int m)
{
 m*=2; // m = m * 2; same result
 cout << "g is " << m << " after multiplyByTwo\n";
}

int main()
{

 int g = 17;
 cout << "integer g starts as " << g << "\n";
 
 multiplyByTwo(g);

 return 0;

}


run this
Last edited on
Topic archived. No new replies allowed.