Electronic – Bitwise operators: Whats the difference ^= vs &= ~(smthg)

ccodemicrocontroller

I'm coding for microcontrollers and I've come across two styles of bitwise operations that quite confuse me a lot because they are used interchangeably.
First let's assume our REGISTER has 4 bits; bit 0,1,2,3. The default value of REGISTER on reset is 1100.

1.what the difference between:

REGESTER ^= (1<<3) | (1<<2);

&

REGESTER &= ~( (1<<3) | (1<<2) );

2.Is the first one correct? ( I'm not certain that it's correct )

3.If the first one's correct, then why use the second statement as the resulting value is same as the second one?

Final Result: 0000

Best Answer

These are not interchangeable in general. You get the same results in this particular case, because the first one toggles bits 3 and 2, the second one clears them. Which one is correct depends on what you want to accomplish