Continue to Site

# what is the difference between DDRA&=~(1<<AINpin) and DDRA=0x01

Status
Not open for further replies.

#### cheenu02

##### Junior Member level 3
what is the difference between DDRA&=~(1<<AINpin) and DDRA=0x01

1<<AINpin shifts left 1 AINpin positions, for example
1<<0 =0b00000001
1<<1 =0b00000010
1<<6 =0b01000000

~ is Ones compliment , it actually inverts the bits , 0 become 1s and 1s become 0s so
~(1<<0) =0b11111110
~(1<<1) =0b11111101
~(1<<6) =0b10111111

DDRA&=~(1<<AINpin) is the same as DDRA= DDRA & ~(1<<AINpin).
When you AND any bit with 1 it keeps its value, when you AND it with 0 it becomes 0
DDRA &= ~(1<<0) is the same as DDRA= DDRA & 0b11111110, this will clear (set to 0 bit0 of DDRA) and all other bits will keep the value they had
DDRA &= ~(1<<6) is the same as DDRA= DDRA & 0b10111111, this will clear (set to 0 bit6 of DDRA) and all other bits will keep the value they had

on the other hand DDRA=0x01 will force the value 0b00000001 , it will write these values to each bits of DDRA

Alex

Big difference:

The DDRA = 0x01 explicitly sets the value of DDRA to 0x01.

DDRA &= ~(1<<AINpin) performs a logic AND function with the value of the inverted binary number created by a single bit 'AINpin'. It acts on whatever value was already in DDRA.

For example, if AINpin is bit 3, (1<<AINpin) would be 00001000 and ~(1<<AINpin) would therefore be 11110111. The logic AND function therefore makes bit 3 of DDRA into a zero while leaving the other bits as they were.

Brian.

Sorry Alex - we were typing at the same time. Same explanation though!

Last edited:

Sorry Alex - we were typing at the same time. Same explanation though!

Yes happens a lot but it's not a bad thing, why be sorry for trying to help, after all two answers are better than none, I have seen this in other cases where the question in very simple and each one leaves someone else to answer the question and in the end nobody answers it.

Best regards
Alex

Status
Not open for further replies.