Tuesday, 2 December 2014

microcontroller - Bitwise operators: Whats the difference ^= vs &= ~(smthg)?


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



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



No comments:

Post a Comment

arduino - Can I use TI&#39;s cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...