Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Digital I/O

Bit Manipulation and Direct Port Access

Learn the bitwise operators used in Arduino code, set, clear and toggle bits, and use DDRx, PORTx and PINx registers to drive many pins at once much faster than digitalWrite.

Underneath digitalWrite(), an AVR chip controls pins by flipping individual bits in special memory locations called registers. Knowing how to manipulate bits lets you pack flags into a byte, read shift registers, decode sensor data and, if you need speed, drive eight pins in a single instruction. This lesson teaches the bitwise operators, which run perfectly well on your computer here, then shows how the Uno's port registers use them.

Bitwise operators

Operator Name Example (8-bit) Result
& AND 0b1100 & 0b1010 0b1000
| OR 0b1100 | 0b1010 0b1110
^ XOR 0b1100 ^ 0b1010 0b0110
~ NOT ~0b00001111 0b11110000
<< shift left 1 << 3 0b1000
>> shift right 0b1000 >> 2 0b0010

Arduino also accepts binary literals such as 0b10100000 and the helper macros bit(n) (same as 1 << n), bitSet, bitClear, bitRead and bitWrite.

The four everyday recipes

With reg as a byte and n as the bit number (0 is the least significant):

TEXT
set bit n:      reg |=  (1 << n)
clear bit n:    reg &= ~(1 << n)
toggle bit n:   reg ^=  (1 << n)
test bit n:     (reg >> n) & 1      or      reg & (1 << n)
C Set, clear, toggle and test bits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Port registers on the ATmega328P

The Uno's pins are grouped into three 8-bit ports:

Port Arduino pins
PORTB 8 to 13 (bits 0 to 5)
PORTC A0 to A5 (bits 0 to 5)
PORTD 0 to 7 (bits 0 to 7)

Each port has three registers with the port letter replacing x:

  • DDRx (data direction): 1 = output, 0 = input, one bit per pin. This is pinMode.
  • PORTx: for outputs, 1 = HIGH and 0 = LOW; for inputs, 1 enables the pull-up. This is digitalWrite.
  • PINx: reading gives the current pin levels. This is digitalRead. Writing a 1 to a bit toggles the output.

Pin 13 is bit 5 of PORTB. Blinking it directly, read-only:

C++
void setup() {
  DDRB |= (1 << DDB5);          // pin 13 as output
}

void loop() {
  PORTB |= (1 << PORTB5);       // HIGH
  delay(500);
  PORTB &= ~(1 << PORTB5);      // LOW
  delay(500);
}

DDB5 and PORTB5 are constants equal to 5, defined in the AVR headers. Setting pins 2 to 7 as outputs and setting them all HIGH takes one line each: DDRD |= 0b11111100; PORTD |= 0b11111100;. Beware of overwriting pins 0 and 1 (serial) by accident; always use |= and &= to change only the bits you intend.

C Stand-in: a simulated PORTB register

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Legend: H = output high, L = output low, - = input. Writing four pins with one assignment is the real advantage of direct port access.

Speed and trade-offs

digitalWrite() has to look up the pin's port and bit, check whether PWM is active on the pin and turn off interrupts briefly: about 3 to 5 microseconds. Writing a port register takes two clock cycles at 16 MHz, 0.125 microseconds. That matters for bit-banged protocols, LED multiplexing or fast waveforms. The cost is code that is harder to read and only works on the AVR family you wrote it for. Use digitalWrite() unless you measured a need, or use libraries such as digitalWriteFast.

Flags in a byte

Bit manipulation is just as useful in plain logic. Eight yes/no states fit in a single byte instead of eight booleans:

C Stand-in: packing eight alarm flags into one byte

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement sets bit 3 of x without changing other bits?

    OR-ing with a mask sets that bit and leaves the rest alone.
  2. 2. What does DDRD |= 0b00001000; do?

    DDRD is the direction register; a 1 bit means output. Bit 3 of PORTD is pin 3.
  3. 3. Which register do you read to get input levels on port B?

    PINx reads the pin levels.
  4. 4. Why is direct port access faster than digitalWrite()?

    A register write is a couple of cycles; digitalWrite does a lookup and extra work.

Summary

  • &, |, ^, ~, << and >> set, clear, toggle and test individual bits.
  • AVR pins live in ports; DDRx sets direction, PORTx sets output level and pull-up, PINx reads.
  • Pin 13 is PORTB bit 5; pins 0 to 7 are PORTD; A0 to A5 are PORTC.
  • Port writes are much faster and set many pins at once, but are less portable and readable.
  • Packing flags into bytes saves memory.

Next: Reading Analog Inputs with analogRead