0
\$\begingroup\$

Can anyone tell me the difference between the following two assignments?

control_reg |= (1<<2) | (1<<4);


control_reg = (1<<2) | (1<<4);
\$\endgroup\$
2
  • 6
    \$\begingroup\$ Look up the difference between |= and =. Your course should have covered that. For homework you should use the homework tag and not expect full answers. \$\endgroup\$ Jun 30, 2021 at 17:46
  • 6
    \$\begingroup\$ I’m voting to close this question because it's a pure programming question, not specifically related to microcontroller programming. It should be asked at stackoverflow.com \$\endgroup\$
    – Lundin
    Jul 1, 2021 at 7:31

1 Answer 1

7
\$\begingroup\$

control_reg |= (1<<2) | (1<<4);

This only affects bits 2 and 4 -- all of the other bits in control_reg are left unmodified.

control_reg = (1<<2) | (1<<4);

This modifies all the bits in control_reg -- bits 2 and 4 are set to 1 and all the other bits are set to 0.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.