@floresta
I don't think you can shift a '0'.
Sure you can: Zero shifted left two bits (or any number of bits) is zero.
@floresta
why I don't use this technique
The issue that I have with that particular statement is that if bit number 2 "just happens" to be a logic "one" before that statement is executed, the register still has a logic "one" at that position after the statement is executed. ORing a "zero" with bit 2 in the register leaves that bit unchanged.
So, here's how I see it:
If you want to make sure one of more bits are set to logic "one" without changing other bits, you use a logical OR. An |= expression with "one" shifted left certain numbers of bits can do it.
If there is only one bit that you want to set, the Arduino bitSet macro (in wiring.h) will perform that operation in a way that is more readable to me.
However...
If you want to make sure one or more bits are reset to logic "zero" without changing other bits, you can use a logical AND with a mask that has values of "one" in bit positions that are not to be changed and values of "zero" in bit positions that you want to reset to "zero." So you could might be able to use an &= expression similar to the one shown.
If you have only one bit position that you want to reset, the Arduino macro bitClear does the deed in a very readable (to me) way.
@InvalidApple
It's for the ease of reading the code.
Well "ease of reading" is in the eye of the beholder.
However...
My objection (regardless of the subjective elegance or lack thereof):
Issues of readability aside, that particular statement doesn't guarantee that bit 2 is zero after executing that statement.
I mean, if you knew that the register was zero beforehand and you want to OR in zero bits here and there, well OK, but...
@floresta
Data sheets are not known for giving the clearest programming examples, just ones that will work.
Indeed. (See Footnote.)
Furthermore---
Maybe they work under certain circumstances that may or may not have been spelled out by the person who wrote the data sheet. Like assuming that the register contained all zeros before that particular statement, (if, indeed, that particular statement was in the data sheet).
Bottom line as I see it:
At the beginning of the program I would use an assignment statement that puts required values in all bit positions.
Later, as the program is running, if I wanted to change certain bit values without changing others, I might use logical OR and logical AND operations.
Regards,
Dave
Footnote:
At a design review (hardware or software) if someone challenges a particular piece of the design, you can't fall back on something like, "Well that's what the Application Note said." At least not if My Boss is there (bless her heart). Believe me. Don't try it!
Been there; done that. (And have the resounding sounds of deprecatory laughter echoing in my little head even now, all these years later.)