Register PortB and logical AND

Hi, I was trying this code to blink the LED on pin 13.

const int led_pin = PB5;

void setup () {

DDRB |= (1 << led_pin); // set led_pin to be output

}

void loop () {

PORTB ^= (1 << led_pin);
delay(1000);

}

This code works, but I don’t understand what happens on the register PORTB. The processor flips the register after the delay ?

::slight_smile: :disappointed_relieved:

Well normally the AND ^ don’t flip the bit on position 13. 1 ^ 1 = 1 and 0 ^ 1 = 0. Why the led blinks ?

Thank you ;D

killwin:
Well normally the AND ^ don't flip the bit on position 13. 1 ^ 1 = 1 and 0 ^ 1 = 0. Why the led blinks ?

That is because a bitwise AND is & The ^ is an exclusive-or.

https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwisexor/

killwin:
Hi, I was trying this code to blink the LED on pin 13.

This code works, but I don't understand what happens on the register PORTB. The processor flips the register after the delay ?

before the delay...

::slight_smile: :disappointed_relieved:

Well normally the AND ^ don't flip the bit on position 13. 1 ^ 1 = 1 and 0 ^ 1 = 0. Why the led blinks ?

Thank you ;D

1 ^ 1 == 0
1 ^ 0 == 1
0 ^ 1 == 1
0 ^ 0 == 0