Behind digitalWrite
digitalWrite(13, HIGH) first looks up pin 13 in tables stored in flash: port B, bit 5. It checks whether a timer drives the pin, pauses interrupts, sets that one bit in a register, then restores the interrupt setting it saved.
The Uno's LED, marked L, sits on pin 13. Inside the ATmega328P that pin is PB5: bit 5 of port B. A port is a group of up to eight pins, and each pin owns one bit in three registers.
Port B's three registers are bytes in the chip's data memory: PINB at 0x23, DDRB at 0x24, PORTB at 0x25. avr-libc turns PORTB into *(volatile uint8_t *)0x25, a pointer to that byte. volatile makes every read and write really happen.
Blink by hand
1 << 5 shifts a 1 five places left: the mask 0b00100000, or 0x20. DDRB |= mask reads DDRB, ORs in the mask and writes it back. Bit 5 becomes 1, so PB5 is an output. The other seven bits keep their values.
PORTB |= 1 << 5 sets bit 5 of PORTB the same way. PB5 is an output, so the pin is driven high, to 5 volts, and the LED lights. PINB bit 5 reads the pin, so it shows 1 too.
_delay_ms(500) from avr-libc busy-waits: it just burns clock cycles. It must know the clock, F_CPU, which is 16 MHz on the Uno, so 500 ms is 8,000,000 cycles. Meanwhile PORTB keeps its value and the LED stays on.
~ flips every bit of the mask: ~0x20 is 0xDF, 0b11011111. PORTB &= 0xDF keeps every other bit and forces bit 5 to 0. The pin drops to 0 volts, the LED goes dark, and PINB bit 5 reads 0 again.
Another 500 ms wait, then while (1) jumps back to line 3: on, wait, off, wait, forever. That is one blink a second with no Arduino functions at all: one register sets the direction, one sets the level.
Shortcuts and trade-offs
The datasheet adds a shortcut: writing a 1 to a bit of PINB flips that bit of PORTB. PINB = 1 << 5; toggles the LED with one write and no read first. The 0 bits in the value change nothing.
With optimisation on, avr-gcc can turn PORTB |= 1 << 5 into one sbi instruction: 2 clock cycles, 125 ns at 16 MHz. digitalWrite repeats its lookups on every call. The price: this code only runs on chips with this port B.
What happens
;; step 1/10 · library digitalWrite(13, HIGH) Arduino core, wiring_digital.c timer = table[13] -> none on pin 13 bit = table[13] -> _BV(5) = 0x20 port = table[13] -> port B save SREG, cli() pause interrupts *out |= bit PORTB |= 0x20 restore SREG interrupts back on all that work, for one bit
The card shows the body of main(). The whole program defines F_CPU as 16000000UL, includes <avr/io.h> and <util/delay.h>, and is built with avr-gcc and avr-libc for the ATmega328P, with no Arduino core. _delay_ms needs optimisation turned on and a constant argument to be exact. Port B has 8 bits, but on the Uno PB6 and PB7 are the XTAL pins of the 16 MHz clock, so only PB0 to PB5 reach the headers, as pins 8 to 13. The figure shows the unused PINB bits as 0; a real input pin with nothing connected can read either value.