-4
\$\begingroup\$

This question already has an answer here:

I am new to atmel studio, and want to know the best way to code this example:
What is the difference between using:

DDRG |= (1<<DDRG3)

vs

DDRG3 = 1

Edit: In subsequent comments, the OP changed his question to be:

"i could modify the question to . what's best DDRG |= (1<<DDRG3) OR DDRG = 0x08"

\$\endgroup\$

marked as duplicate by Daniel Grillo, Dmitry Grigoryev, PeterJ, placeholder, uint128_t May 4 '16 at 20:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3
\$\begingroup\$

Replying to your new version of the question:

what's best DDRG |= (1<<DDRG3) OR DDRG = 0x08

There is no "best" because those two snippets1 of C do different things - they only have the same end result in one situation; in all other situations they will have different end results! This means that, as always in programming, you must be really clear what you want to do and write your code accordingly.

1[From now onwards, I will assume that they are followed by a semicolon and call those two pieces of code "C statements".]

a) DDRG = 0x08;

This clearly just sets bit 3 (the DDRG3 bit, value 0x08) in the DDRG register. Think about what values are set for the other bits in that register? They are all set to zero.

b) DDRG |= (1<<DDRG3);

This reads whatever value is currently set in the DDRG register, sets bit 3 (assuming DDRG3 = 3) and writes this result back to the DDRG register. Think about what values are set for the other bits in that register? They are all unchanged from their previous values.

[An optimising compiler, upon recognising that coding idiom, might be able to use a suitable assembly instruction, if one exists, to set that bit in the DDRG register - without needing to do the read-modify-write implied in the C source code - the end result is the same either way.]


So as you can now see, the only time that those two statements have the same end result, is when the other (used) bits in DDRG are already set to zero before each of those two statements were executed.

Using DDRG |= (1<<DDRG3); suggests to the reader (who might be you, trying to maintain the code some years later!) that you care about preserving the other bits set in that register. Note that using a bitwise-OR operator like this, is so common that it is used by Atmel in their ATmega128A datasheet regarding setting bits in GPIO registers, for example.

Whereas using DDRG = 0x08; suggests that you want to specifically clear any other bits which are set in that register, at the time when you set bit 3.

Which of those statements you should use, depends on exactly what result you want to achieve, for the value in the DDRG register.


I have used page 134 of this Atmel ATmega128A datasheet to confirm the DDRG register layout and DDRG3 bit value, 0x08, within that register. For other MCUs, the interpretation may be different.

\$\endgroup\$
  • \$\begingroup\$ In most AVR development environments, DDRG3 is a constant defined as 3. (It's usually not a register at all.) \$\endgroup\$ – duskwuff May 3 '16 at 16:21
  • \$\begingroup\$ @duskwuff - thanks, yes, agreed DDRG3 is a constant in the programming environment - it is also bit 3 in the DDRG register definition. I tried to be clear, but I knew that I could explain this several ways. I don't want a reader to think that DDRG3 is only a constant, without realising that it is also a bit in the DDRG register! \$\endgroup\$ – SamGibson May 3 '16 at 16:27
  • \$\begingroup\$ Right, but the problem is, if DDRG3 is a constant, the second line of code in the question is a syntax error, as it expands to 3 = 1. \$\endgroup\$ – duskwuff May 3 '16 at 16:34
  • \$\begingroup\$ Yes in the OP's original question. However, note that he changed his question in a subsequent comment - that is the version of the question which I have answered, and that is why I prefixed my answer with "Replying to your new version of the question" and I quoted his new version of the question at the start of my answer. I will edit the "new version" into the question now... \$\endgroup\$ – SamGibson May 3 '16 at 16:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.