0

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

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

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

Mike
  • 2,166
  • 1
  • 14
  • 29
knight
  • 53
  • 9
  • 6
    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. – Spehro Pefhany Jun 30 '21 at 17:46
  • 6
    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 https://stackoverflow.com/ – Lundin Jul 01 '21 at 07:31

1 Answers1

7

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.

ErikR
  • 4,987
  • 12
  • 18