Go Down

Topic: |= and += (Read 643 times) previous topic - next topic

Freeborn_rus

Can anybody explain what is the difference between operators "|=" and "+=" when using them with registers?

For example, using "TIMSK+= 1<<1" works good, but "TIMSK|= 1<<1" kills delay command.

pYro_65

#1
Jan 01, 2014, 09:32 am Last Edit: Jan 01, 2014, 09:39 am by pYro_65 Reason: 1
Addition is slightly different to or. Its no different to using chars, registers are just values too.

6 | 2 = 6
6 + 2 = 8

If you do not know what the operators are:
http://www.cplusplus.com/doc/tutorial/operators/#bitwise
http://www.cplusplus.com/doc/tutorial/operators/#logical
Forum Mod anyone?
https://arduino.land/Moduino/

PaulRB

Well, "+=" performs an addition, so if the bit you are trying to set is already 1, then its going to change to zero, and a 1 gets carried to the next most significant bit and so on. Not a suitable operation to perform on a control register, I would say.

"|=" performs logical OR, so if the bit in question is already 1 then no change is made.

Paul

Freeborn_rus

Thanks a lot, I get it.

Go Up