c++arduino

What is the meaning of 0b00 and 0b11 in C/C++?


When programming an Arduino in C/C++ the line "DDRB |= 0b00101000;" occurs. While I know DDRB is the Data Direction Register for port B and the meaning of the numbers after "0b00" (which are the slots 13 to 9), I still don't really know what "0b00" means.

In definitions, I only read it means HIGH (while 0b11 means LOW), but what does that mean?

Full code:

#include <avr/io.h>
#include <util/delay.h>

int main (void) {

  float seconds = 0.5;
  int time = 1000 * seconds;

  DDRB |= 0b00101000;

  while (1) {

    PORTB |= 0b00001000;
    _delay_ms(time);
    PORTB &= 0b11110111;
    PORTB |= 0b00100000;
    _delay_ms(time);
    PORTB &= 0b11011111;
  }
  return 0;
}

Solution

  • 0b means that a number in binary representation is expected.

    For Data direction registers, setting the bits to 1 will make the respective lines outputs, and setting to 0 will make them inputs.

    The DDRB |= 0b00101000 will do a binary OR operation between the current value of the bits in DDRB with the mask.

    This will result in DDRB = 0b××1×1xxx, so that means DDRB will keep the value for lines 7 and 6. This operation basically sets lines 5 and 3 as Output and leaves the rest as they were.